summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2022-04-12 16:16:25 +0300
committerGitHub <noreply@github.com>2022-04-12 19:16:25 +0600
commit31f5acb8fb3ec6cdfe2b1b0a4a8f329b5f3ca67f (patch)
treed2204cda86a9657113c9fff66ab79ddd36abc561
parent5581a31c21de70444c1162bcfa29f7e0fc86edda (diff)
downloadpyjwt-31f5acb8fb3ec6cdfe2b1b0a4a8f329b5f3ca67f.tar.gz
Replace various string interpolations with f-strings (#744)
-rw-r--r--jwt/__init__.py2
-rw-r--r--jwt/api_jwk.py14
-rw-r--r--jwt/api_jws.py5
-rw-r--r--jwt/api_jwt.py2
-rw-r--r--jwt/exceptions.py2
-rw-r--r--jwt/help.py8
-rw-r--r--tests/test_algorithms.py6
7 files changed, 18 insertions, 21 deletions
diff --git a/jwt/__init__.py b/jwt/__init__.py
index 33b69d7..e692a84 100644
--- a/jwt/__init__.py
+++ b/jwt/__init__.py
@@ -31,7 +31,7 @@ __title__ = "PyJWT"
__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 f58bae2..31250d5 100644
--- a/jwt/api_jwk.py
+++ b/jwt/api_jwk.py
@@ -11,7 +11,7 @@ class PyJWK:
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 @@ class PyJWK:
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 @@ class PyJWKSet:
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 f32de8f..cbf4f6f 100644
--- a/jwt/api_jws.py
+++ b/jwt/api_jws.py
@@ -136,8 +136,7 @@ class PyJWS:
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 @@ class PyJWS:
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 5e11bc8..7d2177b 100644
--- a/jwt/api_jwt.py
+++ b/jwt/api_jwt.py
@@ -108,7 +108,7 @@ class PyJWT:
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 308899a..ee201ad 100644
--- a/jwt/exceptions.py
+++ b/jwt/exceptions.py
@@ -51,7 +51,7 @@ class MissingRequiredClaimError(InvalidTokenError):
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 d8f2302..d5c3ebb 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 f4ab75b..fca930c 100644
--- a/tests/test_algorithms.py
+++ b/tests/test_algorithms.py
@@ -226,16 +226,14 @@ class TestAlgorithms:
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