Skip to content

Commit ffb8b0b

Browse files
authored
Improve stubs for oauthlib.oauth2.rfc6749 (#13752)
1 parent 54e1817 commit ffb8b0b

11 files changed

Lines changed: 257 additions & 140 deletions

File tree

stubs/oauthlib/oauthlib/oauth2/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ from .rfc6749.grant_types import (
5757
from .rfc6749.request_validator import RequestValidator as RequestValidator
5858
from .rfc6749.tokens import BearerToken as BearerToken, OAuth2Token as OAuth2Token
5959
from .rfc6749.utils import is_secure_transport as is_secure_transport
60+
from .rfc8628.clients import DeviceClient as DeviceClient
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from logging import Logger
22

33
from .endpoints.base import BaseEndpoint as BaseEndpoint, catch_errors_and_unavailability as catch_errors_and_unavailability
44
from .errors import (
@@ -8,4 +8,4 @@ from .errors import (
88
TemporarilyUnavailableError as TemporarilyUnavailableError,
99
)
1010

11-
log: Any
11+
log: Logger
Lines changed: 85 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,122 @@
1-
from _typeshed import Incomplete
2-
from typing import Any
1+
from _typeshed import ConvertibleToInt, Incomplete
2+
from collections.abc import Callable
3+
from typing import Final, Literal
4+
from typing_extensions import TypeAlias
35

4-
AUTH_HEADER: str
5-
URI_QUERY: str
6-
BODY: str
7-
FORM_ENC_HEADERS: Any
6+
from oauthlib.common import _HTTPMethod
7+
from oauthlib.oauth2.rfc6749.tokens import OAuth2Token
8+
9+
_TokenPlacement: TypeAlias = Literal["auth_header", "query", "body"]
10+
11+
AUTH_HEADER: Final[_TokenPlacement]
12+
URI_QUERY: Final[_TokenPlacement]
13+
BODY: Final[_TokenPlacement]
14+
FORM_ENC_HEADERS: Final[dict[str, str]]
815

916
class Client:
1017
refresh_token_key: str
11-
client_id: Any
12-
default_token_placement: Any
13-
token_type: Any
14-
access_token: Any
15-
refresh_token: Any
16-
mac_key: Any
17-
mac_algorithm: Any
18-
token: Any
19-
scope: Any
20-
state_generator: Any
21-
state: Any
22-
redirect_url: Any
23-
code: Any
24-
expires_in: Any
25-
code_verifier: str
26-
code_challenge: str
27-
code_challenge_method: str
18+
client_id: str
19+
default_token_placement: _TokenPlacement
20+
token_type: str
21+
access_token: str | None
22+
refresh_token: str | None
23+
mac_key: str | bytes | bytearray | None
24+
mac_algorithm: str | None
25+
token: dict[str, Incomplete]
26+
scope: str | set[object] | tuple[object] | list[object]
27+
state_generator: Callable[[], str]
28+
state: str | None
29+
redirect_url: str | None
30+
code: Incomplete
31+
expires_in: ConvertibleToInt | None
32+
code_verifier: str | None
33+
code_challenge: str | None
34+
code_challenge_method: str | None
2835
def __init__(
2936
self,
30-
client_id,
31-
default_token_placement="auth_header",
37+
client_id: str,
38+
default_token_placement: _TokenPlacement = "auth_header",
3239
token_type: str = "Bearer",
33-
access_token: Incomplete | None = None,
34-
refresh_token: Incomplete | None = None,
35-
mac_key: Incomplete | None = None,
36-
mac_algorithm: Incomplete | None = None,
37-
token: Incomplete | None = None,
38-
scope: Incomplete | None = None,
39-
state: Incomplete | None = None,
40-
redirect_url: Incomplete | None = None,
41-
state_generator=...,
40+
access_token: str | None = None,
41+
refresh_token: str | None = None,
42+
mac_key: str | bytes | bytearray | None = None,
43+
mac_algorithm: str | None = None,
44+
token: dict[str, Incomplete] | None = None,
45+
scope: str | set[object] | tuple[object] | list[object] | None = None,
46+
state: str | None = None,
47+
redirect_url: str | None = None,
48+
state_generator: Callable[[], str] = ...,
4249
code_verifier: str | None = None,
4350
code_challenge: str | None = None,
4451
code_challenge_method: str | None = None,
4552
**kwargs,
4653
) -> None: ...
4754
@property
48-
def token_types(self): ...
55+
def token_types(
56+
self,
57+
) -> dict[
58+
Literal["Bearer", "MAC"],
59+
Callable[
60+
[str, str, str | None, dict[str, str] | None, str | None, Incomplete], tuple[str, dict[str, str] | None, str | None]
61+
],
62+
]: ...
4963
def prepare_request_uri(self, *args, **kwargs) -> str: ...
5064
def prepare_request_body(self, *args, **kwargs) -> str: ...
5165
def parse_request_uri_response(self, *args, **kwargs) -> dict[str, str]: ...
5266
def add_token(
5367
self,
54-
uri,
55-
http_method: str = "GET",
56-
body: Incomplete | None = None,
57-
headers: Incomplete | None = None,
58-
token_placement: Incomplete | None = None,
68+
uri: str,
69+
http_method: _HTTPMethod = "GET",
70+
body: str | None = None,
71+
headers: dict[str, str] | None = None,
72+
token_placement: _TokenPlacement | None = None,
5973
**kwargs,
60-
): ...
74+
) -> tuple[str, dict[str, str] | None, str | None]: ...
6175
def prepare_authorization_request(
6276
self,
63-
authorization_url,
64-
state: Incomplete | None = None,
65-
redirect_url: Incomplete | None = None,
66-
scope: Incomplete | None = None,
77+
authorization_url: str,
78+
state: str | None = None,
79+
redirect_url: str | None = None,
80+
scope: str | set[object] | tuple[object] | list[object] | None = None,
6781
**kwargs,
68-
): ...
82+
) -> tuple[str, dict[str, str], str]: ...
6983
def prepare_token_request(
7084
self,
71-
token_url,
72-
authorization_response: Incomplete | None = None,
73-
redirect_url: Incomplete | None = None,
74-
state: Incomplete | None = None,
85+
token_url: str,
86+
authorization_response: str | None = None,
87+
redirect_url: str | None = None,
88+
state: str | None = None,
7589
body: str = "",
7690
**kwargs,
77-
): ...
91+
) -> tuple[str, dict[str, str], str]: ...
7892
def prepare_refresh_token_request(
79-
self, token_url, refresh_token: Incomplete | None = None, body: str = "", scope: Incomplete | None = None, **kwargs
80-
): ...
93+
self,
94+
token_url: str,
95+
refresh_token: str | None = None,
96+
body: str = "",
97+
scope: str | set[object] | tuple[object] | list[object] | None = None,
98+
**kwargs,
99+
) -> tuple[str, dict[str, str], str]: ...
81100
def prepare_token_revocation_request(
82101
self,
83102
revocation_url,
84103
token,
85-
token_type_hint: str = "access_token",
104+
token_type_hint: Literal["access_token", "refresh_token"] | None = "access_token",
86105
body: str = "",
87-
callback: Incomplete | None = None,
106+
callback: Callable[[Incomplete], Incomplete] | None = None,
88107
**kwargs,
89108
): ...
90-
def parse_request_body_response(self, body, scope: Incomplete | None = None, **kwargs): ...
109+
def parse_request_body_response(
110+
self, body: str, scope: str | set[object] | tuple[object] | list[object] | None = None, **kwargs
111+
) -> OAuth2Token: ...
91112
def prepare_refresh_body(
92-
self, body: str = "", refresh_token: Incomplete | None = None, scope: Incomplete | None = None, **kwargs
93-
): ...
113+
self,
114+
body: str = "",
115+
refresh_token: str | None = None,
116+
scope: str | set[object] | tuple[object] | list[object] | None = None,
117+
**kwargs,
118+
) -> str: ...
94119
def create_code_verifier(self, length: int) -> str: ...
95120
def create_code_challenge(self, code_verifier: str, code_challenge_method: str | None = None) -> str: ...
96-
def populate_code_attributes(self, response) -> None: ...
97-
def populate_token_attributes(self, response) -> None: ...
121+
def populate_code_attributes(self, response: dict[str, Incomplete]) -> None: ...
122+
def populate_token_attributes(self, response: dict[str, Incomplete]) -> None: ...

