Skip to content

Commit 1570e70

Browse files
authored
chore: use sequence for typing rather than list (#970)
* 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
1 parent 868cf4a commit 1570e70

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This project adheres to `Semantic Versioning <https://semver.org/>`__.
1010
Changed
1111
~~~~~~~
1212

13+
- Use ``Sequence`` for parameter types rather than ``List`` where applicable by @imnotjames in `#970 <https://github.com/jpadilla/pyjwt/pull/970>`__
14+
1315
Fixed
1416
~~~~~
1517

jwt/api_jws.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import binascii
44
import json
55
import warnings
6+
from collections.abc import Sequence
67
from typing import TYPE_CHECKING, Any
78

89
from .algorithms import (
@@ -30,7 +31,7 @@ class PyJWS:
3031

3132
def __init__(
3233
self,
33-
algorithms: list[str] | None = None,
34+
algorithms: Sequence[str] | None = None,
3435
options: dict[str, Any] | None = None,
3536
) -> None:
3637
self._algorithms = get_default_algorithms()
@@ -174,7 +175,7 @@ def decode_complete(
174175
self,
175176
jwt: str | bytes,
176177
key: AllowedPublicKeys | PyJWK | str | bytes = "",
177-
algorithms: list[str] | None = None,
178+
algorithms: Sequence[str] | None = None,
178179
options: dict[str, Any] | None = None,
179180
detached_payload: bytes | None = None,
180181
**kwargs,
@@ -219,7 +220,7 @@ def decode(
219220
self,
220221
jwt: str | bytes,
221222
key: AllowedPublicKeys | PyJWK | str | bytes = "",
222-
algorithms: list[str] | None = None,
223+
algorithms: Sequence[str] | None = None,
223224
options: dict[str, Any] | None = None,
224225
detached_payload: bytes | None = None,
225226
**kwargs,
@@ -291,7 +292,7 @@ def _verify_signature(
291292
header: dict[str, Any],
292293
signature: bytes,
293294
key: AllowedPublicKeys | PyJWK | str | bytes = "",
294-
algorithms: list[str] | None = None,
295+
algorithms: Sequence[str] | None = None,
295296
) -> None:
296297
if algorithms is None and isinstance(key, PyJWK):
297298
algorithms = [key.algorithm_name]

jwt/api_jwt.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import json
44
import warnings
55
from calendar import timegm
6-
from collections.abc import Iterable
6+
from collections.abc import Iterable, Sequence
77
from datetime import datetime, timedelta, timezone
8-
from typing import TYPE_CHECKING, Any, List
8+
from typing import TYPE_CHECKING, Any
99

1010
from . import api_jws
1111
from .exceptions import (
@@ -102,7 +102,7 @@ def decode_complete(
102102
self,
103103
jwt: str | bytes,
104104
key: AllowedPublicKeys | PyJWK | str | bytes = "",
105-
algorithms: list[str] | None = None,
105+
algorithms: Sequence[str] | None = None,
106106
options: dict[str, Any] | None = None,
107107
# deprecated arg, remove in pyjwt3
108108
verify: bool | None = None,
@@ -111,7 +111,7 @@ def decode_complete(
111111
# passthrough arguments to _validate_claims
112112
# consider putting in options
113113
audience: str | Iterable[str] | None = None,
114-
issuer: str | List[str] | None = None,
114+
issuer: str | Sequence[str] | None = None,
115115
leeway: float | timedelta = 0,
116116
# kwargs
117117
**kwargs: Any,
@@ -187,7 +187,7 @@ def decode(
187187
self,
188188
jwt: str | bytes,
189189
key: AllowedPublicKeys | PyJWK | str | bytes = "",
190-
algorithms: list[str] | None = None,
190+
algorithms: Sequence[str] | None = None,
191191
options: dict[str, Any] | None = None,
192192
# deprecated arg, remove in pyjwt3
193193
verify: bool | None = None,
@@ -196,7 +196,7 @@ def decode(
196196
# passthrough arguments to _validate_claims
197197
# consider putting in options
198198
audience: str | Iterable[str] | None = None,
199-
issuer: str | List[str] | None = None,
199+
issuer: str | Sequence[str] | None = None,
200200
leeway: float | timedelta = 0,
201201
# kwargs
202202
**kwargs: Any,
@@ -363,7 +363,7 @@ def _validate_iss(self, payload: dict[str, Any], issuer: Any) -> None:
363363
if "iss" not in payload:
364364
raise MissingRequiredClaimError("iss")
365365

366-
if isinstance(issuer, list):
366+
if isinstance(issuer, Sequence):
367367
if payload["iss"] not in issuer:
368368
raise InvalidIssuerError("Invalid issuer")
369369
else:

0 commit comments

Comments
 (0)