Skip to content

Commit a10f912

Browse files
committed
typing
1 parent 6f7e3f4 commit a10f912

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

app/core/db.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
Database
3+
"""
4+
5+
from typing import NoReturn, Self
6+
17
from sqlalchemy.ext.asyncio import (AsyncEngine, async_sessionmaker,
28
create_async_engine)
39
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -9,7 +15,7 @@
915
class Database:
1016
_instance = None
1117

12-
def __new__(cls, *args, **kwargs):
18+
def __new__(cls, *args, **kwargs) -> Self:
1319
if cls._instance is None:
1420
cls._instance = super(Database, cls).__new__(cls)
1521
return cls._instance
@@ -24,13 +30,13 @@ def __init__(
2430
self.session = session
2531
self.initialized = True
2632

27-
async def __set_async_engine(self) -> None:
33+
async def __set_async_engine(self) -> NoReturn:
2834
if self.engine is None:
2935
self.engine = create_async_engine(
3036
settings.pg_dsn.unicode_string(), echo=False, future=True
3137
)
3238

33-
async def __set_async_session(self) -> None:
39+
async def __set_async_session(self) -> NoReturn:
3440
if self.session is None:
3541
self.session = async_sessionmaker(
3642
autocommit=False,
@@ -40,16 +46,16 @@ async def __set_async_session(self) -> None:
4046
expire_on_commit=False,
4147
)()
4248

43-
async def __set_repositories(self) -> None:
49+
async def __set_repositories(self) -> NoReturn:
4450
if self.session is not None:
4551
self.user = repos.UserRepo(session=self.session)
4652

47-
async def __aenter__(self):
53+
async def __aenter__(self) -> Self:
4854
await self.__set_async_engine()
4955
await self.__set_async_session()
5056
await self.__set_repositories()
5157
return self
5258

53-
async def __aexit__(self, exc_type, exc_value, traceback):
59+
async def __aexit__(self, exc_type, exc_value, traceback) -> NoReturn:
5460
if self.session is not None:
5561
await self.session.close()

app/core/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ class Settings(BaseSettings):
2626

2727
@property
2828
def pg_dsn(self) -> PostgresDsn:
29-
dsn = PostgresDsn.build(
29+
return PostgresDsn.build(
3030
scheme='postgresql+asyncpg',
3131
username=self.POSTGRES_USER,
3232
password=self.POSTGRES_PASSWORD,
3333
host=self.POSTGRES_HOST,
3434
port=self.POSTGRES_PORT,
3535
path=self.POSTGRES_DB,
3636
)
37-
return dsn
3837

3938

4039
settings = Settings()

app/logic/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Self
2+
13
from app.core.db import Database
24

35
from .security import Security
@@ -11,7 +13,7 @@ def __init__(self, db: Database):
1113
self.users = Users(self)
1214

1315
@classmethod
14-
async def create(cls) -> 'Logic':
16+
async def create(cls) -> Self:
1517
async with Database() as db:
1618
return cls(db)
1719

app/logic/security/jwt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ def decode_token(self, token: str) -> dict | None:
1313
try:
1414
payload = jwt.decode(token, self.secret_key, algorithms=['HS256'])
1515
except Exception:
16-
raise exps.TOKEN_INVALID
16+
raise exps.TokenInvalidException()
1717

1818
exp = payload.get('exp')
1919
if exp and dt.datetime.now(dt.UTC).timestamp() > exp:
20-
raise exps.TOKEN_EXPIRED
20+
raise exps.TokenExpiredException()
2121
return payload.get('payload')
2222

2323
def encode_token(self, payload: dict, minutes: int) -> str:

0 commit comments

Comments
 (0)