Skip to content

Commit

Permalink
Add ruff and fix the linter warnings (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrimov authored Jan 13, 2024
1 parent b89d345 commit a2e401a
Show file tree
Hide file tree
Showing 42 changed files with 600 additions and 363 deletions.
312 changes: 138 additions & 174 deletions poetry.lock

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,28 @@ testcontainers-minio = "^0.0.1rc1" # for now its a separate package, in future
optional = true

[tool.poetry.group.lint.dependencies]
flake8 = "^6.1.0"
isort = "^5.12.0"
ruff = "^0.1.11"
mypy = "^1.5.1"


[tool.pytest.ini_options]
pythonpath = ["src"]
testpaths = ["tests"]
asyncio_mode = "auto"
addopts = "-vv --order-scope=module"


[tool.mypy]
explicit_package_bases = true


[tool.ruff]
select = ["ALL"]
ignore = ["A003", "ANN", "D", "FA100", "FA102", "FIX", "I", "PGH003", "PGH004", "TD"]

[tool.ruff.extend-per-file-ignores]
"src/app/infrastructure/database/migrations/versions/*" = ["ALL"]
"tests/*" = ["ARG001", "F401", "PT003", "PT004", "S"]


[build-system]
Expand Down
2 changes: 1 addition & 1 deletion src/app/application/common/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC


class DTO(ABC):
class DTO(ABC): # noqa: B024
pass


Expand Down
2 changes: 1 addition & 1 deletion src/app/application/common/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationException(Exception):
class ApplicationException(Exception): # noqa: N818
"""Base application exception"""

@property
Expand Down
2 changes: 1 addition & 1 deletion src/app/application/common/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC


class Filters(ABC):
class Filters(ABC): # noqa: B024
pass
13 changes: 11 additions & 2 deletions src/app/application/common/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ class LimitOffsetPaginationResult(DTO, PaginationResult):
total: int

@classmethod
def from_pagination(cls, pagination: LimitOffsetPagination, total: int) -> "LimitOffsetPaginationResult":
return cls(offset=pagination.offset, limit=pagination.limit, order=pagination.order, total=total)
def from_pagination(
cls,
pagination: LimitOffsetPagination, # type: ignore[override]
total: int,
) -> "LimitOffsetPaginationResult":
return cls(
offset=pagination.offset,
limit=pagination.limit,
order=pagination.order,
total=total,
)


@dataclass(frozen=True)
Expand Down
6 changes: 3 additions & 3 deletions src/app/application/problem/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ProblemStateDatabaseGatewayError(DatabaseGatewayError):


@dataclass(eq=False)
class ProblemIdNotExists(ProblemDatabaseGatewayError):
class ProblemIdNotExists(ProblemDatabaseGatewayError): # noqa: N818
id: int

@property
Expand All @@ -25,7 +25,7 @@ def title(self) -> str:


@dataclass(eq=False)
class ProblemStateIdNotExists(ProblemStateDatabaseGatewayError):
class ProblemStateIdNotExists(ProblemStateDatabaseGatewayError): # noqa: N818
id: int

@property
Expand All @@ -34,7 +34,7 @@ def title(self) -> str:


@dataclass(eq=False)
class ProblemStateNameNotExists(ProblemStateDatabaseGatewayError):
class ProblemStateNameNotExists(ProblemStateDatabaseGatewayError): # noqa: N818
name: str

@property
Expand Down
4 changes: 2 additions & 2 deletions src/app/application/user/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class UserDatabaseGatewayError(DatabaseGatewayError):


@dataclass(eq=False)
class UserIdNotExists(UserDatabaseGatewayError):
class UserIdNotExists(UserDatabaseGatewayError): # noqa: N818
id: int

@property
Expand All @@ -17,7 +17,7 @@ def title(self) -> str:


@dataclass(eq=False)
class UserNicknameNotExists(UserDatabaseGatewayError):
class UserNicknameNotExists(UserDatabaseGatewayError): # noqa: N818
nickname: str

@property
Expand Down
4 changes: 3 additions & 1 deletion src/app/application/user/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ async def get_user_by_nickname(self, nickname: str) -> dto.User:
raise NotImplementedError

@abc.abstractmethod
async def get_users(self, filters: UserFilters, pagination: Pagination) -> dto.Users:
async def get_users(
self, filters: UserFilters, pagination: Pagination,
) -> dto.Users:
raise NotImplementedError

@abc.abstractmethod
Expand Down
3 changes: 2 additions & 1 deletion src/app/infrastructure/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

class AIOBotoGateway:
"""
Generic AIOBoto3 gateway that requires an initialized session (with access-secret keys)
Generic AIOBoto3 gateway that requires
an initialized session (with access-secret keys)
"""

def __init__(self, session: aioboto3.Session):
Expand Down
11 changes: 10 additions & 1 deletion src/app/infrastructure/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
from .models import AppConfig, Config, DatabaseConfig
from .models import AppConfig, Config, DatabaseConfig, ObjectStorageConfig
from .parsers import load_config


__all__ = [
"AppConfig",
"Config",
"DatabaseConfig",
"ObjectStorageConfig",
"load_config",
]
8 changes: 8 additions & 0 deletions src/app/infrastructure/config/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
from .database import DatabaseConfig
from .main import Config
from .object_storage import ObjectStorageConfig


__all__ = [
"AppConfig",
"Config",
"DatabaseConfig",
"ObjectStorageConfig",
]
2 changes: 1 addition & 1 deletion src/app/infrastructure/config/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ def full_url(self) -> str:
return "{}+{}://{}:{}@{}:{}/{}".format(
self.rdbms, self.connector,
self.user, self.password,
self.host, self.port, self.database
self.host, self.port, self.database,
)
5 changes: 5 additions & 0 deletions src/app/infrastructure/config/parsers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
from .main import load_config


__all__ = [
"load_config",
]
28 changes: 21 additions & 7 deletions src/app/infrastructure/database/converters/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def convert_problem_dto_to_db_model(problem_dto: dto.Problem) -> ProblemModel:
)


