modern-di-fastapi 0.1.0__tar.gz → 0.1.1__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.1.0 → modern_di_fastapi-0.1.1}/PKG-INFO +1 -1
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/modern_di_fastapi/depends.py +4 -4
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/tests/test_fastapi_di.py +4 -4
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/.gitignore +0 -0
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/Justfile +0 -0
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/README.md +0 -0
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/modern_di_fastapi/__init__.py +0 -0
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/modern_di_fastapi/middleware.py +0 -0
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/pyproject.toml +0 -0
- {modern_di_fastapi-0.1.0 → modern_di_fastapi-0.1.1}/tests/__init__.py +0 -0
|
@@ -2,7 +2,7 @@ import dataclasses
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
import fastapi
|
|
5
|
-
import
|
|
5
|
+
from modern_di import Container, providers
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
@@ -10,15 +10,15 @@ T_co = typing.TypeVar("T_co", covariant=True)
|
|
|
10
10
|
|
|
11
11
|
@dataclasses.dataclass(slots=True, frozen=True)
|
|
12
12
|
class Dependency(typing.Generic[T_co]):
|
|
13
|
-
dependency:
|
|
13
|
+
dependency: providers.AbstractProvider[T_co]
|
|
14
14
|
|
|
15
15
|
async def __call__(self, request: fastapi.Request) -> T_co:
|
|
16
16
|
return await self.dependency.async_resolve(request.state.modern_di_container)
|
|
17
17
|
|
|
18
18
|
|
|
19
|
-
def FromDI(dependency:
|
|
19
|
+
def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
20
20
|
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def setup_modern_di(container:
|
|
23
|
+
def setup_modern_di(container: Container, app: fastapi.FastAPI) -> None:
|
|
24
24
|
app.state.modern_di_container = container
|
|
@@ -7,7 +7,7 @@ import httpx
|
|
|
7
7
|
import modern_di
|
|
8
8
|
import pytest
|
|
9
9
|
from asgi_lifespan import LifespanManager
|
|
10
|
-
from modern_di import Scope,
|
|
10
|
+
from modern_di import Scope, providers
|
|
11
11
|
from starlette import status
|
|
12
12
|
from starlette.requests import Request
|
|
13
13
|
|
|
@@ -41,9 +41,9 @@ def context_adapter_function(*, request: Request, **_: object) -> str:
|
|
|
41
41
|
return request.method
|
|
42
42
|
|
|
43
43
|
|
|
44
|
-
app_factory =
|
|
45
|
-
request_factory =
|
|
46
|
-
context_adapter =
|
|
44
|
+
app_factory = providers.Factory(Scope.APP, SimpleCreator, dep1="original")
|
|
45
|
+
request_factory = providers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
|
|
46
|
+
context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_function)
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
@app.get("/")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|