modern-di-fastapi 2.0.0__tar.gz → 2.0.0a0__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
1
  Metadata-Version: 2.4
2
2
  Name: modern-di-fastapi
3
- Version: 2.0.0
3
+ Version: 2.0.0a0
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
@@ -16,7 +16,7 @@ Classifier: Topic :: Software Development :: Libraries
16
16
  Classifier: Typing :: Typed
17
17
  Requires-Python: <4,>=3.10
18
18
  Requires-Dist: fastapi>=0.100
19
- Requires-Dist: modern-di>=2.0.0alpha1
19
+ Requires-Dist: modern-di>=1.0.0alpha
20
20
  Description-Content-Type: text/markdown
21
21
 
22
22
  "Modern-DI-FastAPI"
@@ -1,9 +1,7 @@
1
- import contextlib
2
1
  import dataclasses
3
2
  import typing
4
3
 
5
4
  import fastapi
6
- from fastapi.routing import _merge_lifespan_context
7
5
  from modern_di import Container, Scope, providers
8
6
  from starlette.requests import HTTPConnection
9
7
 
@@ -11,35 +9,16 @@ from starlette.requests import HTTPConnection
11
9
  T_co = typing.TypeVar("T_co", covariant=True)
12
10
 
13
11
 
14
- fastapi_request = providers.ContextProvider(scope=Scope.REQUEST, context_type=fastapi.Request)
15
- fastapi_websocket = providers.ContextProvider(scope=Scope.SESSION, context_type=fastapi.WebSocket)
16
-
17
-
18
12
  def fetch_di_container(app_: fastapi.FastAPI) -> Container:
19
13
  return typing.cast(Container, app_.state.di_container)
20
14
 
21
15
 
22
- @contextlib.asynccontextmanager
23
- async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
24
- container = fetch_di_container(app_)
25
- try:
26
- yield
27
- finally:
28
- await container.close_async()
29
-
30
-
31
16
  def setup_di(app: fastapi.FastAPI, container: Container) -> Container:
32
17
  app.state.di_container = container
33
- container.providers_registry.add_providers(fastapi_request=fastapi_request, fastapi_websocket=fastapi_websocket)
34
- old_lifespan_manager = app.router.lifespan_context
35
- app.router.lifespan_context = _merge_lifespan_context(
36
- old_lifespan_manager,
37
- _lifespan_manager,
38
- )
39
18
  return container
40
19
 
41
20
 
42
- async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
21
+ async def build_di_container(connection: HTTPConnection) -> Container:
43
22
  context: dict[type[typing.Any], typing.Any] = {}
44
23
  scope: Scope | None = None
45
24
  if isinstance(connection, fastapi.Request):
@@ -48,11 +27,7 @@ async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator
48
27
  elif isinstance(connection, fastapi.WebSocket):
49
28
  context[fastapi.WebSocket] = connection
50
29
  scope = Scope.SESSION
51
- container = fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
52
- try:
53
- yield container
54
- finally:
55
- await container.close_async()
30
+ return fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
56
31
 
57
32
 
58
33
  @dataclasses.dataclass(slots=True, frozen=True)
@@ -15,8 +15,8 @@ classifiers = [
15
15
  "Typing :: Typed",
16
16
  "Topic :: Software Development :: Libraries",
17
17
  ]
18
- dependencies = ["fastapi>=0.100", "modern-di>=2.0.0alpha1"]
19
- version = "2.0.0"
18
+ dependencies = ["fastapi>=0.100", "modern-di>=1.0.0alpha"]
19
+ version = "2.0.0a0"
20
20
 
21
21
  [project.urls]
22
22
  repository = "https://github.com/modern-python/modern-di"