Skip to content

Commit 99a8728

Browse files
authored
chore: remove superfluous constants (#1136)
* chore: remove superfluous constants This Py >= 3.10 TODO is doable even on 3.9, so do it. * chore: tighten no-crypto mypy typing Scope mypy missing-import suppression to cryptography only, and make no-crypto key aliases explicit with Never.
1 parent 412cb67 commit 99a8728

File tree

3 files changed

+67
-67
lines changed

3 files changed

+67
-67
lines changed

jwt/algorithms.py

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hmac
55
import json
66
import os
7+
import sys
78
from abc import ABC, abstractmethod
89
from typing import (
910
TYPE_CHECKING,
@@ -13,6 +14,7 @@
1314
NoReturn,
1415
Union,
1516
cast,
17+
get_args,
1618
overload,
1719
)
1820

@@ -75,66 +77,47 @@
7577
load_ssh_public_key,
7678
)
7779

78-
# pyjwt-964: we use these both for type checking below, as well as for validating the key passed in.
79-
# in Py >= 3.10, we can replace this with the Union types below
80-
ALLOWED_RSA_KEY_TYPES = (RSAPrivateKey, RSAPublicKey)
81-
ALLOWED_EC_KEY_TYPES = (EllipticCurvePrivateKey, EllipticCurvePublicKey)
82-
ALLOWED_OKP_KEY_TYPES = (
83-
Ed25519PrivateKey,
84-
Ed25519PublicKey,
85-
Ed448PrivateKey,
86-
Ed448PublicKey,
87-
)
88-
ALLOWED_KEY_TYPES = (
89-
ALLOWED_RSA_KEY_TYPES + ALLOWED_EC_KEY_TYPES + ALLOWED_OKP_KEY_TYPES
90-
)
91-
ALLOWED_PRIVATE_KEY_TYPES = (
92-
RSAPrivateKey,
93-
EllipticCurvePrivateKey,
94-
Ed25519PrivateKey,
95-
Ed448PrivateKey,
96-
)
97-
ALLOWED_PUBLIC_KEY_TYPES = (
98-
RSAPublicKey,
99-
EllipticCurvePublicKey,
100-
Ed25519PublicKey,
101-
Ed448PublicKey,
102-
)
80+
if sys.version_info >= (3, 10):
81+
from typing import TypeAlias
82+
else:
83+
# Python 3.9 and lower
84+
from typing_extensions import TypeAlias
85+
86+
# Type aliases for convenience in algorithms method signatures
87+
AllowedRSAKeys: TypeAlias = Union[RSAPrivateKey, RSAPublicKey]
88+
AllowedECKeys: TypeAlias = Union[EllipticCurvePrivateKey, EllipticCurvePublicKey]
89+
AllowedOKPKeys: TypeAlias = Union[
90+
Ed25519PrivateKey, Ed25519PublicKey, Ed448PrivateKey, Ed448PublicKey
91+
]
92+
AllowedKeys: TypeAlias = Union[AllowedRSAKeys, AllowedECKeys, AllowedOKPKeys]
93+
#: Type alias for allowed ``cryptography`` private keys (requires ``cryptography`` to be installed)
94+
AllowedPrivateKeys: TypeAlias = Union[
95+
RSAPrivateKey, EllipticCurvePrivateKey, Ed25519PrivateKey, Ed448PrivateKey
96+
]
97+
#: Type alias for allowed ``cryptography`` public keys (requires ``cryptography`` to be installed)
98+
AllowedPublicKeys: TypeAlias = Union[
99+
RSAPublicKey, EllipticCurvePublicKey, Ed25519PublicKey, Ed448PublicKey
100+
]
103101

104102
if TYPE_CHECKING or bool(os.getenv("SPHINX_BUILD", "")):
105-
import sys
106-
107-
if sys.version_info >= (3, 10):
108-
from typing import TypeAlias
109-
else:
110-
# Python 3.9 and lower
111-
from typing_extensions import TypeAlias
112-
113103
from cryptography.hazmat.primitives.asymmetric.types import (
114104
PrivateKeyTypes,
115105
PublicKeyTypes,
116106
)
117107

118-
# Type aliases for convenience in algorithms method signatures
119-
AllowedRSAKeys: TypeAlias = Union[RSAPrivateKey, RSAPublicKey]
120-
AllowedECKeys: TypeAlias = Union[
121-
EllipticCurvePrivateKey, EllipticCurvePublicKey
122-
]
123-
AllowedOKPKeys: TypeAlias = Union[
124-
Ed25519PrivateKey, Ed25519PublicKey, Ed448PrivateKey, Ed448PublicKey
125-
]
126-
AllowedKeys: TypeAlias = Union[AllowedRSAKeys, AllowedECKeys, AllowedOKPKeys]
127-
#: Type alias for allowed ``cryptography`` private keys (requires ``cryptography`` to be installed)
128-
AllowedPrivateKeys: TypeAlias = Union[
129-
RSAPrivateKey, EllipticCurvePrivateKey, Ed25519PrivateKey, Ed448PrivateKey
130-
]
131-
#: Type alias for allowed ``cryptography`` public keys (requires ``cryptography`` to be installed)
132-
AllowedPublicKeys: TypeAlias = Union[
133-
RSAPublicKey, EllipticCurvePublicKey, Ed25519PublicKey, Ed448PublicKey
134-
]
135-
136108
has_crypto = True
137109
except ModuleNotFoundError:
110+
if sys.version_info >= (3, 11):
111+
from typing import Never
112+
else:
113+
from typing_extensions import Never
114+
115+
AllowedRSAKeys = Never # type: ignore[misc]
116+
AllowedECKeys = Never # type: ignore[misc]
117+
AllowedOKPKeys = Never # type: ignore[misc]
118+
AllowedKeys = Never # type: ignore[misc]
119+
AllowedPrivateKeys = Never # type: ignore[misc]
120+
AllowedPublicKeys = Never # type: ignore[misc]
138121
has_crypto = False
139122

