modern-di-fastapi 1.1.0__py3-none-any.whl → 2.0.0a0__py3-none-any.whl
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/main.py +9 -24
- {modern_di_fastapi-1.1.0.dist-info → modern_di_fastapi-2.0.0a0.dist-info}/METADATA +1 -1
- modern_di_fastapi-2.0.0a0.dist-info/RECORD +6 -0
- modern_di_fastapi-1.1.0.dist-info/RECORD +0 -6
- {modern_di_fastapi-1.1.0.dist-info → modern_di_fastapi-2.0.0a0.dist-info}/WHEEL +0 -0
modern_di_fastapi/main.py
CHANGED
|
@@ -1,37 +1,24 @@
|
|
|
1
|
-
import contextlib
|
|
2
1
|
import dataclasses
|
|
3
2
|
import typing
|
|
4
3
|
|
|
5
4
|
import fastapi
|
|
6
|
-
from
|
|
7
|
-
from modern_di import AsyncContainer, Scope, providers
|
|
5
|
+
from modern_di import Container, Scope, providers
|
|
8
6
|
from starlette.requests import HTTPConnection
|
|
9
7
|
|
|
10
8
|
|
|
11
9
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
12
10
|
|
|
13
11
|
|
|
14
|
-
def fetch_di_container(app_: fastapi.FastAPI) ->
|
|
15
|
-
return typing.cast(
|
|
12
|
+
def fetch_di_container(app_: fastapi.FastAPI) -> Container:
|
|
13
|
+
return typing.cast(Container, app_.state.di_container)
|
|
16
14
|
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
|
|
20
|
-
async with fetch_di_container(app_):
|
|
21
|
-
yield
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def setup_di(app: fastapi.FastAPI, container: AsyncContainer) -> AsyncContainer:
|
|
16
|
+
def setup_di(app: fastapi.FastAPI, container: Container) -> Container:
|
|
25
17
|
app.state.di_container = container
|
|
26
|
-
old_lifespan_manager = app.router.lifespan_context
|
|
27
|
-
app.router.lifespan_context = _merge_lifespan_context(
|
|
28
|
-
old_lifespan_manager,
|
|
29
|
-
_lifespan_manager,
|
|
30
|
-
)
|
|
31
18
|
return container
|
|
32
19
|
|
|
33
20
|
|
|
34
|
-
async def build_di_container(connection: HTTPConnection) ->
|
|
21
|
+
async def build_di_container(connection: HTTPConnection) -> Container:
|
|
35
22
|
context: dict[type[typing.Any], typing.Any] = {}
|
|
36
23
|
scope: Scope | None = None
|
|
37
24
|
if isinstance(connection, fastapi.Request):
|
|
@@ -40,9 +27,7 @@ async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator
|
|
|
40
27
|
elif isinstance(connection, fastapi.WebSocket):
|
|
41
28
|
context[fastapi.WebSocket] = connection
|
|
42
29
|
scope = Scope.SESSION
|
|
43
|
-
|
|
44
|
-
async with container.build_child_container(context=context, scope=scope) as request_container:
|
|
45
|
-
yield request_container
|
|
30
|
+
return fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
|
|
46
31
|
|
|
47
32
|
|
|
48
33
|
@dataclasses.dataclass(slots=True, frozen=True)
|
|
@@ -50,11 +35,11 @@ class Dependency(typing.Generic[T_co]):
|
|
|
50
35
|
dependency: providers.AbstractProvider[T_co] | type[T_co]
|
|
51
36
|
|
|
52
37
|
async def __call__(
|
|
53
|
-
self, request_container: typing.Annotated[
|
|
38
|
+
self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
|
|
54
39
|
) -> T_co:
|
|
55
40
|
if isinstance(self.dependency, providers.AbstractProvider):
|
|
56
|
-
return
|
|
57
|
-
return
|
|
41
|
+
return request_container.resolve_provider(self.dependency)
|
|
42
|
+
return request_container.resolve(dependency_type=self.dependency)
|
|
58
43
|
|
|
59
44
|
|
|
60
45
|
def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
|
|
2
|
+
modern_di_fastapi/main.py,sha256=Kwq7h23JafSrAQsCjEAGBimw_V9upoT_t_UuP82By5c,1683
|
|
3
|
+
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
modern_di_fastapi-2.0.0a0.dist-info/METADATA,sha256=6X-CtNc2nL8isvPBQW4a5oJOCw2r15uroPl8E3A0KgY,991
|
|
5
|
+
modern_di_fastapi-2.0.0a0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
modern_di_fastapi-2.0.0a0.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
|
|
2
|
-
modern_di_fastapi/main.py,sha256=21y8wmPHmaQpZE-IxvJVul0lHuM9k7PaEL9DmqWi9rw,2270
|
|
3
|
-
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
modern_di_fastapi-1.1.0.dist-info/METADATA,sha256=6o5px2yTPFAYn7woBqej2rSnMmGDQ3uhqMS0GHU72Kg,989
|
|
5
|
-
modern_di_fastapi-1.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
-
modern_di_fastapi-1.1.0.dist-info/RECORD,,
|
|
File without changes
|