Skip to content

Commit d2f4bec

Browse files
committed
Restore cast() calls with cross-version type: ignore for prepare_key
The previous commit removed the `cast()` calls because newer mypy considered them redundant. That broke Python 3.9's mypy, which still needs the cast for narrowing through `isinstance(key, self._crypto_key_types)`. Restore the casts and suppress `redundant-cast` on newer mypy while allowing `unused-ignore` on older mypy where the directive isn't needed.
1 parent 22f478c commit d2f4bec

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

jwt/algorithms.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,10 @@ def check_key_length(self, key: AllowedRSAKeys) -> str | None:
443443

444444
def prepare_key(self, key: AllowedRSAKeys | str | bytes) -> AllowedRSAKeys:
445445
if isinstance(key, self._crypto_key_types):
446-
return key
446+
# Cast is required for type narrowing on Python 3.9's mypy
447+
# but redundant on newer mypy versions; suppress both
448+
# diagnostics so the line works across all supported envs.
449+
return cast(AllowedRSAKeys, key) # type: ignore[redundant-cast,unused-ignore]
447450

448451
if not isinstance(key, (bytes, str)):
449452
raise TypeError("Expecting a PEM-formatted key.")
@@ -637,8 +640,10 @@ def _validate_curve(self, key: AllowedECKeys) -> None:
637640

638641
def prepare_key(self, key: AllowedECKeys | str | bytes) -> AllowedECKeys:
639642
if isinstance(key, self._crypto_key_types):
640-
self._validate_curve(key)
641-
return key
643+
# See note in RSAAlgorithm.prepare_key.
644+
ec_key = cast(AllowedECKeys, key) # type: ignore[redundant-cast,unused-ignore]
645+
self._validate_curve(ec_key)
646+
return ec_key
642647

643648
if not isinstance(key, (bytes, str)):
644649
raise TypeError("Expecting a PEM-formatted key.")

0 commit comments

Comments
 (0)