Skip to content

Commit 9a550ef

Browse files
committed
fix: mypy errors
1 parent bd84c29 commit 9a550ef

39 files changed

+149
-106
lines changed

.github/workflows/mypy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
if [ -f requirements-dev.txt ]; then pip install -r requirements-dev.txt; fi
2727
- name: Test with Mypy
2828
run: |
29-
mypy .
29+
mypy --config-file backend/pyproject.toml backend/src
File renamed without changes.

backend/src/app/api/deps.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Module for dependency injection."""
22

3-
from typing import Annotated, Type
3+
from typing import Annotated
44
from uuid import UUID
55

66
import jwt
@@ -17,7 +17,7 @@
1717
from app.crud.user import UserCRUD
1818
from app.models import User
1919

20-
__all__: tuple[str, ...] = ("CurrentUser", "CurrentSuperuser", "ItemCRUDDep", "OptionalCurrentUser", "UserCRUDDep")
20+
__all__: tuple[str, ...] = ("CurrentUser", "CurrentSuperuser", "ItemCrudDep", "OptionalCurrentUser", "UserCrudDep")
2121

2222
# Basic settings
2323
settings: Settings = get_settings()
@@ -31,11 +31,11 @@
3131
reusable_oauth2_optional: OAuth2PasswordBearer = OAuth2PasswordBearer(tokenUrl=token_url, auto_error=False)
3232

3333
# Basic dependencies
34-
SessionDep: Type[AsyncSession] = Annotated[AsyncSession, Depends(db_manager.get_session)]
35-
SecurityManagerDep: Type[SecurityManager] = Annotated[SecurityManager, Depends(get_security_manager)]
34+
SessionDep = Annotated[AsyncSession, Depends(db_manager.get_session)]
35+
SecurityManagerDep = Annotated[SecurityManager, Depends(get_security_manager)]
3636
# Creating two versions of token dependency
37-
StrictTokenDep: Type[str] = Annotated[str, Depends(reusable_oauth2_strict)]
38-
OptionalTokenDep: Type[str | None] = Annotated[str | None, Depends(reusable_oauth2_optional)]
37+
StrictTokenDep = Annotated[str, Depends(reusable_oauth2_strict)]
38+
OptionalTokenDep = Annotated[str | None, Depends(reusable_oauth2_optional)]
3939

4040

4141
# Class dependencies for CRUD
@@ -64,16 +64,16 @@ def __call__(self, session: SessionDep) -> ItemCRUD:
6464
return ItemCRUD(session=session)
6565

6666

67-
UserCRUDDep: Type[UserCRUD] = Annotated[UserCRUD, Depends(UserCRUDProvider())]
68-
ItemCRUDDep: Type[ItemCRUD] = Annotated[ItemCRUD, Depends(ItemCRUDProvider())]
67+
UserCrudDep = Annotated[UserCRUD, Depends(UserCRUDProvider())]
68+
ItemCrudDep = Annotated[ItemCRUD, Depends(ItemCRUDProvider())]
6969

7070

7171
# Class dependencies for authentication
7272
class CurrentUserProvider:
7373
"""Class dependency for obtaining the current user (strict)."""
7474

7575
async def __call__(
76-
self, token: StrictTokenDep, user_crud: UserCRUDDep, security_manager: SecurityManagerDep
76+
self, token: StrictTokenDep, user_crud: UserCrudDep, security_manager: SecurityManagerDep
7777
) -> User:
7878
"""
7979
Retrieve the current user based on the provided JWT token.
@@ -101,7 +101,7 @@ class OptionalCurrentUserProvider:
101101
"""Class dependency for optionally obtaining the current user."""
102102

