Improve dependencies typing

This commit is contained in:
François Voron
2022-01-15 11:08:49 +01:00
parent c43303c386
commit 533504634f
7 changed files with 24 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ from fastapi_users import models
from fastapi_users.authentication.backend import AuthenticationBackend
from fastapi_users.authentication.strategy import Strategy
from fastapi_users.manager import BaseUserManager, UserManagerDependency
from fastapi_users.types import DependencyCallable
INVALID_CHARS_PATTERN = re.compile(r"[^0-9a-zA-Z_]")
INVALID_LEADING_CHARS_PATTERN = re.compile(r"^[^a-zA-Z_]+")
@@ -30,7 +31,7 @@ class DuplicateBackendNamesError(Exception):
pass
EnabledBackendsDependency = Callable[..., Sequence[AuthenticationBackend]]
EnabledBackendsDependency = DependencyCallable[Sequence[AuthenticationBackend]]
class Authenticator:

View File

@@ -1,4 +1,4 @@
from typing import Any, Callable, Generic
from typing import Any, Generic
from fastapi import Response
@@ -11,6 +11,7 @@ from fastapi_users.authentication.transport import (
Transport,
TransportLogoutNotSupportedError,
)
from fastapi_users.types import DependencyCallable
class AuthenticationBackend(Generic[models.UC, models.UD]):
@@ -32,7 +33,7 @@ class AuthenticationBackend(Generic[models.UC, models.UD]):
self,
name: str,
transport: Transport,
get_strategy: Callable[..., Strategy[models.UC, models.UD]],
get_strategy: DependencyCallable[Strategy[models.UC, models.UD]],
):
self.name = name
self.transport = transport

View File

@@ -1,8 +1,9 @@
from typing import Callable, Generic, Optional, Type
from typing import Generic, Optional, Type
from pydantic import UUID4
from fastapi_users.models import UD
from fastapi_users.types import DependencyCallable
class BaseUserDatabase(Generic[UD]):
@@ -42,4 +43,4 @@ class BaseUserDatabase(Generic[UD]):
raise NotImplementedError()
UserDatabaseDependency = Callable[..., BaseUserDatabase[UD]]
UserDatabaseDependency = DependencyCallable[BaseUserDatabase[UD]]

View File

@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, Generic, Optional, Type, Union
from typing import Any, Dict, Generic, Optional, Type, Union
import jwt
from fastapi import Request
@@ -9,6 +9,7 @@ from fastapi_users import models, password
from fastapi_users.db import BaseUserDatabase
from fastapi_users.jwt import SecretType, decode_jwt, generate_jwt
from fastapi_users.password import generate_password, get_password_hash
from fastapi_users.types import DependencyCallable
RESET_PASSWORD_TOKEN_AUDIENCE = "fastapi-users:reset"
VERIFY_USER_TOKEN_AUDIENCE = "fastapi-users:verify"
@@ -555,4 +556,4 @@ class BaseUserManager(Generic[models.UC, models.UD]):
return await self.user_db.update(user)
UserManagerDependency = Callable[..., BaseUserManager[models.UC, models.UD]]
UserManagerDependency = DependencyCallable[BaseUserManager[models.UC, models.UD]]

7
fastapi_users/types.py Normal file
View File

@@ -0,0 +1,7 @@
from typing import Callable, Coroutine, TypeVar, Union
RETURN_TYPE = TypeVar("RETURN_TYPE")
DependencyCallable = Callable[
..., Union[RETURN_TYPE, Coroutine[None, None, RETURN_TYPE]]
]

View File

@@ -1,4 +1,4 @@
from typing import AsyncGenerator, Callable, Generic, List, Optional, Sequence
from typing import AsyncGenerator, Generic, List, Optional, Sequence
import httpx
import pytest
@@ -11,6 +11,7 @@ from fastapi_users.authentication.authenticator import DuplicateBackendNamesErro
from fastapi_users.authentication.strategy import Strategy
from fastapi_users.authentication.transport import Transport
from fastapi_users.manager import BaseUserManager
from fastapi_users.types import DependencyCallable
from tests.conftest import UserDB
@@ -71,7 +72,7 @@ def get_test_auth_client(get_user_manager, get_test_client):
async def _get_test_auth_client(
backends: List[AuthenticationBackend],
get_enabled_backends: Optional[
Callable[..., Sequence[AuthenticationBackend]]
DependencyCallable[Sequence[AuthenticationBackend]]
] = None,
) -> AsyncGenerator[httpx.AsyncClient, None]:
app = FastAPI()

View File

@@ -1,4 +1,4 @@
from typing import Callable, Generic, Optional, Type
from typing import Callable, Generic, Optional, Type, cast
import pytest
from fastapi import Response
@@ -56,5 +56,6 @@ def backend(
@pytest.mark.asyncio
@pytest.mark.authentication
async def test_logout(backend: AuthenticationBackend, user: UserDB):
result = await backend.logout(backend.get_strategy(), user, "TOKEN", Response())
strategy = cast(Strategy, backend.get_strategy())
result = await backend.logout(strategy, user, "TOKEN", Response())
assert result is None