Improve type hints (#1401)

* Add type parameters to `AuthenticationBackend`

* add more type-hints
This commit is contained in:
Alexander Zinov
2024-07-14 17:04:13 +04:00
committed by GitHub
parent 6ac64ad293
commit abfa9a1c47
8 changed files with 25 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
import re
from inspect import Parameter, Signature
from typing import Callable, List, Optional, Sequence, Tuple, cast
from typing import Any, Callable, Generic, List, Optional, Sequence, Tuple, cast
from fastapi import Depends, HTTPException, status
from makefun import with_signature
@@ -31,10 +31,10 @@ class DuplicateBackendNamesError(Exception):
pass
EnabledBackendsDependency = DependencyCallable[Sequence[AuthenticationBackend]]
EnabledBackendsDependency = DependencyCallable[Sequence[AuthenticationBackend[models.UP, models.ID]]]
class Authenticator:
class Authenticator(Generic[models.UP, models.ID]):
"""
Provides dependency callables to retrieve authenticated user.
@@ -46,11 +46,11 @@ class Authenticator:
:param get_user_manager: User manager dependency callable.
"""
backends: Sequence[AuthenticationBackend]
backends: Sequence[AuthenticationBackend[models.UP, models.ID]]
def __init__(
self,
backends: Sequence[AuthenticationBackend],
backends: Sequence[AuthenticationBackend[models.UP, models.ID]],
get_user_manager: UserManagerDependency[models.UP, models.ID],
):
self.backends = backends
@@ -62,7 +62,7 @@ class Authenticator:
active: bool = False,
verified: bool = False,
superuser: bool = False,
get_enabled_backends: Optional[EnabledBackendsDependency] = None,
get_enabled_backends: Optional[EnabledBackendsDependency[models.UP, models.ID]] = None,
):
"""
Return a dependency callable to retrieve currently authenticated user and token.
@@ -88,7 +88,7 @@ class Authenticator:
signature = self._get_dependency_signature(get_enabled_backends)
@with_signature(signature)
async def current_user_token_dependency(*args, **kwargs):
async def current_user_token_dependency(*args: Any, **kwargs: Any):
return await self._authenticate(
*args,
optional=optional,
@@ -106,7 +106,7 @@ class Authenticator:
active: bool = False,
verified: bool = False,
superuser: bool = False,
get_enabled_backends: Optional[EnabledBackendsDependency] = None,
get_enabled_backends: Optional[EnabledBackendsDependency[models.UP, models.ID]] = None,
):
"""
Return a dependency callable to retrieve currently authenticated user.
@@ -132,7 +132,7 @@ class Authenticator:
signature = self._get_dependency_signature(get_enabled_backends)
@with_signature(signature)
async def current_user_dependency(*args, **kwargs):
async def current_user_dependency(*args: Any, **kwargs: Any):
user, _ = await self._authenticate(
*args,
optional=optional,
@@ -157,7 +157,7 @@ class Authenticator:
) -> Tuple[Optional[models.UP], Optional[str]]:
user: Optional[models.UP] = None
token: Optional[str] = None
enabled_backends: Sequence[AuthenticationBackend] = kwargs.get(
enabled_backends: Sequence[AuthenticationBackend[models.UP, models.ID]] = kwargs.get(
"enabled_backends", self.backends
)
for backend in self.backends:

View File

@@ -35,12 +35,12 @@ class FastAPIUsers(Generic[models.UP, models.ID]):
with a specific set of parameters.
"""
authenticator: Authenticator
authenticator: Authenticator[models.UP, models.ID]
def __init__(
self,
get_user_manager: UserManagerDependency[models.UP, models.ID],
auth_backends: Sequence[AuthenticationBackend],
auth_backends: Sequence[AuthenticationBackend[models.UP, models.ID]],
):
self.authenticator = Authenticator(auth_backends, get_user_manager)
self.get_user_manager = get_user_manager
@@ -72,7 +72,7 @@ class FastAPIUsers(Generic[models.UP, models.ID]):
return get_reset_password_router(self.get_user_manager)
def get_auth_router(
self, backend: AuthenticationBackend, requires_verification: bool = False
self, backend: AuthenticationBackend[models.UP, models.ID], requires_verification: bool = False
) -> APIRouter:
"""
Return an auth router for a given authentication backend.
@@ -91,7 +91,7 @@ class FastAPIUsers(Generic[models.UP, models.ID]):
def get_oauth_router(
self,
oauth_client: BaseOAuth2,
backend: AuthenticationBackend,
backend: AuthenticationBackend[models.UP, models.ID],
state_secret: SecretType,
redirect_url: Optional[str] = None,
associate_by_email: bool = False,

View File

@@ -11,9 +11,9 @@ from fastapi_users.router.common import ErrorCode, ErrorModel
def get_auth_router(
backend: AuthenticationBackend,
backend: AuthenticationBackend[models.UP, models.ID],
get_user_manager: UserManagerDependency[models.UP, models.ID],
authenticator: Authenticator,
authenticator: Authenticator[models.UP, models.ID],
requires_verification: bool = False,
) -> APIRouter:
"""Generate a router with login/logout routes for an authentication backend."""

View File

@@ -29,7 +29,7 @@ def generate_state_token(
def get_oauth_router(
oauth_client: BaseOAuth2,
backend: AuthenticationBackend,
backend: AuthenticationBackend[models.UP, models.ID],
get_user_manager: UserManagerDependency[models.UP, models.ID],
state_secret: SecretType,
redirect_url: Optional[str] = None,
@@ -156,7 +156,7 @@ def get_oauth_router(
def get_oauth_associate_router(
oauth_client: BaseOAuth2,
authenticator: Authenticator,
authenticator: Authenticator[models.UP, models.ID],
get_user_manager: UserManagerDependency[models.UP, models.ID],
user_schema: Type[schemas.U],
state_secret: SecretType,

View File

@@ -12,7 +12,7 @@ def get_users_router(
get_user_manager: UserManagerDependency[models.UP, models.ID],
user_schema: Type[schemas.U],
user_update_schema: Type[schemas.UU],
authenticator: Authenticator,
authenticator: Authenticator[models.UP, models.ID],
requires_verification: bool = False,
) -> APIRouter:
"""Generate a router with the authentication routes."""