Skip to content

Commit

Permalink
fix(sftkit): eliminate all typing and linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mikonse committed Jun 23, 2024
1 parent 208b953 commit ccb0751
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ target-version = "py311"

[tool.ruff.lint]
select = ["I", "F", "E", "PL"]
ignore = ["PLR0913", "E722"]

[tool.mypy]
ignore_missing_imports = true
Expand Down
1 change: 1 addition & 0 deletions sftkit/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ target-version = "py311"

[tool.ruff.lint]
select = ["I", "F", "E", "PL"]
ignore = ["PLR0913", "E722"]

[tool.mypy]
ignore_missing_imports = true
Expand Down
1 change: 0 additions & 1 deletion sftkit/sftkit/__main__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from sftkit.devel import cli_main
2 changes: 1 addition & 1 deletion sftkit/sftkit/database/_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ._attach import psql_attach
from ._config import DatabaseConfig
from ._migrations import SchemaMigration, apply_migrations, MIGRATION_TABLE, reload_db_code
from ._migrations import MIGRATION_TABLE, SchemaMigration, apply_migrations, reload_db_code
from ._pool import Pool, create_db_pool


Expand Down
1 change: 1 addition & 0 deletions sftkit/sftkit/devel/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from sftkit import database
from sftkit.util import log_setup

from ._config import read_config
from ._debian import build_debian_packages as _build_debian_packages

Expand Down
2 changes: 1 addition & 1 deletion sftkit/sftkit/devel/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _load_toml(path: Path) -> dict[str, Any]:
return tomllib.load(f)


def _find_pyproject_toml(start_path: Path | None) -> Path | None:
def _find_pyproject_toml(start_path: Path) -> Path | None:
for directory in (start_path, *start_path.parents):
pyproject_path = directory / "pyproject.toml"
if pyproject_path.is_file():
Expand Down
2 changes: 1 addition & 1 deletion sftkit/sftkit/http/_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar, Generic
from typing import Generic, TypeVar

from fastapi import Request, WebSocket
from starlette.types import ASGIApp, Receive, Scope, Send
Expand Down
20 changes: 13 additions & 7 deletions sftkit/sftkit/http/_error.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing

from fastapi import Request, status
from fastapi.responses import JSONResponse

Expand All @@ -9,8 +11,9 @@
)


def not_found_exception_handler(request: Request, exc: NotFound):
def not_found_exception_handler(request: Request, broad_exception: Exception) -> JSONResponse:
del request
exc = typing.cast(NotFound, broad_exception)
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content={
Expand All @@ -23,8 +26,9 @@ def not_found_exception_handler(request: Request, exc: NotFound):
)


def service_exception_handler(request: Request, exc: ServiceException):
def service_exception_handler(request: Request, broad_exception: Exception) -> JSONResponse:
del request
exc = typing.cast(ServiceException, broad_exception)
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={
Expand All @@ -35,8 +39,9 @@ def service_exception_handler(request: Request, exc: ServiceException):
)


