Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace various string interpolations with f-strings #744

Merged
merged 1 commit into from Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion jwt/__init__.py
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions jwt/api_jwk.py
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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}")
5 changes: 2 additions & 3 deletions jwt/api_jws.py
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion jwt/api_jwt.py
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion jwt/exceptions.py
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions jwt/help.py
Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions tests/test_algorithms.py
Expand Up @@ -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
Expand Down