stubs/oauthlib/oauthlib/oauth2/rfc6749/errors.pyi

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from _typeshed import Incomplete
2-
from typing import Any
2+
from typing import Any, NoReturn
3+
4+
from oauthlib.common import Request
35

46
class OAuth2Error(Exception):
5-
error: Any
7+
error: str | None
68
status_code: int
79
description: str
8-
uri: Any
10+
uri: str | None
911
state: Any
1012
redirect_uri: Any
1113
client_id: Any
@@ -15,21 +17,21 @@ class OAuth2Error(Exception):
1517
grant_type: Any
1618
def __init__(
1719
self,
18-
description: Incomplete | None = None,
19-
uri: Incomplete | None = None,
20+
description: str | None = None,
21+
uri: str | None = None,
2022
state: Incomplete | None = None,
21-
status_code: Incomplete | None = None,
22-
request: Incomplete | None = None,
23+
status_code: int | None = None,
24+
request: Request | None = None,
2325
) -> None: ...
24-
def in_uri(self, uri): ...
26+
def in_uri(self, uri: str) -> str: ...
2527
@property
26-
def twotuples(self): ...
28+
def twotuples(self) -> list[tuple[str, Incomplete | str | None]]: ...
2729
@property
28-
def urlencoded(self): ...
30+
def urlencoded(self) -> str: ...
2931
@property
30-
def json(self): ...
32+
def json(self) -> str: ...
3133
@property
32-
def headers(self): ...
34+
def headers(self) -> dict[str, str]: ...
3335

