Skip to content

Commit

Permalink
chore: use sequence for typing rather than list (#970)
Browse files Browse the repository at this point in the history
* chore: use sequence for typing rather than list

there's no reason we need to use `List` for the typing of issuers & other parameters - the `Sequence` type allows us to accept more types such as `tuple` and `set` which still support the behavior we need

* chore: use sequence rather than list for parameters in jws

loosens up the typing on `api_jws` so we accept sequences rather than just lists

* chore: undo return type change

* chore: update changelog
  • Loading branch information
imnotjames authored Aug 7, 2024
1 parent 868cf4a commit 1570e70
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project adheres to `Semantic Versioning <https://semver.org/>`__.
Changed
~~~~~~~

- Use ``Sequence`` for parameter types rather than ``List`` where applicable by @imnotjames in `#970 <https://github.com/jpadilla/pyjwt/pull/970>`__

Fixed
~~~~~

Expand Down
9 changes: 5 additions & 4 deletions jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import binascii
import json
import warnings
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any

from .algorithms import (
Expand Down Expand Up @@ -30,7 +31,7 @@ class PyJWS:

def __init__(
self,
algorithms: list[str] | None = None,
algorithms: Sequence[str] | None = None,
options: dict[str, Any] | None = None,
) -> None:
self._algorithms = get_default_algorithms()
Expand Down Expand Up @@ -174,7 +175,7 @@ def decode_complete(
self,
jwt: str | bytes,
key: AllowedPublicKeys | PyJWK | str | bytes = "",
algorithms: list[str] | None = None,
algorithms: Sequence[str] | None = None,
options: dict[str, Any] | None = None,
detached_payload: bytes | None = None,
**kwargs,
Expand Down Expand Up @@ -219,7 +220,7 @@ def decode(
self,
jwt: str | bytes,
key: AllowedPublicKeys | PyJWK | str | bytes = "",
algorithms: list[str] | None = None,
algorithms: Sequence[str] | None = None,
options: dict[str, Any] | None = None,
detached_payload: bytes | None = None,
**kwargs,
Expand Down Expand Up @@ -291,7 +292,7 @@ def _verify_signature(
header: dict[str, Any],
signature: bytes,
key: AllowedPublicKeys | PyJWK | str | bytes = "",
algorithms: list[str] | None = None,
algorithms: Sequence[str] | None = None,
) -> None:
if algorithms is None and isinstance(key, PyJWK):
algorithms = [key.algorithm_name]
Expand Down
14 changes: 7 additions & 7 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import json
import warnings
from calendar import timegm
from collections.abc import Iterable
from collections.abc import Iterable, Sequence
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING, Any, List
from typing import TYPE_CHECKING, Any

from . import api_jws
from .exceptions import (
Expand Down Expand Up @@ -102,7 +102,7 @@ def decode_complete(
self,
jwt: str | bytes,
key: AllowedPublicKeys | PyJWK | str | bytes = "",
algorithms: list[str] | None = None,
algorithms: Sequence[str] | None = None,
options: dict[str, Any] | None = None,
# deprecated arg, remove in pyjwt3
verify: bool | None = None,
Expand All @@ -111,7 +111,7 @@ def decode_complete(
# passthrough arguments to _validate_claims
# consider putting in options
audience: str | Iterable[str] | None = None,
issuer: str | List[str] | None = None,
issuer: str | Sequence[str] | None = None,
leeway: float | timedelta = 0,
# kwargs
**kwargs: Any,
Expand Down Expand Up @@ -187,7 +187,7 @@ def decode(
self,
jwt: str | bytes,
key: AllowedPublicKeys | PyJWK | str | bytes = "",
algorithms: list[str] | None = None,
algorithms: Sequence[str] | None = None,
options: dict[str, Any] | None = None,
# deprecated arg, remove in pyjwt3
verify: bool | None = None,
Expand All @@ -196,7 +196,7 @@ def decode(
# passthrough arguments to _validate_claims
# consider putting in options
audience: str | Iterable[str] | None = None,
issuer: str | List[str] | None = None,
issuer: str | Sequence[str] | None = None,
leeway: float | timedelta = 0,
# kwargs
**kwargs: Any,
Expand Down Expand Up @@ -363,7 +363,7 @@ def _validate_iss(self, payload: dict[str, Any], issuer: Any) -> None:
if "iss" not in payload:
raise MissingRequiredClaimError("iss")

if isinstance(issuer, list):
if isinstance(issuer, Sequence):
if payload["iss"] not in issuer:
raise InvalidIssuerError("Invalid issuer")
else:
Expand Down

0 comments on commit 1570e70

Please sign in to comment.