103103
async def __call__(
104-
self, token: OptionalTokenDep, user_crud: UserCRUDDep, security_manager: SecurityManagerDep
104+
self, token: OptionalTokenDep, user_crud: UserCrudDep, security_manager: SecurityManagerDep
105105
) -> User | None:
106106
"""
107107
Retrieve the current user based on the provided JWT token.
@@ -142,6 +142,6 @@ def __call__(self, current_user: Annotated[User, Depends(CurrentUserProvider())]
142142

143143

144144
# Final pseudonyms
145-
CurrentUser: Type[User] = Annotated[User, Depends(CurrentUserProvider())]
146-
CurrentSuperuser: Type[User] = Annotated[User, Depends(ActiveSuperuserProvider())]
147-
OptionalCurrentUser: Type[User | None] = Annotated[User | None, Depends(OptionalCurrentUserProvider())]
145+
CurrentUser = Annotated[User, Depends(CurrentUserProvider())]
146+
CurrentSuperuser = Annotated[User, Depends(ActiveSuperuserProvider())]
147+
OptionalCurrentUser = Annotated[User | None, Depends(OptionalCurrentUserProvider())]

backend/src/app/api/routes/items.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"""Module for items endpoints."""
22

3-
from typing import Annotated, Any, Type
3+
from typing import Annotated, Any
44
from uuid import UUID
55

66
from fastapi import APIRouter, HTTPException, Path, status
77
from fastapi_utils.cbv import cbv
88

9-
from app.api.deps import CurrentUser, ItemCRUDDep, OptionalCurrentUser
9+
from app.api.deps import CurrentUser, ItemCrudDep, OptionalCurrentUser
1010
from app.models import Item, ItemCreate, ItemPublic, ItemsPublic, ItemUpdate, Message
1111

1212
__all__: tuple[str] = ("items_router",)
1313

1414

15-
ItemIDDep: Type[UUID] = Annotated[UUID, Path(alias="id", description="ID элемента")]
15+
ItemIdDep = Annotated[UUID, Path(alias="id", description="Item ID")]
1616

1717
items_router: APIRouter = APIRouter()
1818

@@ -21,14 +21,14 @@
2121
class ItemsRouter:
2222
"""Class-based view for item endpoints."""
2323

24-
def __init__(self, item_crud: ItemCRUDDep, current_user: OptionalCurrentUser) -> None:
24+
def __init__(self, item_crud: ItemCrudDep, current_user: OptionalCurrentUser) -> None:
2525
"""
2626
Initializes the ItemsRouter class with the necessary dependencies.
2727
2828
:param item_crud: Dependency for item CRUD operations.
2929
:param current_user: Dependency for the current authorized user.
3030
"""
31-
self._item_crud: ItemCRUDDep = item_crud
31+
self._item_crud: ItemCrudDep = item_crud
3232
self._current_user: CurrentUser = current_user
3333

3434
async def _get_and_validate_item(self, item_id: UUID) -> Item:
@@ -95,7 +95,7 @@ async def create_item(self, *, item_in: ItemCreate) -> Item:
9595
@items_router.get(
9696
path="/{id}", response_model=ItemPublic, summary="Read Item by ID", description="Getting an item by ID."
9797
)
98-
async def read_item(self, item_id: ItemIDDep) -> Any:
98+
async def read_item(self, item_id: ItemIdDep) -> Any:
9999
"""
100100
Endpoint for retrieving an item by its ID.
101101
@@ -105,7 +105,7 @@ async def read_item(self, item_id: ItemIDDep) -> Any:
105105
return await self._get_and_validate_item(item_id=item_id)
106106

107107
@items_router.put(path="/{id}", response_model=ItemPublic, summary="Update Item", description="Item update.")
108-
async def update_item(self, *, item_id: ItemIDDep, item_in: ItemUpdate) -> Item:
108+
async def update_item(self, *, item_id: ItemIdDep, item_in: ItemUpdate) -> Item:
109109
"""
110110
Endpoint for updating an existing item.
111111
@@ -117,7 +117,7 @@ async def update_item(self, *, item_id: ItemIDDep, item_in: ItemUpdate) -> Item:
117117
return await self._item_crud.update(db_item=db_item, item_in=item_in)
118118

119119
@items_router.delete(path="/{id}", response_model=Message, summary="Delete Item", description="Deleting an Item.")
120-
async def delete_item(self, item_id: ItemIDDep) -> Message:
120+
async def delete_item(self, item_id: ItemIdDep) -> Message:
121121
"""
122122
Endpoint for deleting an item.
123123

