Skip to content

Commit 6c8ae29

Browse files
committed
Improve coverage config and eliminate unused test suite code
This introduces the following changes: * Fix duplicate, overlooked coverage and pytest dependencies in the `dev` dependency * Remove now-unnecessary `pragma: no cover` comments (coverage now auto-ignores functions with `...` as their sole line) * Configure coverage to watch the code in `tests/` as well * Remove unused helper functions in the test suite * Refactor test suite code to eliminate impossible-to-reach code
1 parent d28dc35 commit 6c8ae29

File tree

6 files changed

+23
-59
lines changed

6 files changed

+23
-59
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Added
2323

2424
- Support Python 3.14, and test against PyPy 3.10 and 3.11 by @kurtmckee in `#1104 <https://github.com/jpadilla/pyjwt/pull/1104>`__
2525
- Development: Migrate to ``build`` to test package building in CI by @kurtmckee in `#1108 <https://github.com/jpadilla/pyjwt/pull/1108>`__
26+
- Development: Improve coverage config and eliminate unused test suite code by @kurtmckee in `#1115 <https://github.com/jpadilla/pyjwt/pull/1115>`__
2627
- Docs: Standardize CHANGELOG links to PRs by @kurtmckee in `#1110 <https://github.com/jpadilla/pyjwt/pull/1110>`__
2728
- Docs: Add example of using leeway with nbf by @djw8605 in `#1034 <https://github.com/jpadilla/pyjwt/pull/1034>`__
2829
- Docs: Refactored docs with ``autodoc``; added ``PyJWS`` and ``jwt.algorithms`` docs by @pachewise in `#1045 <https://github.com/jpadilla/pyjwt/pull/1045>`__

jwt/algorithms.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ def verify(self, msg: bytes, key: Any, sig: bytes) -> bool:
251251
@overload
252252
@staticmethod
253253
@abstractmethod
254-
def to_jwk(key_obj, as_dict: Literal[True]) -> JWKDict: ... # pragma: no cover
254+
def to_jwk(key_obj, as_dict: Literal[True]) -> JWKDict: ...
255255

256256
@overload
257257
@staticmethod
258258
@abstractmethod
259-
def to_jwk(key_obj, as_dict: Literal[False] = False) -> str: ... # pragma: no cover
259+
def to_jwk(key_obj, as_dict: Literal[False] = False) -> str: ...
260260

261261
@staticmethod
262262
@abstractmethod
@@ -329,15 +329,11 @@ def prepare_key(self, key: str | bytes) -> bytes:
329329

330330
@overload
331331
@staticmethod
332-
def to_jwk(
333-
key_obj: str | bytes, as_dict: Literal[True]
334-
) -> JWKDict: ... # pragma: no cover
332+
def to_jwk(key_obj: str | bytes, as_dict: Literal[True]) -> JWKDict: ...
335333

336334
@overload
337335
@staticmethod
338-
def to_jwk(
339-
key_obj: str | bytes, as_dict: Literal[False] = False
340-
) -> str: ... # pragma: no cover
336+
def to_jwk(key_obj: str | bytes, as_dict: Literal[False] = False) -> str: ...
341337

342338
@staticmethod
343339
def to_jwk(key_obj: str | bytes, as_dict: bool = False) -> JWKDict | str:
@@ -424,15 +420,11 @@ def prepare_key(self, key: AllowedRSAKeys | str | bytes) -> AllowedRSAKeys:
424420

425421
@overload
426422
@staticmethod
427-
def to_jwk(
428-
key_obj: AllowedRSAKeys, as_dict: Literal[True]
429-
) -> JWKDict: ... # pragma: no cover
423+
def to_jwk(key_obj: AllowedRSAKeys, as_dict: Literal[True]) -> JWKDict: ...
430424

431425
@overload
432426
@staticmethod
433-
def to_jwk(
434-
key_obj: AllowedRSAKeys, as_dict: Literal[False] = False
435-
) -> str: ... # pragma: no cover
427+
def to_jwk(key_obj: AllowedRSAKeys, as_dict: Literal[False] = False) -> str: ...
436428

437429
@staticmethod
438430
def to_jwk(key_obj: AllowedRSAKeys, as_dict: bool = False) -> JWKDict | str:
@@ -620,15 +612,11 @@ def verify(self, msg: bytes, key: AllowedECKeys, sig: bytes) -> bool:
620612

621613
@overload
622614
@staticmethod
623-
def to_jwk(
624-
key_obj: AllowedECKeys, as_dict: Literal[True]
625-
) -> JWKDict: ... # pragma: no cover
615+
def to_jwk(key_obj: AllowedECKeys, as_dict: Literal[True]) -> JWKDict: ...
626616