3436
class TokenExpiredError(OAuth2Error):
3537
error: str
@@ -135,7 +137,14 @@ class LoginRequired(OAuth2Error):
135137
error: str
136138

137139
class CustomOAuth2Error(OAuth2Error):
138-
error: Any
139-
def __init__(self, error, *args, **kwargs) -> None: ...
140+
def __init__(
141+
self,
142+
error: str,
143+
description: str | None = None,
144+
uri: str | None = None,
145+
state: Incomplete | None = None,
146+
status_code: int | None = None,
147+
request: Request | None = None,
148+
) -> None: ...
140149

141-
def raise_from_error(error, params: Incomplete | None = None) -> None: ...
150+
def raise_from_error(error: str, params: dict[str, Incomplete] | None = None) -> NoReturn: ...
Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
from _typeshed import Incomplete
2+
from collections.abc import Callable
3+
from typing import Literal
4+
5+
from .tokens import OAuth2Token
26

37
def prepare_grant_uri(
4-
uri,
5-
client_id,
6-
response_type,
7-
redirect_uri: Incomplete | None = None,
8-
scope: Incomplete | None = None,
9-
state: Incomplete | None = None,
8+
uri: str,
9+
client_id: str,
10+
response_type: Literal["code", "token"],
11+
redirect_uri: str | None = None,
12+
scope: str | set[object] | tuple[object] | list[object] | None = None,
13+
state: str | None = None,
1014
code_challenge: str | None = None,
1115
code_challenge_method: str | None = "plain",
1216
**kwargs,
13-
): ...
17+
) -> str: ...
1418
def prepare_token_request(
15-
grant_type, body: str = "", include_client_id: bool = True, code_verifier: str | None = None, **kwargs
16-
): ...
19+
grant_type: str,
20+
body: str = "",
21+
include_client_id: bool = True,
22+
code_verifier: str | None = None,
23+
*,
24+
scope: str | set[object] | tuple[object] | list[object] | None = None,
25+
client_id: str | None = None,
26+
client_secret: str | None = None,
27+
**kwargs,
28+
) -> str: ...
1729
def prepare_token_revocation_request(
18-
url, token, token_type_hint: str = "access_token", callback: Incomplete | None = None, body: str = "", **kwargs
19-
): ...
20-
def parse_authorization_code_response(uri, state: Incomplete | None = None): ...
21-
def parse_implicit_response(uri, state: Incomplete | None = None, scope: Incomplete | None = None): ...
22-
def parse_token_response(body, scope: Incomplete | None = None): ...
23-
def validate_token_parameters(params) -> None: ...
30+
url: str,
31+
token: str,
32+
token_type_hint: Literal["access_token", "refresh_token"] | None = "access_token",
33+
callback: Callable[[Incomplete], Incomplete] | None = None,
34+
body: str = "",
35+
**kwargs,
36+
) -> tuple[str, dict[str, str], str]: ...
37+
def parse_authorization_code_response(uri: str, state: str | None = None) -> dict[str, str]: ...
38+
def parse_implicit_response(
39+
uri: str, state: str | None = None, scope: str | set[object] | tuple[object] | list[object] | None = None
40+
) -> OAuth2Token: ...
41+
def parse_token_response(
42+
body: str | bytes | bytearray, scope: str | set[object] | tuple[object] | list[object] | None = None
43+
) -> OAuth2Token: ...
44+
def validate_token_parameters(params: dict[str, Incomplete]) -> None: ...

stubs/oauthlib/oauthlib/oauth2/rfc6749/request_validator.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import Mapping
2-
from typing import Any, Literal, TypedDict
2+
from logging import Logger
3+
from typing import Literal, TypedDict
34
from typing_extensions import NotRequired
45

56
from oauthlib.common import Request
@@ -18,7 +19,7 @@ class _AuthorizationCode(TypedDict):
1819
state: NotRequired[str]
1920
nonce: NotRequired[str]
2021

21-
log: Any
22+
log: Logger
2223

2324
class RequestValidator:
2425
def client_authentication_required(self, request: Request, *args, **kwargs) -> bool: ...

0 commit comments

Comments
 (0)