backend/src/app/api/routes/login.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Module for login and authentication endpoints."""
22

33
from datetime import timedelta
4-
from typing import Annotated, Any, Type
4+
from typing import Annotated, Any
55

66
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
77
from fastapi.responses import HTMLResponse
88
from fastapi.security import OAuth2PasswordRequestForm
99
from fastapi_utils.cbv import cbv
1010

11-
from app.api.deps import CurrentUser, CurrentSuperuser, UserCRUDDep
11+
from app.api.deps import CurrentUser, CurrentSuperuser, UserCrudDep
1212
from app.core.config import get_settings, Settings
1313
from app.core.emails import EmailManager, get_email_manager
1414
from app.core.security import get_security_manager, SecurityManager
@@ -17,10 +17,10 @@
1717
__all__: tuple[str] = ("login_router",)
1818

1919

20-
SettingsDep: Type[Settings] = Annotated[Settings, Depends(get_settings)]
21-
EmailManagerDep: Type[EmailManager] = Annotated[EmailManager, Depends(get_email_manager)]
22-
SecurityManagerDep: Type[SecurityManager] = Annotated[SecurityManager, Depends(get_security_manager)]
23-
OAuthFormDep: Type[OAuth2PasswordRequestForm] = Annotated[OAuth2PasswordRequestForm, Depends()]
20+
SettingsDep = Annotated[Settings, Depends(get_settings)]
21+
EmailManagerDep = Annotated[EmailManager, Depends(get_email_manager)]
22+
SecurityManagerDep = Annotated[SecurityManager, Depends(get_security_manager)]
23+
OAuthFormDep = Annotated[OAuth2PasswordRequestForm, Depends()]
2424

2525
login_router: APIRouter = APIRouter()
2626

@@ -38,7 +38,7 @@ class LoginRouter:
3838
async def login_access_token(
3939
self,
4040
form_data: OAuthFormDep,
41-
user_crud: UserCRUDDep,
41+
user_crud: UserCrudDep,
4242
security_manager: SecurityManagerDep,
4343
settings: SettingsDep,
4444
) -> Token:
@@ -84,7 +84,7 @@ async def test_token(self, current_user: CurrentUser) -> Any:
8484
description="Send a password recovery email.",
8585
)
8686
async def recover_password(
87-
self, email: str, user_crud: UserCRUDDep, email_manager: EmailManagerDep, background_tasks: BackgroundTasks
87+
self, email: str, user_crud: UserCrudDep, email_manager: EmailManagerDep, background_tasks: BackgroundTasks
8888
) -> Message:
8989
"""
9090
Endpoint to initiate password recovery for a user.
@@ -109,7 +109,7 @@ async def recover_password(
109109
description="Reset user password using a recovery token.",
110110
)
111111
async def reset_password(
112-
self, body: NewPassword, user_crud: UserCRUDDep, email_manager: EmailManagerDep
112+
self, body: NewPassword, user_crud: UserCrudDep, email_manager: EmailManagerDep
113113
) -> Message:
114114
"""
115115
Endpoint to reset a user's password using a valid token.

backend/src/app/api/routes/private.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
"""Module for private endpoints."""
22

3-
from typing import Annotated, Type
3+
from typing import Annotated
44

55
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status
66
from fastapi_utils.cbv import cbv
77

8-
from app.api.deps import UserCRUDDep
8+
from app.api.deps import UserCrudDep
99
from app.core.config import get_settings, Settings
1010
from app.core.emails import EmailManager, get_email_manager
1111
from app.models import UserCreate, UserPublic, User
1212

1313
__all__: tuple[str] = ("private_router",)
1414

1515

16-
EmailManagerDep: Type[EmailManager] = Annotated[EmailManager, Depends(get_email_manager)]
17-
SettingsDep: Type[Settings] = Annotated[Settings, Depends(get_settings)]
16+
EmailManagerDep = Annotated[EmailManager, Depends(get_email_manager)]
17+
SettingsDep = Annotated[Settings, Depends(get_settings)]
1818

1919
private_router: APIRouter = APIRouter()
2020

@@ -23,15 +23,15 @@
2323
class PrivateRouter:
2424
"""Class-based view for private endpoints."""
2525

26-
def __init__(self, user_crud: UserCRUDDep, email_manager: EmailManagerDep, settings: SettingsDep) -> None:
26+
def __init__(self, user_crud: UserCrudDep, email_manager: EmailManagerDep, settings: SettingsDep) -> None:
2727
"""
2828
Initializes the PrivateRouter class with the necessary dependencies.
2929
3030
:param user_crud: Dependency for user CRUD operations.
3131
:param email_manager: Dependency for email management operations.
3232
:param settings: Dependency for application settings.
3333
"""
34-
self._user_crud: UserCRUDDep = user_crud
34+
self._user_crud: UserCrudDep = user_crud
3535
self._email_manager: EmailManagerDep = email_manager
3636
self._settings: SettingsDep = settings
3737

backend/src/app/api/routes/users.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Module for users endpoints."""
22

