Skip to content

Commit 82745be

Browse files
authored
Complete python-jose typing (#8258)
1 parent 6bc6289 commit 82745be

File tree

10 files changed

+72
-47
lines changed

10 files changed

+72
-47
lines changed

stubs/python-jose/@tests/stubtest_allowlist.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@ jose.backends.CryptographyECKey
44
jose.backends.CryptographyHMACKey
55
jose.backends.CryptographyRSAKey
66
jose.backends.ECDSAECKey
7-
jose.backends.ECKey
8-
jose.backends.HMACKey
9-
jose.backends.RSAKey

stubs/python-jose/METADATA.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
version = "3.3.*"
2-
requires = [] # excluding pyasn1 until typing is available
2+
requires = [] # excluding pyasn1 until typing is available
3+
4+
[tool.stubtest]
5+
ignore_missing_stub = false

stubs/python-jose/jose/__init__.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ from .exceptions import (
55
JWTError as JWTError,
66
)
77

8-
__license__: str
98
__version__: str
9+
__author__: str
10+
__license__: str
11+
__copyright__: str

stubs/python-jose/jose/backends/__init__.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ from .rsa_backend import RSAKey as BackendRSAKey
1414
# python-jose relies on importing from cryptography_backend
1515
# then falling back on other imports
1616
# these are all the potential options
17-
AESKey: CryptographyAESKey | None
18-
HMACKey: CryptographyHMACKey | NativeHMACKey
19-
RSAKey: CryptographyRSAKey | BackendRSAKey | None
20-
ECKey: CryptographyECKey | ECDSAECKey
17+
AESKey: type[CryptographyAESKey] | None
18+
HMACKey: type[CryptographyHMACKey] | type[NativeHMACKey]
19+
RSAKey: type[CryptographyRSAKey] | type[BackendRSAKey] | None
20+
ECKey: type[CryptographyECKey] | type[ECDSAECKey]
2121
get_random_bytes: Callable[[int], bytes]

stubs/python-jose/jose/constants.pyi

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from typing import Any
1+
from collections.abc import Callable, Mapping
2+
from hashlib import _Hash
3+
4+
from .backends.base import Key
25

36
class Algorithms:
47
NONE: str
@@ -56,14 +59,14 @@ class Algorithms:
5659
GCM: set[str]
5760
SUPPORTED: set[str]
5861
ALL: set[str]
59-
HASHES: Any
60-
KEYS: Any
62+
HASHES: Mapping[str, Callable[[bytes], _Hash]]
63+
KEYS: Mapping[str, type[Key]]
6164

62-
ALGORITHMS: Any
65+
ALGORITHMS: Algorithms
6366

6467
class Zips:
6568
DEF: str
66-
NONE: Any
67-
SUPPORTED: Any
69+
NONE: None
70+
SUPPORTED: set[str | None]
6871

69-
ZIPS: Any
72+
ZIPS: Zips

stubs/python-jose/jose/jwe.pyi

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
from typing import Any
22

3+
from .backends.base import Key
4+
35
def encrypt(
4-
plaintext: Any,
5-
key: dict[str, str],
6-
encryption=...,
7-
algorithm=...,
8-
zip: Any | None = ...,
9-
cty: Any | None = ...,
10-
kid: Any | None = ...,
11-
): ...
12-
def decrypt(jwe_str: str, key: str | dict[str, str]): ...
13-
def get_unverified_header(jwe_str: str): ...
6+
plaintext: str | bytes,
7+
# Internally it's passed down to jwk.construct(), which explicitly checks for
8+
# key as dict instance, instead of a Mapping
9+
key: str | bytes | dict[str, Any] | Key,
10+
encryption: str = ...,
11+
algorithm: str = ...,
12+
zip: str | None = ...,
13+
cty: str | None = ...,
14+
kid: str | None = ...,
15+
) -> bytes: ...
16+
def decrypt(
17+
jwe_str: str | bytes,
18+
# Internally it's passed down to jwk.construct(), which explicitly checks for
19+
# key as dict instance, instead of a Mapping
20+
key: str | bytes | dict[str, Any] | Key,
21+
) -> bytes | None: ...
22+
def get_unverified_header(jwe_str: str | bytes | None) -> dict[str, Any]: ...

stubs/python-jose/jose/jwk.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from typing import Any
2+
from typing_extensions import Literal
23

