modern-di-fastapi 0.4.2__tar.gz → 0.6.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.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: modern-di-fastapi
3
- Version: 0.4.2
3
+ Version: 0.6.0
4
4
  Summary: Modern-DI integration for FastAPI
5
5
  Project-URL: repository, https://github.com/modern-python/modern-di
6
6
  Project-URL: docs, https://modern-di.readthedocs.io
@@ -1,8 +1,10 @@
1
+ import contextlib
1
2
  import dataclasses
2
3
  import enum
3
4
  import typing
4
5
 
5
6
  import fastapi
7
+ from fastapi.routing import _merge_lifespan_context
6
8
  from modern_di import Container, Scope, providers
7
9
  from starlette.requests import HTTPConnection
8
10
 
@@ -10,13 +12,26 @@ from starlette.requests import HTTPConnection
10
12
  T_co = typing.TypeVar("T_co", covariant=True)
11
13
 
12
14
 
13
- def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP) -> Container:
14
- app.state.di_container = Container(scope=scope)
15
- return app.state.di_container
15
+ def fetch_di_container(app_: fastapi.FastAPI) -> Container:
16
+ return typing.cast(Container, app_.state.di_container)
16
17
 
17
18
 
18
- def fetch_di_container(app: fastapi.FastAPI) -> Container:
19
- return typing.cast(Container, app.state.di_container)
19
+ @contextlib.asynccontextmanager
20
+ async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
21
+ async with fetch_di_container(app_):
22
+ yield
23
+
24
+
25
+ def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP, container: Container | None = None) -> Container:
26
+ if not container:
27
+ container = Container(scope=scope)
28
+ app.state.di_container = container
29
+ old_lifespan_manager = app.router.lifespan_context
30
+ app.router.lifespan_context = _merge_lifespan_context(
31
+ old_lifespan_manager,
32
+ _lifespan_manager,
33
+ )
34
+ return container
20
35
 
21
36
 
22
37
  async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
@@ -30,7 +30,6 @@ dev = [
30
30
  "mypy",
31
31
  "typing-extensions",
32
32
  "httpx",
33
- "asgi-lifespan",
34
33
  ]
35
34
 
36
35
  [build-system]