Skip to content

Commit 31f5acb

Browse files
authored
Replace various string interpolations with f-strings (jpadilla#744)
1 parent 5581a31 commit 31f5acb

File tree

7 files changed

+18
-21
lines changed

7 files changed

+18
-21
lines changed

jwt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
__description__ = "JSON Web Token implementation in Python"
3232
__url__ = "https://pyjwt.readthedocs.io"
3333
__uri__ = __url__
34-
__doc__ = __description__ + " <" + __uri__ + ">"
34+
__doc__ = f"{__description__} <{__uri__}>"
3535

3636
__author__ = "José Padilla"
3737
__email__ = "hello@jpadilla.com"

jwt/api_jwk.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, jwk_data, algorithm=None):
1111

1212
kty = self._jwk_data.get("kty", None)
1313
if not kty:
14-
raise InvalidKeyError("kty is not found: %s" % self._jwk_data)
14+
raise InvalidKeyError(f"kty is not found: {self._jwk_data}")
1515

1616
if not algorithm and isinstance(self._jwk_data, dict):
1717
algorithm = self._jwk_data.get("alg", None)
@@ -29,25 +29,25 @@ def __init__(self, jwk_data, algorithm=None):
2929
elif crv == "secp256k1":
3030
algorithm = "ES256K"
3131
else:
32-
raise InvalidKeyError("Unsupported crv: %s" % crv)
32+
raise InvalidKeyError(f"Unsupported crv: {crv}")
3333
elif kty == "RSA":
3434
algorithm = "RS256"
3535
elif kty == "oct":
3636
algorithm = "HS256"
3737
elif kty == "OKP":
3838
if not crv:
39-
raise InvalidKeyError("crv is not found: %s" % self._jwk_data)
39+
raise InvalidKeyError(f"crv is not found: {self._jwk_data}")
4040
if crv == "Ed25519":
4141
algorithm = "EdDSA"
4242
else:
43-
raise InvalidKeyError("Unsupported crv: %s" % crv)
43+
raise InvalidKeyError(f"Unsupported crv: {crv}")
4444
else:
45-
raise InvalidKeyError("Unsupported kty: %s" % kty)
45+
raise InvalidKeyError(f"Unsupported kty: {kty}")
4646

4747
self.Algorithm = self._algorithms.get(algorithm)
4848

4949
if not self.Algorithm:
50-
raise PyJWKError("Unable to find a algorithm for key: %s" % self._jwk_data)
50+
raise PyJWKError(f"Unable to find a algorithm for key: {self._jwk_data}")
5151

5252
self.key = self.Algorithm.from_jwk(self._jwk_data)
5353

@@ -100,4 +100,4 @@ def __getitem__(self, kid):
100100
for key in self.keys:
101101
if key.key_id == kid:
102102
return key
103-
raise KeyError("keyset has no key for kid: %s" % kid)
103+
raise KeyError(f"keyset has no key for kid: {kid}")

jwt/api_jws.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ def encode(
136136
except KeyError as e:
137137
if not has_crypto and algorithm in requires_cryptography:
138138
raise NotImplementedError(
139-
"Algorithm '%s' could not be found. Do you have cryptography "
140-
"installed?" % algorithm
139+
f"Algorithm '{algorithm}' could not be found. Do you have cryptography installed?"
141140
) from e
142141
raise NotImplementedError("Algorithm not supported") from e
143142

@@ -231,7 +230,7 @@ def _load(self, jwt):
231230
try:
232231
header = json.loads(header_data)
233232
except ValueError as e:
234-
raise DecodeError("Invalid header string: %s" % e) from e
233+
raise DecodeError(f"Invalid header string: {e}") from e
235234

236235
if not isinstance(header, Mapping):
237236
raise DecodeError("Invalid header string: must be a json object")

jwt/api_jwt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def decode_complete(
108108
try:
109109
payload = json.loads(decoded["payload"])
110110
except ValueError as e:
111-
raise DecodeError("Invalid payload string: %s" % e)
111+
raise DecodeError(f"Invalid payload string: {e}")
112112
if not isinstance(payload, dict):
113113
raise DecodeError("Invalid payload string: must be a json object")
114114

jwt/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, claim):
5151
self.claim = claim
5252

5353
def __str__(self):
54-
return 'Token is missing the "%s" claim' % self.claim
54+
return f'Token is missing the "{self.claim}" claim'
5555

5656

5757
class PyJWKError(PyJWTError):

jwt/help.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def info():
2828
if implementation == "CPython":
2929
implementation_version = platform.python_version()
3030
elif implementation == "PyPy":
31-
implementation_version = "{}.{}.{}".format(
32-
sys.pypy_version_info.major,
33-
sys.pypy_version_info.minor,
34-
sys.pypy_version_info.micro,
31+
implementation_version = (
32+
f"{sys.pypy_version_info.major}."
33+
f"{sys.pypy_version_info.minor}."
34+
f"{sys.pypy_version_info.micro}"
3535
)
3636
if sys.pypy_version_info.releaselevel != "final":
3737
implementation_version = "".join(

tests/test_algorithms.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,14 @@ def test_ec_jwk_fails_on_invalid_json(self):
226226
for curve in ("P-256", "P-384", "P-521", "secp256k1"):
227227
with pytest.raises(InvalidKeyError):
228228
algo.from_jwk(
229-
'{{"kty": "EC", "crv": "{}", "x": "dGVzdA==", '
230-
'"y": "dGVzdA=="}}'.format(curve)
229+
f'{{"kty": "EC", "crv": "{curve}", "x": "dGVzdA==", "y": "dGVzdA=="}}'
231230
)
232231

233232
# EC private key length invalid
234233
for (curve, point) in valid_points.items():
235234
with pytest.raises(InvalidKeyError):
236235
algo.from_jwk(
237-
'{{"kty": "EC", "crv": "{}", "x": "{}", "y": "{}", '
238-
'"d": "dGVzdA=="}}'.format(curve, point["x"], point["y"])
236+
f'{{"kty": "EC", "crv": "{curve}", "x": "{point["x"]}", "y": "{point["y"]}", "d": "dGVzdA=="}}'
239237
)
240238

241239
@crypto_required

0 commit comments

Comments
 (0)