Skip to content

Commit d9687da

Browse files
authored
[Authlib] Various improvements (#15865)
- Add type annotations to method arguments and return types. - Use variable annotations with `ClassVar` instead of assignments for class variables. - Use type vars to ensure the same input and output types for `OAuth2Auth.__call__` and `OAuth2ClientAuth.__call__`.
1 parent bfb8238 commit d9687da

4 files changed

Lines changed: 43 additions & 35 deletions

File tree

‎stubs/Authlib/METADATA.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ version = "1.8.0"
22
upstream-repository = "https://github.com/authlib/authlib"
33
dependencies = ["cryptography"]
44
# Requires a version of flask with a `py.typed` file
5-
optional-dependencies = ["requests", "httpx2", "Flask>=2.0.0"]
5+
optional-dependencies = ["Flask>=2.0.0", "httpx2", "requests", "types-oauthlib"]

‎stubs/Authlib/authlib/integrations/httpx_client/oauth2_client.pyi‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections.abc import Generator
2+
from typing import ClassVar
23
from typing_extensions import Never
34

45
import httpx2
@@ -23,10 +24,10 @@ class OAuth2ClientAuth(Auth, ClientAuth):
2324
def auth_flow(self, request: Request) -> Generator[Request, Response]: ...
2425

2526
class AsyncOAuth2Client(_OAuth2Client, httpx2.AsyncClient):
26-
SESSION_REQUEST_PARAMS: list[str]
27-
client_auth_class = OAuth2ClientAuth
28-
token_auth_class = OAuth2Auth
29-
oauth_error_class = OAuthError # type: ignore[assignment]
27+
SESSION_REQUEST_PARAMS: ClassVar[list[str]]
28+
client_auth_class: ClassVar[type[OAuth2ClientAuth]]
29+
token_auth_class: ClassVar[type[OAuth2Auth]]
30+
oauth_error_class: ClassVar[type[OAuthError]]
3031
def __init__(
3132
self,
3233
client_id=None,
@@ -46,10 +47,10 @@ class AsyncOAuth2Client(_OAuth2Client, httpx2.AsyncClient):
4647
async def ensure_active_token(self, token): ... # type: ignore[override]
4748

4849
class OAuth2Client(_OAuth2Client, httpx2.Client):
49-
SESSION_REQUEST_PARAMS: list[str]
50-
client_auth_class = OAuth2ClientAuth
51-
token_auth_class = OAuth2Auth
52-
oauth_error_class = OAuthError # type: ignore[assignment]
50+
SESSION_REQUEST_PARAMS: ClassVar[list[str]]
51+
client_auth_class: ClassVar[type[OAuth2ClientAuth]]
52+
token_auth_class: ClassVar[type[OAuth2Auth]]
53+
oauth_error_class: ClassVar[type[OAuthError]]
5354
def __init__(
5455
self,
5556
client_id=None,

‎stubs/Authlib/authlib/integrations/requests_client/oauth2_session.pyi‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from _typeshed import Incomplete
1+
from collections.abc import Callable
22
from http.cookiejar import CookieJar
3+
from typing import Any, ClassVar, TypeVar
34

45
from authlib.oauth2.auth import ClientAuth, TokenAuth
56
from authlib.oauth2.client import OAuth2Client
7+
from oauthlib.oauth2 import OAuth2Token
68
from requests import PreparedRequest, Response, Session
79
from requests._types import (
810
AuthType,
@@ -24,36 +26,38 @@ from ..base_client import OAuthError
2426

2527
__all__ = ["OAuth2Session", "OAuth2Auth"]
2628

29+
_PR = TypeVar("_PR", bound=PreparedRequest)
30+
2731
class OAuth2Auth(AuthBase, TokenAuth):
2832
def ensure_active_token(self) -> None: ...
29-
def __call__(self, req: PreparedRequest) -> PreparedRequest: ...
33+
def __call__(self, req: _PR) -> _PR: ...
3034

3135
class OAuth2ClientAuth(AuthBase, ClientAuth):
32-
def __call__(self, req: PreparedRequest) -> PreparedRequest: ...
36+
def __call__(self, req: _PR) -> _PR: ...
3337

3438
class OAuth2Session(OAuth2Client, Session):
35-
client_auth_class = OAuth2ClientAuth
36-
token_auth_class = OAuth2Auth
37-
oauth_error_class = OAuthError # type: ignore[assignment]
38-
SESSION_REQUEST_PARAMS: tuple[str, ...] # type: ignore[assignment]
39-
default_timeout: Incomplete
39+
client_auth_class: ClassVar[type[OAuth2ClientAuth]]
40+
token_auth_class: ClassVar[type[OAuth2Auth]]
41+
oauth_error_class: ClassVar[type[OAuthError]]
42+
SESSION_REQUEST_PARAMS: ClassVar[tuple[str, ...]]
43+
default_timeout: float | tuple[float | None, float | None] | None
4044
def __init__(
4145
self,
42-
client_id=None,
43-
client_secret=None,
44-
token_endpoint_auth_method=None,
45-
revocation_endpoint_auth_method=None,
46-
scope=None,
47-
state=None,
48-
redirect_uri=None,
49-
token=None,
50-
token_placement="header",
51-
update_token=None,
52-
leeway=60,
53-
default_timeout=None,
46+
client_id: str | None = None,
47+
client_secret: str | None = None,
48+
token_endpoint_auth_method: str | None = None,
49+
revocation_endpoint_auth_method: str | None = None,
50+
scope: str | None = None,
51+
state: str | None = None,
52+
redirect_uri: str | None = None,
53+
token: dict[str, Any] | None = None,
54+
token_placement: str = ...,
55+
update_token: Callable[[OAuth2Token], None] | None = None,
56+
leeway: int = 60,
57+
default_timeout: float | tuple[float | None, float | None] | None = None,
5458
**kwargs,
5559
) -> None: ...
56-
def fetch_access_token(self, url=None, **kwargs): ...
60+
def fetch_access_token(self, url: str | None = None, **kwargs) -> OAuth2Token: ...
5761
def request( # type: ignore[override]
5862
self,
5963
method: str,

‎stubs/Authlib/authlib/oauth2/client.pyi‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from _typeshed import Incomplete
2+
from collections.abc import Sequence
3+
from typing import ClassVar
24

3-
from authlib.oauth2 import ClientAuth, OAuth2Error, TokenAuth
5+
from authlib.common.errors import AuthlibBaseError
6+
from authlib.oauth2 import ClientAuth, TokenAuth
47

58
DEFAULT_HEADERS: Incomplete
69

710
class OAuth2Client:
8-
client_auth_class = ClientAuth
9-
token_auth_class = TokenAuth
10-
oauth_error_class = OAuth2Error
11+
client_auth_class: ClassVar[type[ClientAuth]]
12+
token_auth_class: ClassVar[type[TokenAuth]]
13+
oauth_error_class: ClassVar[type[AuthlibBaseError]]
1114
EXTRA_AUTHORIZE_PARAMS: tuple[str, ...]
12-
SESSION_REQUEST_PARAMS: list[str]
15+
SESSION_REQUEST_PARAMS: ClassVar[Sequence[str]]
1316
session: Incomplete
1417
client_id: Incomplete
1518
client_secret: Incomplete

0 commit comments

Comments
 (0)