140123

@@ -417,7 +400,10 @@ class RSAAlgorithm(Algorithm):
417400
SHA384: ClassVar[type[hashes.HashAlgorithm]] = hashes.SHA384
418401
SHA512: ClassVar[type[hashes.HashAlgorithm]] = hashes.SHA512
419402

420-
_crypto_key_types = ALLOWED_RSA_KEY_TYPES
403+
_crypto_key_types = cast(
404+
tuple[type[AllowedKeys], ...],
405+
get_args(Union[RSAPrivateKey, RSAPublicKey]),
406+
)
421407
_MIN_KEY_SIZE: ClassVar[int] = 2048
422408

423409
def __init__(self, hash_alg: type[hashes.HashAlgorithm]) -> None:
@@ -434,7 +420,7 @@ def check_key_length(self, key: AllowedRSAKeys) -> str | None:
434420

435421
def prepare_key(self, key: AllowedRSAKeys | str | bytes) -> AllowedRSAKeys:
436422
if isinstance(key, self._crypto_key_types):
437-
return key
423+
return cast(AllowedRSAKeys, key)
438424

439425
if not isinstance(key, (bytes, str)):
440426
raise TypeError("Expecting a PEM-formatted key.")
@@ -602,7 +588,10 @@ class ECAlgorithm(Algorithm):
602588
SHA384: ClassVar[type[hashes.HashAlgorithm]] = hashes.SHA384
603589
SHA512: ClassVar[type[hashes.HashAlgorithm]] = hashes.SHA512
604590

605-
_crypto_key_types = ALLOWED_EC_KEY_TYPES
591+
_crypto_key_types = cast(
592+
tuple[type[AllowedKeys], ...],
593+
get_args(Union[EllipticCurvePrivateKey, EllipticCurvePublicKey]),
594+
)
606595

607596
def __init__(
608597
self,
@@ -625,8 +614,9 @@ def _validate_curve(self, key: AllowedECKeys) -> None:
625614

626615
def prepare_key(self, key: AllowedECKeys | str | bytes) -> AllowedECKeys:
627616
if isinstance(key, self._crypto_key_types):
628-
self._validate_curve(key)
629-
return key
617+
ec_key = cast(AllowedECKeys, key)
618+
self._validate_curve(ec_key)
619+
return ec_key
630620

631621
if not isinstance(key, (bytes, str)):
632622
raise TypeError("Expecting a PEM-formatted key.")
@@ -840,7 +830,17 @@ class OKPAlgorithm(Algorithm):
840830
This class requires ``cryptography>=2.6`` to be installed.
841831
"""
842832

843-
_crypto_key_types = ALLOWED_OKP_KEY_TYPES
833+
_crypto_key_types = cast(
834+
tuple[type[AllowedKeys], ...],
835+
get_args(
836+
Union[
837+
Ed25519PrivateKey,
838+
Ed25519PublicKey,
839+
Ed448PrivateKey,
840+
Ed448PublicKey,
841+
]
842+
),
843+
)
844844

845845
def __init__(self, **kwargs: Any) -> None:
846846
pass

jwt/api_jwt.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,12 @@
3131
# Python 3.9 and lower
3232
from typing_extensions import TypeAlias
3333

34-
from .algorithms import has_crypto
34+
from .algorithms import AllowedPrivateKeys, AllowedPublicKeys
3535
from .api_jwk import PyJWK
3636
from .types import FullOptions, Options, SigOptions
3737

38-
if has_crypto:
39-
from .algorithms import AllowedPrivateKeys, AllowedPublicKeys
40-
41-
AllowedPrivateKeyTypes: TypeAlias = Union[AllowedPrivateKeys, PyJWK, str, bytes]
42-
AllowedPublicKeyTypes: TypeAlias = Union[AllowedPublicKeys, PyJWK, str, bytes]
43-
else:
44-
AllowedPrivateKeyTypes: TypeAlias = Union[PyJWK, str, bytes] # type: ignore
45-
AllowedPublicKeyTypes: TypeAlias = Union[PyJWK, str, bytes] # type: ignore
38+
AllowedPrivateKeyTypes: TypeAlias = Union[AllowedPrivateKeys, PyJWK, str, bytes]
39+
AllowedPublicKeyTypes: TypeAlias = Union[AllowedPublicKeys, PyJWK, str, bytes]
4640

4741

4842
class PyJWT:

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,18 @@ source = [
9191
]
9292

9393
[tool.mypy]
94-
ignore_missing_imports = true
9594
packages = "jwt"
9695
strict = true
9796
warn_return_any = true
9897
warn_unused_ignores = true
9998

99+
[[tool.mypy.overrides]]
100+
ignore_missing_imports = true
101+
module = [
102+
"cryptography",
103+
"cryptography.*",
104+
]
105+
100106
[tool.pytest.ini_options]
101107
addopts = "-ra"
102108
filterwarnings = [

0 commit comments

Comments
 (0)