Skip to content

Commit

Permalink
v.1.5.2: fix type for depends protocol - add new protocols.
Browse files Browse the repository at this point in the history
  • Loading branch information
ALittleMoron committed May 16, 2024
1 parent c302ec3 commit b4c0ae8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ dev = [

[project]
name = "sqlrepo"
version = "1.5.1"
version = "1.5.2"
description = "sqlalchemy repositories with crud operations and other utils for it."
authors = [{ name = "Dmitriy Lunev", email = "dima.lunev14@gmail.com" }]
requires-python = ">=3.11"
Expand Down
27 changes: 23 additions & 4 deletions sqlrepo/ext/fastapi/containers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Protocol
from typing import TYPE_CHECKING, Protocol, TypeAlias

from fastapi import Depends, Request

Expand All @@ -9,17 +9,36 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm.session import Session

class SyncSessionGeneratorDependsProtocol(Protocol):
"""Sync session depends protocol for FastAPI framework."""

@staticmethod
def __call__() -> Generator[Session, None, None]: ... # noqa: D102

class SyncSessionDependsProtocol(Protocol):
"""Sync session depends protocol for FastAPI framework."""

@staticmethod
def __call__() -> Session | Generator[Session, None, None]: ... # noqa: D102
def __call__() -> Session: ... # noqa: D102

class AsyncSessionGeneratorDependsProtocol(Protocol):
"""Async session depends protocol for FastAPI framework."""

@staticmethod
async def __call__() -> AsyncGenerator[AsyncSession, None]: ... # noqa: D102

class AsyncSessionDependsProtocol(Protocol):
"""Async session depends protocol for FastAPI framework."""

@staticmethod
async def __call__() -> AsyncSession | AsyncGenerator[AsyncSession, None]: ... # noqa: D102
async def __call__() -> AsyncSession: ... # noqa: D102

SessionDepends: TypeAlias = (
SyncSessionDependsProtocol
| AsyncSessionDependsProtocol
| SyncSessionGeneratorDependsProtocol
| AsyncSessionGeneratorDependsProtocol
)


def _get_session_stub() -> None:
Expand All @@ -28,7 +47,7 @@ def _get_session_stub() -> None:

def add_container_overrides(
app: "FastAPI",
session_depends: "SyncSessionDependsProtocol | AsyncSessionDependsProtocol",
session_depends: "SessionDepends",
) -> "FastAPI":
"""Container plugin function.
Expand Down
8 changes: 4 additions & 4 deletions sqlrepo/uow.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ async def __aenter__(self) -> Self: # noqa: D105

async def __aexit__( # noqa: D105
self,
exc_type: type[BaseException] | None,
exc_type: type[BaseException] | None, # noqa
exc: BaseException | None,
traceback: types.TracebackType | None,
traceback: types.TracebackType | None, # noqa
) -> None:
if exc: # pragma: no coverage
logger.error("UNIT-OF-WORK E0: %s", exc)
Expand Down Expand Up @@ -101,9 +101,9 @@ def __enter__(self) -> Self: # noqa: D105

def __exit__( # noqa: D105
self,
exc_type: type[BaseException] | None,
exc_type: type[BaseException] | None, # noqa
exc: BaseException | None,
traceback: types.TracebackType | None,
traceback: types.TracebackType | None, # noqa
) -> None:
if exc: # pragma: no coverage
logger.error("UNIT-OF-WORK E0: %s", exc)
Expand Down

0 comments on commit b4c0ae8

Please sign in to comment.