627617
@overload
628618
@staticmethod
629-
def to_jwk(
630-
key_obj: AllowedECKeys, as_dict: Literal[False] = False
631-
) -> str: ... # pragma: no cover
619+
def to_jwk(key_obj: AllowedECKeys, as_dict: Literal[False] = False) -> str: ...
632620

633621
@staticmethod
634622
def to_jwk(key_obj: AllowedECKeys, as_dict: bool = False) -> JWKDict | str:
@@ -853,15 +841,11 @@ def verify(
853841

854842
@overload
855843
@staticmethod
856-
def to_jwk(
857-
key: AllowedOKPKeys, as_dict: Literal[True]
858-
) -> JWKDict: ... # pragma: no cover
844+
def to_jwk(key: AllowedOKPKeys, as_dict: Literal[True]) -> JWKDict: ...
859845

860846
@overload
861847
@staticmethod
862-
def to_jwk(
863-
key: AllowedOKPKeys, as_dict: Literal[False] = False
864-
) -> str: ... # pragma: no cover
848+
def to_jwk(key: AllowedOKPKeys, as_dict: Literal[False] = False) -> str: ...
865849

866850
@staticmethod
867851
def to_jwk(key: AllowedOKPKeys, as_dict: bool = False) -> JWKDict | str:

pyproject.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ crypto = [
4444
"cryptography>=3.4.0",
4545
]
4646
dev = [
47-
"coverage[toml]==5.0.4",
47+
"coverage[toml]==7.10.7",
4848
"cryptography>=3.4.0",
4949
"pre-commit",
50-
"pytest>=6.0.0,<7.0.0",
50+
"pytest>=8.4.2,<9.0.0",
5151
"sphinx",
5252
"sphinx-rtd-theme",
5353
"zope.interface",
@@ -71,22 +71,23 @@ Homepage = "https://github.com/jpadilla/pyjwt"
7171

7272
[tool.coverage.paths]
7373
source = [
74-
".tox/*/site-packages",
7574
"jwt",
75+
"*/site-packages",
7676
]
7777

7878
[tool.coverage.report]
79-
exclude_lines = [
79+
skip_covered = true
80+
exclude_also = [
8081
"if TYPE_CHECKING",
81-
"pragma: no cover",
8282
]
83-
show_missing = true
8483

8584
[tool.coverage.run]
8685
branch = true
8786
parallel = true
87+
relative_files = true
8888
source = [
8989
"jwt",
90+
"tests",
9091
]
9192

9293
[tool.isort]

tests/keys/__init__.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,11 @@ def load_hmac_key():
2727
return base64url_decode(keyobj["k"])
2828

2929

30-
def load_rsa_key():
31-
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json")) as infile:
32-
return RSAAlgorithm.from_jwk(infile.read())
33-
34-
3530
def load_rsa_pub_key():
3631
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
3732
return RSAAlgorithm.from_jwk(infile.read())
3833

3934

40-
def load_ec_key():
41-
with open(os.path.join(BASE_PATH, "jwk_ec_key.json")) as infile:
42-
keyobj = json.load(infile)
43-
44-
return ec.EllipticCurvePrivateNumbers(
45-
private_value=decode_value(keyobj["d"]),
46-
public_numbers=load_ec_pub_key_p_521().public_numbers(),
47-
)
48-
49-
5035
def load_ec_pub_key_p_521():
5136
with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
5237
keyobj = json.load(infile)

tests/test_api_jws.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,8 @@ def test_decode_options_must_be_dict(self, jws, payload):
709709
def test_custom_json_encoder(self, jws, payload):
710710
class CustomJSONEncoder(json.JSONEncoder):
711711
def default(self, o):
712-
if isinstance(o, Decimal):
713-
return "it worked"
714-
return super().default(o)
712+
assert isinstance(o, Decimal)
713+
return "it worked"
715714

716715
data = {"some_decimal": Decimal("2.2")}
717716

@@ -732,14 +731,9 @@ def test_encode_headers_parameter_adds_headers(self, jws, payload):
732731
headers = {"testheader": True}
733732
token = jws.encode(payload, "secret", headers=headers)
734733

735-
if not isinstance(token, str):
736-
token = token.decode()
737-
738734
header = token[0 : token.index(".")].encode()
739735
header = base64url_decode(header)
740-
741-
if not isinstance(header, str):
742-
header = header.decode()
736+
header = header.decode()
743737

744738
header_obj = json.loads(header)
745739

tests/test_api_jwt.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,8 @@ def test_skip_check_nbf(self, jwt):
658658
def test_custom_json_encoder(self, jwt):
659659
class CustomJSONEncoder(json.JSONEncoder):
660660
def default(self, o):
661-
if isinstance(o, Decimal):
662-
return "it worked"
663-
return super().default(o)
661+
assert isinstance(o, Decimal)
662+
return "it worked"
664663

665664
data = {"some_decimal": Decimal("2.2")}
666665

0 commit comments

Comments
 (0)