From 89a386eee8305ee89737ada1b058ce9d9510af4b Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 30 Mar 2022 10:36:29 +0300 Subject: [PATCH] Replace various string interpolations with f-strings --- jwt/__init__.py | 2 +- jwt/api_jwk.py | 14 +++++++------- jwt/api_jws.py | 5 ++--- jwt/api_jwt.py | 2 +- jwt/exceptions.py | 2 +- jwt/help.py | 8 ++++---- tests/test_algorithms.py | 6 ++---- 7 files changed, 18 insertions(+), 21 deletions(-) diff --git a/jwt/__init__.py b/jwt/__init__.py index 33b69d71..e692a842 100644 --- a/jwt/__init__.py +++ b/jwt/__init__.py @@ -31,7 +31,7 @@ __description__ = "JSON Web Token implementation in Python" __url__ = "https://pyjwt.readthedocs.io" __uri__ = __url__ -__doc__ = __description__ + " <" + __uri__ + ">" +__doc__ = f"{__description__} <{__uri__}>" __author__ = "José Padilla" __email__ = "hello@jpadilla.com" diff --git a/jwt/api_jwk.py b/jwt/api_jwk.py index f58bae28..31250d57 100644 --- a/jwt/api_jwk.py +++ b/jwt/api_jwk.py @@ -11,7 +11,7 @@ def __init__(self, jwk_data, algorithm=None): kty = self._jwk_data.get("kty", None) if not kty: - raise InvalidKeyError("kty is not found: %s" % self._jwk_data) + raise InvalidKeyError(f"kty is not found: {self._jwk_data}") if not algorithm and isinstance(self._jwk_data, dict): algorithm = self._jwk_data.get("alg", None) @@ -29,25 +29,25 @@ def __init__(self, jwk_data, algorithm=None): elif crv == "secp256k1": algorithm = "ES256K" else: - raise InvalidKeyError("Unsupported crv: %s" % crv) + raise InvalidKeyError(f"Unsupported crv: {crv}") elif kty == "RSA": algorithm = "RS256" elif kty == "oct": algorithm = "HS256" elif kty == "OKP": if not crv: - raise InvalidKeyError("crv is not found: %s" % self._jwk_data) + raise InvalidKeyError(f"crv is not found: {self._jwk_data}") if crv == "Ed25519": algorithm = "EdDSA" else: - raise InvalidKeyError("Unsupported crv: %s" % crv) + raise InvalidKeyError(f"Unsupported crv: {crv}") else: - raise InvalidKeyError("Unsupported kty: %s" % kty) + raise InvalidKeyError(f"Unsupported kty: {kty}") self.Algorithm = self._algorithms.get(algorithm) if not self.Algorithm: - raise PyJWKError("Unable to find a algorithm for key: %s" % self._jwk_data) + raise PyJWKError(f"Unable to find a algorithm for key: {self._jwk_data}") self.key = self.Algorithm.from_jwk(self._jwk_data) @@ -100,4 +100,4 @@ def __getitem__(self, kid): for key in self.keys: if key.key_id == kid: return key - raise KeyError("keyset has no key for kid: %s" % kid) + raise KeyError(f"keyset has no key for kid: {kid}") diff --git a/jwt/api_jws.py b/jwt/api_jws.py index f32de8fb..cbf4f6f5 100644 --- a/jwt/api_jws.py +++ b/jwt/api_jws.py @@ -136,8 +136,7 @@ def encode( except KeyError as e: if not has_crypto and algorithm in requires_cryptography: raise NotImplementedError( - "Algorithm '%s' could not be found. Do you have cryptography " - "installed?" % algorithm + f"Algorithm '{algorithm}' could not be found. Do you have cryptography installed?" ) from e raise NotImplementedError("Algorithm not supported") from e @@ -231,7 +230,7 @@ def _load(self, jwt): try: header = json.loads(header_data) except ValueError as e: - raise DecodeError("Invalid header string: %s" % e) from e + raise DecodeError(f"Invalid header string: {e}") from e if not isinstance(header, Mapping): raise DecodeError("Invalid header string: must be a json object") diff --git a/jwt/api_jwt.py b/jwt/api_jwt.py index c9d34a5f..66552442 100644 --- a/jwt/api_jwt.py +++ b/jwt/api_jwt.py @@ -98,7 +98,7 @@ def decode_complete( try: payload = json.loads(decoded["payload"]) except ValueError as e: - raise DecodeError("Invalid payload string: %s" % e) + raise DecodeError(f"Invalid payload string: {e}") if not isinstance(payload, dict): raise DecodeError("Invalid payload string: must be a json object") diff --git a/jwt/exceptions.py b/jwt/exceptions.py index 308899aa..ee201add 100644 --- a/jwt/exceptions.py +++ b/jwt/exceptions.py @@ -51,7 +51,7 @@ def __init__(self, claim): self.claim = claim def __str__(self): - return 'Token is missing the "%s" claim' % self.claim + return f'Token is missing the "{self.claim}" claim' class PyJWKError(PyJWTError): diff --git a/jwt/help.py b/jwt/help.py index d8f23024..d5c3ebbf 100644 --- a/jwt/help.py +++ b/jwt/help.py @@ -28,10 +28,10 @@ def info(): if implementation == "CPython": implementation_version = platform.python_version() elif implementation == "PyPy": - implementation_version = "{}.{}.{}".format( - sys.pypy_version_info.major, - sys.pypy_version_info.minor, - sys.pypy_version_info.micro, + implementation_version = ( + f"{sys.pypy_version_info.major}." + f"{sys.pypy_version_info.minor}." + f"{sys.pypy_version_info.micro}" ) if sys.pypy_version_info.releaselevel != "final": implementation_version = "".join( diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index f4ab75bb..fca930cf 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -226,16 +226,14 @@ def test_ec_jwk_fails_on_invalid_json(self): for curve in ("P-256", "P-384", "P-521", "secp256k1"): with pytest.raises(InvalidKeyError): algo.from_jwk( - '{{"kty": "EC", "crv": "{}", "x": "dGVzdA==", ' - '"y": "dGVzdA=="}}'.format(curve) + f'{{"kty": "EC", "crv": "{curve}", "x": "dGVzdA==", "y": "dGVzdA=="}}' ) # EC private key length invalid for (curve, point) in valid_points.items(): with pytest.raises(InvalidKeyError): algo.from_jwk( - '{{"kty": "EC", "crv": "{}", "x": "{}", "y": "{}", ' - '"d": "dGVzdA=="}}'.format(curve, point["x"], point["y"]) + f'{{"kty": "EC", "crv": "{curve}", "x": "{point["x"]}", "y": "{point["y"]}", "d": "dGVzdA=="}}' ) @crypto_required