modern-di-litestar 2.0.0a0__tar.gz → 2.0.0a1__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-litestar
3
- Version: 2.0.0a0
3
+ Version: 2.0.0a1
4
4
  Summary: Modern-DI integration for LiteStar
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: litestar
19
- Requires-Dist: modern-di>=1.0.0alpha1
19
+ Requires-Dist: modern-di>=2.0.0alpha1
20
20
  Description-Content-Type: text/markdown
21
21
 
22
22
  "Modern-DI-LiteStar"
@@ -1,3 +1,4 @@
1
+ import contextlib
1
2
  import dataclasses
2
3
  import typing
3
4
 
@@ -7,16 +8,30 @@ from litestar.di import Provide
7
8
  from litestar.params import Dependency
8
9
  from litestar.plugins import InitPlugin
9
10
  from modern_di import Container, providers
11
+ from modern_di.scope import Scope
10
12
  from modern_di.scope import Scope as DIScope
11
13
 
12
14
 
13
15
  T_co = typing.TypeVar("T_co", covariant=True)
14
16
 
15
17
 
18
+ litestar_request = providers.ContextProvider(scope=Scope.REQUEST, context_type=litestar.Request)
19
+ litestar_websocket = providers.ContextProvider(scope=Scope.REQUEST, context_type=litestar.WebSocket)
20
+
21
+
16
22
  def fetch_di_container(app_: litestar.Litestar) -> Container:
17
23
  return typing.cast(Container, app_.state.di_container)
18
24
 
19
25
 
26
+ @contextlib.asynccontextmanager
27
+ async def _lifespan_manager(app_: litestar.Litestar) -> typing.AsyncIterator[None]:
28
+ container = fetch_di_container(app_)
29
+ try:
30
+ yield
31
+ finally:
32
+ await container.close_async()
33
+
34
+
20
35
  class ModernDIPlugin(InitPlugin):
21
36
  __slots__ = ("container",)
22
37
 
@@ -24,14 +39,18 @@ class ModernDIPlugin(InitPlugin):
24
39
  self.container = container
25
40
 
26
41
  def on_app_init(self, app_config: AppConfig) -> AppConfig:
42
+ self.container.providers_registry.add_providers(
43
+ litestar_request=litestar_request, litestar_websocket=litestar_websocket
44
+ )
27
45
  app_config.state.di_container = self.container
28
- app_config.dependencies["di_container"] = Provide(build_di_container, sync_to_thread=False)
46
+ app_config.dependencies["di_container"] = Provide(build_di_container)
47
+ app_config.lifespan.append(_lifespan_manager)
29
48
  return app_config
30
49
 
31
50
 
32
- def build_di_container(
51
+ async def build_di_container(
33
52
  request: litestar.Request[typing.Any, typing.Any, typing.Any],
34
- ) -> Container:
53
+ ) -> typing.AsyncIterator[Container]:
35
54
  context: dict[type[typing.Any], typing.Any] = {}
36
55
  scope: DIScope | None
37
56
  if isinstance(request, litestar.WebSocket):
@@ -40,7 +59,11 @@ def build_di_container(
40
59
  else:
41
60
  context[litestar.Request] = request
42
61
  scope = DIScope.REQUEST
43
- return fetch_di_container(request.app).build_child_container(context=context, scope=scope)
62
+ container = fetch_di_container(request.app).build_child_container(context=context, scope=scope)
63
+ try:
64
+ yield container
65
+ finally:
66
+ await container.close_async()
44
67
 
45
68
 
46
69
  @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 = ["litestar", "modern-di>=1.0.0alpha1"]
19
- version = "2.0.0a0"
18
+ dependencies = ["litestar", "modern-di>=2.0.0alpha1"]
19
+ version = "2.0.0a1"
20
20
 
21
21
  [project.urls]
22
22
  repository = "https://github.com/modern-python/modern-di"