modern-di-fastapi 0.4.2__py3-none-any.whl → 0.5.0__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 CHANGED
@@ -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,15 +12,26 @@ from starlette.requests import HTTPConnection
10
12
  T_co = typing.TypeVar("T_co", covariant=True)
11
13
 
12
14
 
15
+ def fetch_di_container(app_: fastapi.FastAPI) -> Container:
16
+ return typing.cast(Container, app_.state.di_container)
17
+
18
+
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
+
13
25
  def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP) -> Container:
14
26
  app.state.di_container = Container(scope=scope)
27
+ old_lifespan_manager = app.router.lifespan_context
28
+ app.router.lifespan_context = _merge_lifespan_context(
29
+ old_lifespan_manager,
30
+ _lifespan_manager,
31
+ )
15
32
  return app.state.di_container
16
33
 
17
34
 
18
- def fetch_di_container(app: fastapi.FastAPI) -> Container:
19
- return typing.cast(Container, app.state.di_container)
20
-
21
-
22
35
  async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
23
36
  context: dict[str, typing.Any] = {}
24
37
  scope: Scope | None = None
@@ -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.5.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
@@ -0,0 +1,6 @@
1
+ modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
2
+ modern_di_fastapi/main.py,sha256=qmrZTCbZ3QGF1UEbkxKAoUauNGIwgvjda4_ELaohXbg,2073
3
+ modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ modern_di_fastapi-0.5.0.dist-info/METADATA,sha256=LSbWWDOSrYSaBFbxWmJo6kdAMAmlLRC5vD0iB24dNEY,926
5
+ modern_di_fastapi-0.5.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ modern_di_fastapi-0.5.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.25.0
2
+ Generator: hatchling 1.27.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,6 +0,0 @@
1
- modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
2
- modern_di_fastapi/main.py,sha256=-LoYLK-0wqBguzpuYXsDRbVqg9Q0sEqfYDwJNegMoCY,1653
3
- modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- modern_di_fastapi-0.4.2.dist-info/METADATA,sha256=PkzDUpUtVRLAh08Ivw6T1N209jtSkWqNpguwECcUT8o,926
5
- modern_di_fastapi-0.4.2.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
6
- modern_di_fastapi-0.4.2.dist-info/RECORD,,