def access_exception_handler(request: Request, exc: AccessDenied):
def access_exception_handler(request: Request, broad_exception: Exception) -> JSONResponse:
del request
exc = typing.cast(AccessDenied, broad_exception)
return JSONResponse(
status_code=status.HTTP_403_FORBIDDEN,
content={
Expand All @@ -47,8 +52,9 @@ def access_exception_handler(request: Request, exc: AccessDenied):
)


def unauthorized_exception_handler(request: Request, exc: Unauthorized):
def unauthorized_exception_handler(request: Request, broad_exception: Exception) -> JSONResponse:
del request
exc = typing.cast(Unauthorized, broad_exception)
return JSONResponse(
status_code=status.HTTP_401_UNAUTHORIZED,
content={
Expand All @@ -59,7 +65,7 @@ def unauthorized_exception_handler(request: Request, exc: Unauthorized):
)


def try_again_later_exception_handler(request: Request, exc: Exception):
def try_again_later_exception_handler(request: Request, exc: Exception) -> JSONResponse:
del request
return JSONResponse(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
Expand All @@ -72,7 +78,7 @@ def try_again_later_exception_handler(request: Request, exc: Exception):
)


def bad_request_exception_handler(request: Request, exc: Exception):
def bad_request_exception_handler(request: Request, exc: Exception) -> JSONResponse:
del request
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
Expand All @@ -84,7 +90,7 @@ def bad_request_exception_handler(request: Request, exc: Exception):
)


def catchall_exception_handler(request: Request, exc: Exception):
def catchall_exception_handler(request: Request, exc: Exception) -> JSONResponse:
del request
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
Expand Down
12 changes: 5 additions & 7 deletions sftkit/sftkit/http/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import asyncio
import logging
from typing import TypeVar, Generic
from typing import Generic, TypeVar
from urllib.parse import urlparse

import asyncpg
Expand All @@ -16,6 +16,10 @@
from ._config import HTTPServerConfig
from ._context import ContextMiddleware
from ._error import (
AccessDenied,
NotFound,
ServiceException,
Unauthorized,
access_exception_handler,
bad_request_exception_handler,
catchall_exception_handler,
Expand All @@ -24,12 +28,6 @@
try_again_later_exception_handler,
unauthorized_exception_handler,
)
from ._error import (
AccessDenied,
NotFound,
ServiceException,
Unauthorized,
)

ContextT = TypeVar("ContextT")

Expand Down
16 changes: 9 additions & 7 deletions sftkit/sftkit/service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
import logging
import typing
from abc import ABC
from functools import wraps
from inspect import Parameter, signature
from random import random
from typing import Awaitable, Callable, Generic, TypeVar, overload, Concatenate, ParamSpec
from typing import Awaitable, Callable, Concatenate, Generic, ParamSpec, TypeVar, overload

import asyncpg

Expand Down Expand Up @@ -54,12 +55,12 @@ def with_db_connection(
func: Callable[Concatenate[Self, P], Awaitable[R]],
) -> Callable[Concatenate[Self, P], Awaitable[R]]:
@wraps(func)
async def wrapper(self, **kwargs: P.kwargs):
async def wrapper(self, *args: P.args, **kwargs: P.kwargs):
if "conn" in kwargs:
return await func(self, **kwargs)
return await func(self, *args, **kwargs)

async with self.db_pool.acquire() as conn:
return await func(self, conn=conn, **kwargs)
return await func(self, *args, conn=conn, **kwargs)

return wrapper

Expand All @@ -68,20 +69,20 @@ def _with_db_isolation_transaction(
func: Callable[Concatenate[Self, P], Awaitable[R]], read_only: bool, n_retries: int | None = None
) -> Callable[Concatenate[Self, P], Awaitable[R]]:
@wraps(func)
async def wrapper(self, **kwargs: P.kwargs):
async def wrapper(self, *args: P.args, **kwargs: P.kwargs):
_add_readonly_to_kwargs(read_only, kwargs, func)

max_retries = n_retries or self.default_transaction_retries
current_retries = max_retries
if "conn" in kwargs:
return await func(self, **kwargs)
return await func(self, *args, **kwargs)

async with self.db_pool.acquire() as conn:
exception = None
while current_retries > 0:
try:
async with conn.transaction(isolation=None if read_only else "serializable"):
return await func(self, conn=conn, **kwargs)
return await func(self, *args, conn=conn, **kwargs)
except (
asyncpg.exceptions.DeadlockDetectedError,
asyncpg.exceptions.SerializationError,
Expand Down Expand Up @@ -121,6 +122,7 @@ def with_db_transaction(
"""Case with arguments"""


@typing.no_type_check
def with_db_transaction(read_only, n_retries: int | None = None):
if callable(read_only):
return _with_db_isolation_transaction(read_only, read_only=False, n_retries=n_retries)
Expand Down
3 changes: 1 addition & 2 deletions sftkit/tests/test_dbhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import asyncpg
import pytest

from sftkit.database import DatabaseHook, DatabaseConfig
from sftkit.database import create_db_pool
from sftkit.database import DatabaseConfig, DatabaseHook, create_db_pool


@pytest.mark.skip("currently does not work as test setup is not yet finalized")
Expand Down

0 comments on commit ccb0751

Please sign in to comment.