Skip to content

Commit b85050f

Browse files
chore(tests): enable mypy (#1138)
* chore(tests): enable mypy Add suppressions for all current errors; these will be fixed in subsequent commits. * chore(tests): enable `no-untyped-{def,call}` * chore(tests): enable `unused-ignore` * chore(tests): enable `arg-type` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1272b26 commit b85050f

14 files changed

Lines changed: 651 additions & 425 deletions

pyproject.toml

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

9393
[tool.mypy]
94-
packages = "jwt"
94+
files = [
95+
"jwt",
96+
"tests",
97+
]
9598
strict = true
9699
warn_return_any = true
97100
warn_unused_ignores = true

tests/keys/__init__.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
11
import json
22
import os
3+
from typing import Union
34

4-
from jwt.algorithms import has_crypto
5+
from jwt.algorithms import has_crypto, AllowedRSAKeys
56
from jwt.utils import base64url_decode
67

7-
try:
8-
from cryptography.hazmat.primitives.asymmetric import ec
9-
except ModuleNotFoundError:
10-
pass
11-
12-
if has_crypto:
13-
from jwt.algorithms import RSAAlgorithm
14-
158
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
169

1710

18-
def decode_value(val):
11+
def decode_value(val: Union[str, bytes]) -> int:
1912
decoded = base64url_decode(val)
2013
return int.from_bytes(decoded, byteorder="big")
2114

2215

23-
def load_hmac_key():
16+
def load_hmac_key() -> bytes:
2417
with open(os.path.join(BASE_PATH, "jwk_hmac.json")) as infile:
2518
keyobj = json.load(infile)
2619

2720
return base64url_decode(keyobj["k"])
2821

2922

30-
def load_rsa_pub_key():
31-
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
32-
return RSAAlgorithm.from_jwk(infile.read())
23+
if has_crypto:
24+
from cryptography.hazmat.primitives.asymmetric import ec
25+
from jwt.algorithms import RSAAlgorithm
26+
27+
def load_rsa_pub_key() -> AllowedRSAKeys:
28+
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
29+
return RSAAlgorithm.from_jwk(infile.read())
3330

31+
def load_ec_pub_key_p_521() -> ec.EllipticCurvePublicKey:
32+
with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
33+
keyobj = json.load(infile)
3434

35-
def load_ec_pub_key_p_521():
36-
with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
37-
keyobj = json.load(infile)
35+
return ec.EllipticCurvePublicNumbers(
36+
x=decode_value(keyobj["x"]),
37+
y=decode_value(keyobj["y"]),
38+
curve=ec.SECP521R1(),
39+
).public_key()
40+
else:
41+
import sys
42+
43+
if sys.version_info >= (3, 11):
44+
from typing import Never
45+
else:
46+
from typing_extensions import Never
47+
48+
def load_rsa_pub_key() -> AllowedRSAKeys:
49+
raise RuntimeError("cryptography is not available")
3850

39-
return ec.EllipticCurvePublicNumbers(
40-
x=decode_value(keyobj["x"]),
41-
y=decode_value(keyobj["y"]),
42-
curve=ec.SECP521R1(),
43-
).public_key()
51+
def load_ec_pub_key_p_521() -> Never: # type: ignore[misc]
52+
raise RuntimeError("cryptography is not available")

tests/test_advisory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class TestAdvisory:
2727
@crypto_required
28-
def test_ghsa_ffqj_6fqr_9h24(self):
28+
def test_ghsa_ffqj_6fqr_9h24(self) -> None:
2929
# Generate ed25519 private key
3030
# private_key = ed25519.Ed25519PrivateKey.generate()
3131

0 commit comments

Comments
 (0)