diff options
author | 2020-12-15 08:13:37 +0300 | |
---|---|---|
committer | 2020-12-15 08:13:37 +0300 | |
commit | 947a30f7406b7d025cf2e5754b59389af4213718 (patch) | |
tree | c6236edb812db32f583ac0acc9099be33d1a0e2b | |
parent | Add ks123 (GH ks129) to CODEOWNERS (diff) |
fix various type annotation issues
-rw-r--r-- | backend/authentication/backend.py | 4 | ||||
-rw-r--r-- | backend/authentication/user.py | 2 | ||||
-rw-r--r-- | backend/models/form.py | 8 | ||||
-rw-r--r-- | backend/models/form_response.py | 2 | ||||
-rw-r--r-- | backend/models/question.py | 8 | ||||
-rw-r--r-- | backend/route.py | 10 | ||||
-rw-r--r-- | backend/route_manager.py | 6 |
7 files changed, 20 insertions, 20 deletions
diff --git a/backend/authentication/backend.py b/backend/authentication/backend.py index 38668eb..e4699bd 100644 --- a/backend/authentication/backend.py +++ b/backend/authentication/backend.py @@ -14,7 +14,7 @@ class JWTAuthenticationBackend(authentication.AuthenticationBackend, ABC): """Custom Starlette authentication backend for JWT.""" @staticmethod - def get_token_from_header(header: str) -> t.Optional[str]: + def get_token_from_header(header: str) -> str: """Parse JWT token from header value.""" try: prefix, token = header.split() @@ -32,7 +32,7 @@ class JWTAuthenticationBackend(authentication.AuthenticationBackend, ABC): async def authenticate( self, request: Request - ) -> t.Optional[t.Tuple[authentication.AuthCredentials, authentication.BaseUser]]: + ) -> t.Optional[tuple[authentication.AuthCredentials, authentication.BaseUser]]: """Handles JWT authentication process.""" if "Authorization" not in request.headers: return diff --git a/backend/authentication/user.py b/backend/authentication/user.py index afa243f..3bed0a1 100644 --- a/backend/authentication/user.py +++ b/backend/authentication/user.py @@ -7,7 +7,7 @@ from starlette.authentication import BaseUser class User(BaseUser, ABC): """Starlette BaseUser implementation for JWT authentication.""" - def __init__(self, token: str, payload: t.Dict) -> None: + def __init__(self, token: str, payload: dict[str, t.Any]) -> None: self.token = token self.payload = payload diff --git a/backend/models/form.py b/backend/models/form.py index 2cf8486..21cc549 100644 --- a/backend/models/form.py +++ b/backend/models/form.py @@ -12,8 +12,8 @@ class Form(BaseModel): """Schema model for form.""" id: str = Field(alias="_id") - features: t.List[str] - questions: t.List[Question] + features: list[str] + questions: list[Question] name: str description: str @@ -21,7 +21,7 @@ class Form(BaseModel): allow_population_by_field_name = True @validator("features") - def validate_features(cls, value: t.List[str]) -> t.Optional[t.List[str]]: + def validate_features(cls, value: list[str]) -> t.Optional[list[str]]: """Validates is all features in allowed list.""" # Uppercase everything to avoid mixed case in DB value = [v.upper() for v in value] @@ -34,7 +34,7 @@ class Form(BaseModel): return value - def dict(self, admin: bool = True, **kwargs: t.Dict) -> t.Dict[str, t.Any]: + def dict(self, admin: bool = True, **kwargs: t.Any) -> dict[str, t.Any]: """Wrapper for original function to exclude private data for public access.""" data = super().dict(**kwargs) diff --git a/backend/models/form_response.py b/backend/models/form_response.py index bea070f..f3296cd 100644 --- a/backend/models/form_response.py +++ b/backend/models/form_response.py @@ -12,7 +12,7 @@ class FormResponse(BaseModel): id: str = Field(alias="_id") user: t.Optional[DiscordUser] antispam: t.Optional[AntiSpam] - response: t.Dict[str, t.Any] + response: dict[str, t.Any] form_id: str class Config: diff --git a/backend/models/question.py b/backend/models/question.py index 1a012ff..3b98024 100644 --- a/backend/models/question.py +++ b/backend/models/question.py @@ -11,7 +11,7 @@ class Question(BaseModel): id: str = Field(alias="_id") name: str type: str - data: t.Dict[str, t.Any] + data: dict[str, t.Any] class Config: allow_population_by_field_name = True @@ -31,14 +31,14 @@ class Question(BaseModel): @root_validator def validate_question_data( cls, - value: t.Dict[str, t.Any] - ) -> t.Optional[t.Dict[str, t.Any]]: + value: dict[str, t.Any] + ) -> t.Optional[dict[str, t.Any]]: """Check does required data exists for question type and remove other data.""" # When question type don't need data, don't add anything to keep DB clean. if value.get("type") not in REQUIRED_QUESTION_TYPE_DATA: return value - for key, data_type in REQUIRED_QUESTION_TYPE_DATA[value.get("type")].items(): + for key, data_type in REQUIRED_QUESTION_TYPE_DATA[value["type"]].items(): if key not in value.get("data", {}): raise ValueError(f"Required question data key '{key}' not provided.") diff --git a/backend/route.py b/backend/route.py index eb69ebc..d8c38fc 100644 --- a/backend/route.py +++ b/backend/route.py @@ -5,13 +5,13 @@ from starlette.endpoints import HTTPEndpoint class Route(HTTPEndpoint): - name: str = None - path: str = None + name: str + path: str @classmethod - def check_parameters(cls) -> "Route": - if cls.name is None: + def check_parameters(cls): + if not hasattr(cls, "name"): raise ValueError(f"Route {cls.__name__} has not defined a name") - if cls.path is None: + if not hasattr(cls, "path"): raise ValueError(f"Route {cls.__name__} has not defined a path") diff --git a/backend/route_manager.py b/backend/route_manager.py index 25529eb..7427298 100644 --- a/backend/route_manager.py +++ b/backend/route_manager.py @@ -6,13 +6,13 @@ import importlib import inspect from pathlib import Path -from starlette.routing import Route as StarletteRoute, Mount +from starlette.routing import Route as StarletteRoute, BaseRoute, Mount from nested_dict import nested_dict from backend.route import Route -def construct_route_map_from_dict(route_dict: dict) -> list: +def construct_route_map_from_dict(route_dict: dict) -> list[BaseRoute]: route_map = [] for mount, item in route_dict.items(): if inspect.isclass(item): @@ -26,7 +26,7 @@ def construct_route_map_from_dict(route_dict: dict) -> list: return route_map -def create_route_map() -> list: +def create_route_map() -> list[BaseRoute]: routes_directory = Path("backend") / "routes" route_dict = nested_dict() |