3-
from .backends.base import Key as Key
4+
from .backends import AESKey as AESKey, ECKey as ECKey, HMACKey as HMACKey, RSAKey as RSAKey
5+
from .backends.base import DIRKey as DIRKey, Key
46

5-
def get_key(algorithm): ...
6-
def register_key(algorithm, key_class: Key): ...
7-
def construct(key_data, algorithm: Any | None = ...): ...
7+
def get_key(algorithm: str) -> type[Key] | None: ...
8+
def register_key(algorithm: str, key_class: type[Key]) -> Literal[True]: ...
9+
def construct(
10+
# explicitly checks for key_data as dict instance, instead of a Mapping
11+
key_data: str | bytes | dict[str, Any] | Key,
12+
algorithm: str | None = ...,
13+
) -> Key: ...

stubs/python-jose/jose/jws.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ from typing import Any
44
from .backends.base import Key
55

66
def sign(
7-
payload: str | Mapping[str, Any],
7+
payload: bytes | Mapping[str, Any],
88
# Internally it's passed down to jwk.construct(), which explicitly checks for
99
# key as dict instance, instead of a Mapping
10-
key: str | dict[str, Any] | Key,
10+
key: str | bytes | dict[str, Any] | Key,
1111
headers: Mapping[str, Any] | None = ...,
1212
algorithm: str = ...,
1313
) -> str: ...
1414
def verify(
15-
token: str,
16-
key: str | Mapping[str, Any] | Key,
15+
token: str | bytes,
16+
key: str | bytes | Mapping[str, Any] | Key,
1717
# Callers of this function, like jwt.decode(), and functions called internally,
1818
# like jws._verify_signature(), use and accept algorithms=None
1919
algorithms: str | Container[str] | None,
2020
verify: bool = ...,
21-
) -> str: ...
21+
) -> bytes: ...
2222
def get_unverified_header(token: str) -> dict[str, Any]: ...
2323
def get_unverified_headers(token: str) -> dict[str, Any]: ...
2424
def get_unverified_claims(token: str) -> str: ...

stubs/python-jose/jose/jwt.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ from .backends.base import Key
66
def encode(
77
claims: MutableMapping[str, Any],
88
# Internally it calls jws.sign() that expects a key dict instance instead of Mapping
9-
key: str | dict[str, Any] | Key,
9+
key: str | bytes | dict[str, Any] | Key,
1010
algorithm: str = ...,
1111
headers: Mapping[str, Any] | None = ...,
1212
access_token: str | None = ...,
1313
) -> str: ...
1414
def decode(
15-
token: str,
16-
key: str | Mapping[str, Any] | Key,
15+
token: str | bytes,
16+
key: str | bytes | Mapping[str, Any] | Key,
1717
algorithms: str | Container[str] | None = ...,
1818
options: Mapping[str, Any] | None = ...,
1919
audience: str | None = ...,

stubs/python-jose/jose/utils.pyi

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
def long_to_bytes(n, blocksize: int = ...): ...
2-
def long_to_base64(data, size: int = ...): ...
3-
def int_arr_to_long(arr): ...
4-
def base64_to_long(data): ...
5-
def calculate_at_hash(access_token, hash_alg): ...
6-
def base64url_decode(input): ...
7-
def base64url_encode(input): ...
8-
def timedelta_total_seconds(delta): ...
9-
def ensure_binary(s): ...
1+
from collections.abc import Callable, Iterable
2+
from datetime import timedelta
3+
from hashlib import _Hash
4+
from typing import Any
5+
6+
def long_to_bytes(n: int, blocksize: int | None = ...) -> bytes: ...
7+
def long_to_base64(data: int, size: int | None = ...) -> bytes: ...
8+
def int_arr_to_long(arr: Iterable[Any]) -> int: ...
9+
def base64_to_long(data: str | bytes) -> int: ...
10+
def calculate_at_hash(access_token: str, hash_alg: Callable[[bytes], _Hash]) -> str: ...
11+
def base64url_decode(input: bytes) -> bytes: ...
12+
def base64url_encode(input: bytes) -> bytes: ...
13+
def timedelta_total_seconds(delta: timedelta) -> int: ...
14+
def ensure_binary(s: str | bytes) -> bytes: ...

0 commit comments

Comments
 (0)