3-
from typing import Annotated, Any, Type
3+
from typing import Annotated, Any
44
from uuid import UUID
55

66
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Path, status
77
from fastapi_utils.cbv import cbv
88

9-
from app.api.deps import CurrentUser, CurrentSuperuser, ItemCRUDDep, UserCRUDDep
9+
from app.api.deps import CurrentUser, CurrentSuperuser, ItemCrudDep, UserCrudDep
1010
from app.core.config import get_settings, Settings
1111
from app.core.emails import EmailManager, get_email_manager
1212
from app.core.security import get_security_manager, SecurityManager
@@ -24,10 +24,10 @@
2424

2525
__all__: tuple[str] = ("users_router",)
2626

27-
EmailManagerDep: Type[EmailManager] = Annotated[EmailManager, Depends(get_email_manager)]
28-
SettingsDep: Type[Settings] = Annotated[Settings, Depends(get_settings)]
29-
SecurityManagerDep: Type[SecurityManager] = Annotated[SecurityManager, Depends(get_security_manager)]
30-
UserIDDep: Type[UUID] = Annotated[UUID, Path(alias="id", description="User ID")]
27+
EmailManagerDep = Annotated[EmailManager, Depends(get_email_manager)]
28+
SettingsDep = Annotated[Settings, Depends(get_settings)]
29+
SecurityManagerDep = Annotated[SecurityManager, Depends(get_security_manager)]
30+
UserIdDep = Annotated[UUID, Path(alias="id", description="User ID")]
3131

3232
users_router: APIRouter = APIRouter()
3333

@@ -36,13 +36,13 @@
3636
class UsersRouter:
3737
"""Class-based view for user endpoints."""
3838

39-
def __init__(self, user_crud: UserCRUDDep) -> None:
39+
def __init__(self, user_crud: UserCrudDep) -> None:
4040
"""
4141
Initializes the UsersRouter class with the necessary dependencies.
4242
4343
:param user_crud: Dependency for user CRUD operations.
4444
"""
45-
self._user_crud: UserCRUDDep = user_crud
45+
self._user_crud: UserCrudDep = user_crud
4646

