Skip to content

Commit 099ec02

Browse files
committed
Add the missing typehints
1 parent d013953 commit 099ec02

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

src/fastapi_oauth2/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
client_secret: str,
2323
redirect_uri: Optional[str] = None,
2424
scope: Optional[Sequence[str]] = None,
25-
):
25+
) -> None:
2626
self.backend = backend
2727
self.client_id = client_id
2828
self.client_secret = client_secret

src/fastapi_oauth2/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
jwt_expires: Union[int, str] = 900,
2323
jwt_algorithm: str = "HS256",
2424
clients: List[OAuth2Client] = None,
25-
):
25+
) -> None:
2626
if allow_http:
2727
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
2828
self.allow_http = allow_http

src/fastapi_oauth2/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ class OAuth2LoginError(HTTPException):
2626
class OAuth2Strategy(BaseStrategy):
2727
"""Dummy strategy for using the `BaseOAuth2.user_data` method."""
2828

29-
def request_data(self, merge=True):
29+
def request_data(self, merge=True) -> Dict[str, Any]:
3030
return {}
3131

32-
def absolute_uri(self, path=None):
32+
def absolute_uri(self, path=None) -> str:
3333
return path
3434

35-
def get_setting(self, name):
36-
return None
35+
def get_setting(self, name) -> Any:
36+
"""Mocked setting method."""
3737

3838
@staticmethod
39-
def get_json(url, method='GET', *args, **kwargs):
39+
def get_json(url, method='GET', *args, **kwargs) -> httpx.Response:
4040
return httpx.request(method, url, *args, **kwargs)
4141

4242

src/fastapi_oauth2/middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(
104104
Auth.register_client(client)
105105
self.callback = callback
106106

107-
async def authenticate(self, request: Request) -> Optional[Tuple["Auth", "User"]]:
107+
async def authenticate(self, request: Request) -> Optional[Tuple[Auth, User]]:
108108
authorization = request.headers.get(
109109
"Authorization",
110110
request.cookies.get("Authorization"),

src/fastapi_oauth2/security.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
from typing import Callable
2+
from typing import Optional
3+
from typing import Type
4+
15
from fastapi.security import OAuth2 as FastAPIOAuth2
26
from fastapi.security import OAuth2AuthorizationCodeBearer as FastAPICodeBearer
37
from fastapi.security import OAuth2PasswordBearer as FastAPIPasswordBearer
48
from starlette.datastructures import Headers
59
from starlette.requests import Request
610

711

8-
def use_cookie(cls: FastAPIOAuth2):
9-
def _use_cookie(*args, **kwargs):
10-
async def __call__(self, request: Request):
12+
def use_cookies(cls: Type[FastAPIOAuth2]) -> Callable[[...], FastAPIOAuth2]:
13+
"""OAuth2 classes wrapped with this decorator will use cookies for the Authorization header."""
14+
15+
def _use_cookies(*args, **kwargs) -> FastAPIOAuth2:
16+
async def __call__(self: FastAPIOAuth2, request: Request) -> Optional[str]:
1117
authorization = request.headers.get("Authorization", request.cookies.get("Authorization"))
1218
if authorization:
1319
request._headers = Headers({**request.headers, "Authorization": authorization})
@@ -16,19 +22,19 @@ async def __call__(self, request: Request):
1622
cls.__call__ = __call__
1723
return cls(*args, **kwargs)
1824

19-
return _use_cookie
25+
return _use_cookies
2026

2127

22-
@use_cookie
28+
@use_cookies
2329
class OAuth2(FastAPIOAuth2):
24-
...
30+
"""Wrapper class of the `fastapi.security.OAuth2` class."""
2531

2632

27-
@use_cookie
33+
@use_cookies
2834
class OAuth2PasswordBearer(FastAPIPasswordBearer):
29-
...
35+
"""Wrapper class of the `fastapi.security.OAuth2PasswordBearer` class."""
3036

3137

32-
@use_cookie
38+
@use_cookies
3339
class OAuth2AuthorizationCodeBearer(FastAPICodeBearer):
34-
...
40+
"""Wrapper class of the `fastapi.security.OAuth2AuthorizationCodeBearer` class."""

0 commit comments

Comments
 (0)