def convert_problem_create_dto_to_db_model(problem_dto: dto.ProblemCreate) -> ProblemModel:
def convert_problem_create_dto_to_db_model(
problem_dto: dto.ProblemCreate,
) -> ProblemModel:
return ProblemModel(
title=problem_dto.title,
description=problem_dto.description,
Expand All @@ -39,23 +41,35 @@ def convert_problem_create_dto_to_db_model(problem_dto: dto.ProblemCreate) -> Pr
)


def update_problem_fields(existing_problem: dto.Problem, problem_update_dto: dto.ProblemUpdate) -> ProblemModel:
def update_problem_fields(
existing_problem: dto.Problem, problem_update_dto: dto.ProblemUpdate,
) -> ProblemModel:
result_model = ProblemModel()
for field in fields(existing_problem):
existing_field_value = getattr(existing_problem, field.name)
new_value = getattr(problem_update_dto, field.name) if hasattr(problem_update_dto, field.name) else None
setattr(result_model, field.name, new_value if new_value is not None else existing_field_value)

if hasattr(problem_update_dto, field.name):
new_value = getattr(problem_update_dto, field.name)
else:
new_value = None

settable_value = new_value if new_value is not None else existing_field_value
setattr(result_model, field.name, settable_value)
return result_model


def convert_problem_state_create_dto_to_db_model(problem_state_dto: dto.ProblemStateCreate) -> ProblemStateModel:
def convert_problem_state_create_dto_to_db_model(
problem_state_dto: dto.ProblemStateCreate,
) -> ProblemStateModel:
return ProblemStateModel(
name=problem_state_dto.name,
)


def convert_db_model_to_problem_state_dto(problem: ProblemStateModel) -> dto.ProblemState:
def convert_db_model_to_problem_state_dto(
problem: ProblemStateModel,
) -> dto.ProblemState:
return dto.ProblemState(
id=problem.id,
name=problem.name
name=problem.name,
)
14 changes: 11 additions & 3 deletions src/app/infrastructure/database/converters/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ def convert_user_create_dto_to_db_model(user_dto: dto.UserCreate) -> UserModel:
)


def update_user_fields(existing_user: dto.User, user_update_dto: dto.UserUpdate) -> UserModel:
def update_user_fields(
existing_user: dto.User, user_update_dto: dto.UserUpdate,
) -> UserModel:
result_model = UserModel()
for field in fields(existing_user):
existing_field_value = getattr(existing_user, field.name)
new_value = getattr(user_update_dto, field.name) if hasattr(user_update_dto, field.name) else None
setattr(result_model, field.name, new_value if new_value is not None else existing_field_value)

if hasattr(user_update_dto, field.name):
new_value = getattr(user_update_dto, field.name)
else:
new_value = None

settable_value = new_value if new_value is not None else existing_field_value
setattr(result_model, field.name, settable_value)
return result_model
2 changes: 1 addition & 1 deletion src/app/infrastructure/database/exception_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def exception_mapper(
func: Callable[Param, Coroutine[Any, Any, ReturnType]]
func: Callable[Param, Coroutine[Any, Any, ReturnType]],
) -> Callable[Param, Coroutine[Any, Any, ReturnType]]:
@wraps(func)
async def wrapped(*args: Param.args, **kwargs: Param.kwargs) -> ReturnType:
Expand Down
16 changes: 11 additions & 5 deletions src/app/infrastructure/database/factory.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from collections.abc import AsyncGenerator

from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
create_async_engine,
)

from src.app.infrastructure.config.models.database import DatabaseConfig


async def build_sa_engine(db_config: DatabaseConfig) -> AsyncGenerator[AsyncEngine, None]:
async def build_sa_engine(
db_config: DatabaseConfig,
) -> AsyncGenerator[AsyncEngine, None]:
engine = create_async_engine(
db_config.full_url,
echo=True,
Expand All @@ -18,12 +25,11 @@ async def build_sa_engine(db_config: DatabaseConfig) -> AsyncGenerator[AsyncEngi


def build_sa_session_factory(engine: AsyncEngine) -> async_sessionmaker[AsyncSession]:
session_factory = async_sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
return session_factory
return async_sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)


async def build_sa_session(
session_factory: async_sessionmaker[AsyncSession]
session_factory: async_sessionmaker[AsyncSession],
) -> AsyncGenerator[AsyncSession, None]:
async with session_factory() as session:
yield session
Loading

0 comments on commit a2e401a

Please sign in to comment.