4747
@users_router.get(
4848
path="/",
@@ -209,7 +209,7 @@ async def register_user(self, user_in: UserRegister) -> User:
209209
@users_router.get(
210210
path="/{id}", response_model=UserPublic, summary="Read User by ID", description="Retrieving user data by ID."
211211
)
212-
async def read_user_by_id(self, user_id: UserIDDep, current_user: CurrentUser) -> User:
212+
async def read_user_by_id(self, user_id: UserIdDep, current_user: CurrentUser) -> User:
213213
"""
214214
Endpoint to retrieve a user's data by their ID.
215215
@@ -233,7 +233,7 @@ async def read_user_by_id(self, user_id: UserIDDep, current_user: CurrentUser) -
233233
summary="Update User by ID",
234234
description="Update user by ID (for superusers only).",
235235
)
236-
async def update_user(self, user_id: UserIDDep, user_in: UserUpdate, _: CurrentSuperuser) -> User:
236+
async def update_user(self, user_id: UserIdDep, user_in: UserUpdate, _: CurrentSuperuser) -> User:
237237
"""
238238
Endpoint to update a user by their ID. Requires superuser privileges.
239239
@@ -262,7 +262,7 @@ async def update_user(self, user_id: UserIDDep, user_in: UserUpdate, _: CurrentS
262262
description="Delete a user by their ID (superusers only).",
263263
)
264264
async def delete_user(
265-
self, user_id: UserIDDep, item_crud: ItemCRUDDep, current_superuser: CurrentSuperuser
265+
self, user_id: UserIdDep, item_crud: ItemCrudDep, current_superuser: CurrentSuperuser
266266
) -> Message:
267267
"""
268268
Endpoint to delete a user by their ID. Requires superuser privileges.

backend/src/app/api/routes/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Module for utility endpoints."""
22

3-
from typing import Annotated, Type
3+
from typing import Annotated
44

55
from fastapi import APIRouter, BackgroundTasks, Depends
66
from fastapi_utils.cbv import cbv
@@ -13,7 +13,7 @@
1313
__all__: tuple[str] = ("utils_router",)
1414

1515

16-
EmailManagerDep: Type[EmailManager] = Annotated[EmailManager, Depends(get_email_manager)]
16+
EmailManagerDep = Annotated[EmailManager, Depends(get_email_manager)]
1717

1818
utils_router: APIRouter = APIRouter()
1919

backend/src/app/core/config.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from urllib.parse import quote_plus
77
from warnings import warn
88

9-
from pydantic import AnyUrl, BeforeValidator, EmailStr, computed_field, model_validator
9+
from pydantic import AnyUrl, BeforeValidator, EmailStr, computed_field, model_validator, SecretStr
1010
from pydantic_settings import BaseSettings, SettingsConfigDict
1111
from typing_extensions import Self
1212

@@ -46,7 +46,7 @@ def _parse_cors(v: list[str] | str) -> list[str] | str:
4646
return v
4747
raise ValueError(v)
4848

49-
@computed_field
49+
@computed_field # type: ignore[prop-decorator]
5050
@property
5151
def all_cors_origins(self) -> list[str]:
5252
"""
@@ -68,7 +68,7 @@ def all_cors_origins(self) -> list[str]:
6868
POSTGRES_PASSWORD: str = ""
6969
POSTGRES_DB: str = ""
7070

71-
@computed_field
71+
@computed_field # type: ignore[prop-decorator]
7272
@property
7373
def sqlalchemy_database_uri(self) -> str:
7474
"""
@@ -100,7 +100,7 @@ def sqlalchemy_database_uri(self) -> str:
100100
SMTP_PORT: int = 587
101101
SMTP_HOST: str | None = None
102102
SMTP_USER: str | None = None
103-
SMTP_PASSWORD: str | None = None
103+
SMTP_PASSWORD: SecretStr | None = None
104104
EMAILS_FROM_EMAIL: EmailStr | None = None
105105
EMAILS_FROM_NAME: EmailStr | None = None
106106
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
@@ -114,10 +114,10 @@ def _set_default_emails_from(self) -> Self:
114114
:return: The instance of the model.
115115
"""
116116
if not self.EMAILS_FROM_NAME:
117-
self.EMAILS_FROM_NAME = self.PROJECT_NAME
117+
self.EMAILS_FROM_NAME = self.PROJECT_NAME # pylint: disable=invalid-name
118118
return self
119119

120-
@computed_field
120+
@computed_field # type: ignore[prop-decorator]
121121
@property
122122
def emails_enabled(self) -> bool:
123123
"""
@@ -164,4 +164,4 @@ def get_settings() -> Settings:
164164
165165
:return: Settings object.
166166
"""
167-
return Settings()
167+
return Settings() # type: ignore[call-arg]

0 commit comments

Comments
 (0)