modern-di-fastapi 0.1.3__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- modern_di_fastapi-0.2.0/Justfile +18 -0
- {modern_di_fastapi-0.1.3 → modern_di_fastapi-0.2.0}/PKG-INFO +1 -1
- modern_di_fastapi-0.2.0/modern_di_fastapi/__init__.py +9 -0
- modern_di_fastapi-0.2.0/modern_di_fastapi/main.py +38 -0
- {modern_di_fastapi-0.1.3 → modern_di_fastapi-0.2.0}/pyproject.toml +0 -3
- {modern_di_fastapi-0.1.3 → modern_di_fastapi-0.2.0}/tests/test_fastapi_di.py +5 -6
- modern_di_fastapi-0.1.3/modern_di_fastapi/__init__.py +0 -9
- modern_di_fastapi-0.1.3/modern_di_fastapi/depends.py +0 -24
- modern_di_fastapi-0.1.3/modern_di_fastapi/middleware.py +0 -27
- {modern_di_fastapi-0.1.3 → modern_di_fastapi-0.2.0}/.gitignore +0 -0
- {modern_di_fastapi-0.1.3 → modern_di_fastapi-0.2.0}/README.md +0 -0
- {modern_di_fastapi-0.1.3 → modern_di_fastapi-0.2.0}/modern_di_fastapi/py.typed +0 -0
- {modern_di_fastapi-0.1.3 → modern_di_fastapi-0.2.0}/tests/__init__.py +0 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
default: install lint test
|
|
2
|
+
|
|
3
|
+
install:
|
|
4
|
+
uv lock --upgrade
|
|
5
|
+
uv sync --all-extras --frozen
|
|
6
|
+
|
|
7
|
+
lint:
|
|
8
|
+
uv run ruff format .
|
|
9
|
+
uv run ruff check . --fix
|
|
10
|
+
uv run mypy .
|
|
11
|
+
|
|
12
|
+
lint-ci:
|
|
13
|
+
uv run ruff format . --check
|
|
14
|
+
uv run ruff check . --no-fix
|
|
15
|
+
uv run mypy .
|
|
16
|
+
|
|
17
|
+
test *args:
|
|
18
|
+
uv run pytest {{ args }}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
import fastapi
|
|
5
|
+
from modern_di import Container, providers
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def save_di_container(obj: fastapi.FastAPI | fastapi.Request, container: Container) -> None:
|
|
12
|
+
obj.state.di_container = container
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def fetch_di_container(obj: fastapi.FastAPI | fastapi.Request) -> Container:
|
|
16
|
+
return typing.cast(Container, obj.state.di_container)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
20
|
+
class Dependency(typing.Generic[T_co]):
|
|
21
|
+
dependency: providers.AbstractProvider[T_co]
|
|
22
|
+
|
|
23
|
+
async def __call__(self, request: fastapi.Request) -> T_co:
|
|
24
|
+
return await self.dependency.async_resolve(fetch_di_container(request))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
28
|
+
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
async def enter_di_request_scope(request: fastapi.Request) -> typing.AsyncIterator[None]:
|
|
32
|
+
container: Container = fetch_di_container(request.app)
|
|
33
|
+
async with container.build_child_container(context={"request": request}) as request_container:
|
|
34
|
+
save_di_container(request, request_container)
|
|
35
|
+
try:
|
|
36
|
+
yield
|
|
37
|
+
finally:
|
|
38
|
+
del request.state.di_container
|
|
@@ -11,20 +11,19 @@ from modern_di import Scope, providers
|
|
|
11
11
|
from starlette import status
|
|
12
12
|
from starlette.requests import Request
|
|
13
13
|
|
|
14
|
-
import
|
|
15
|
-
from modern_di_fastapi import
|
|
14
|
+
from modern_di_fastapi import FromDI, save_di_container
|
|
15
|
+
from modern_di_fastapi.main import enter_di_request_scope
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
@contextlib.asynccontextmanager
|
|
19
19
|
async def lifespan(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
|
|
20
20
|
di_container = modern_di.Container(scope=modern_di.Scope.APP)
|
|
21
|
-
|
|
21
|
+
save_di_container(app_, di_container)
|
|
22
22
|
async with di_container:
|
|
23
23
|
yield
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
app = fastapi.FastAPI(lifespan=lifespan)
|
|
27
|
-
app.add_middleware(ContainerMiddleware)
|
|
26
|
+
app = fastapi.FastAPI(lifespan=lifespan, dependencies=[fastapi.Depends(enter_di_request_scope)])
|
|
28
27
|
|
|
29
28
|
|
|
30
29
|
@dataclasses.dataclass(kw_only=True, slots=True)
|
|
@@ -60,7 +59,7 @@ async def read_root(
|
|
|
60
59
|
@pytest.fixture(scope="session")
|
|
61
60
|
async def client() -> typing.AsyncIterator[httpx.AsyncClient]:
|
|
62
61
|
async with LifespanManager(app):
|
|
63
|
-
yield httpx.AsyncClient(app=app, base_url="http://test")
|
|
62
|
+
yield httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test")
|
|
64
63
|
|
|
65
64
|
|
|
66
65
|
async def test_read_main(client: httpx.AsyncClient) -> None:
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import dataclasses
|
|
2
|
-
import typing
|
|
3
|
-
|
|
4
|
-
import fastapi
|
|
5
|
-
from modern_di import Container, providers
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@dataclasses.dataclass(slots=True, frozen=True)
|
|
12
|
-
class Dependency(typing.Generic[T_co]):
|
|
13
|
-
dependency: providers.AbstractProvider[T_co]
|
|
14
|
-
|
|
15
|
-
async def __call__(self, request: fastapi.Request) -> T_co:
|
|
16
|
-
return await self.dependency.async_resolve(request.state.modern_di_container)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
20
|
-
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def setup_modern_di(container: Container, app: fastapi.FastAPI) -> None:
|
|
24
|
-
app.state.modern_di_container = container
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import typing
|
|
2
|
-
|
|
3
|
-
import modern_di
|
|
4
|
-
from starlette.requests import Request
|
|
5
|
-
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class ContainerMiddleware:
|
|
9
|
-
def __init__(self, app: ASGIApp) -> None:
|
|
10
|
-
self.app = app
|
|
11
|
-
|
|
12
|
-
async def __call__(
|
|
13
|
-
self,
|
|
14
|
-
scope: Scope,
|
|
15
|
-
receive: Receive,
|
|
16
|
-
send: Send,
|
|
17
|
-
) -> None:
|
|
18
|
-
if scope["type"] != "http":
|
|
19
|
-
return await self.app(scope, receive, send)
|
|
20
|
-
|
|
21
|
-
request = Request(scope, receive=receive, send=send)
|
|
22
|
-
context: dict[str, typing.Any] = {"request": request}
|
|
23
|
-
|
|
24
|
-
container: modern_di.Container = request.app.state.modern_di_container
|
|
25
|
-
async with container.build_child_container(context=context) as request_container:
|
|
26
|
-
request.state.modern_di_container = request_container
|
|
27
|
-
return await self.app(scope, receive, send)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|