modern-di-litestar 1.0.0a1__py3-none-any.whl → 2.0.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.
@@ -7,31 +7,41 @@ from litestar.config.app import AppConfig
7
7
  from litestar.di import Provide
8
8
  from litestar.params import Dependency
9
9
  from litestar.plugins import InitPlugin
10
- from modern_di import AsyncContainer, providers
11
- from modern_di import Scope as DIScope
10
+ from modern_di import Container, providers
11
+ from modern_di.scope import Scope
12
+ from modern_di.scope import Scope as DIScope
12
13
 
13
14
 
14
15
  T_co = typing.TypeVar("T_co", covariant=True)
15
16
 
16
17
 
17
- def fetch_di_container(app_: litestar.Litestar) -> AsyncContainer:
18
- return typing.cast(AsyncContainer, app_.state.di_container)
18
+ litestar_request = providers.ContextProvider(scope=Scope.REQUEST, context_type=litestar.Request)
19
+ litestar_websocket = providers.ContextProvider(scope=Scope.SESSION, context_type=litestar.WebSocket)
20
+
21
+
22
+ def fetch_di_container(app_: litestar.Litestar) -> Container:
23
+ return typing.cast(Container, app_.state.di_container)
19
24
 
20
25
 
21
26
  @contextlib.asynccontextmanager
22
27
  async def _lifespan_manager(app_: litestar.Litestar) -> typing.AsyncIterator[None]:
23
28
  container = fetch_di_container(app_)
24
- async with container:
29
+ try:
25
30
  yield
31
+ finally:
32
+ await container.close_async()
26
33
 
27
34
 
28
35
  class ModernDIPlugin(InitPlugin):
29
36
  __slots__ = ("container",)
30
37
 
31
- def __init__(self, container: AsyncContainer) -> None:
38
+ def __init__(self, container: Container) -> None:
32
39
  self.container = container
33
40
 
34
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
+ )
35
45
  app_config.state.di_container = self.container
36
46
  app_config.dependencies["di_container"] = Provide(build_di_container)
37
47
  app_config.lifespan.append(_lifespan_manager)
@@ -40,7 +50,7 @@ class ModernDIPlugin(InitPlugin):
40
50
 
41
51
  async def build_di_container(
42
52
  request: litestar.Request[typing.Any, typing.Any, typing.Any],
43
- ) -> typing.AsyncIterator[AsyncContainer]:
53
+ ) -> typing.AsyncIterator[Container]:
44
54
  context: dict[type[typing.Any], typing.Any] = {}
45
55
  scope: DIScope | None
46
56
  if isinstance(request, litestar.WebSocket):
@@ -49,9 +59,11 @@ async def build_di_container(
49
59
  else:
50
60
  context[litestar.Request] = request
51
61
  scope = DIScope.REQUEST
52
- container: AsyncContainer = fetch_di_container(request.app)
53
- async with container.build_child_container(context=context, scope=scope) as request_container:
54
- yield request_container
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()
55
67
 
56
68
 
57
69
  @dataclasses.dataclass(slots=True, frozen=True)
@@ -59,12 +71,11 @@ class _Dependency(typing.Generic[T_co]):
59
71
  dependency: providers.AbstractProvider[T_co] | type[T_co]
60
72
 
61
73
  async def __call__(
62
- self, di_container: typing.Annotated[AsyncContainer | None, Dependency(skip_validation=True)] = None
74
+ self, di_container: typing.Annotated[Container, Dependency(skip_validation=True)]
63
75
  ) -> T_co | None:
64
- assert di_container
65
76
  if isinstance(self.dependency, providers.AbstractProvider):
66
- return await di_container.resolve_provider(self.dependency)
67
- return await di_container.resolve(dependency_type=self.dependency)
77
+ return di_container.resolve_provider(self.dependency)
78
+ return di_container.resolve(dependency_type=self.dependency)
68
79
 
69
80
 
70
81
  def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co]) -> Provide: # noqa: N802
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: modern-di-litestar
3
- Version: 1.0.0a1
3
+ Version: 2.0.0
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
@@ -11,11 +11,12 @@ Classifier: Programming Language :: Python :: 3.10
11
11
  Classifier: Programming Language :: Python :: 3.11
12
12
  Classifier: Programming Language :: Python :: 3.12
13
13
  Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Programming Language :: Python :: 3.14
14
15
  Classifier: Topic :: Software Development :: Libraries
15
16
  Classifier: Typing :: Typed
16
17
  Requires-Python: <4,>=3.10
17
18
  Requires-Dist: litestar
18
- Requires-Dist: modern-di>=1.0.0alpha1
19
+ Requires-Dist: modern-di>=2.0.0alpha1
19
20
  Description-Content-Type: text/markdown
20
21
 
21
22
  "Modern-DI-LiteStar"
@@ -0,0 +1,6 @@
1
+ modern_di_litestar/__init__.py,sha256=9uFGF-DoOfFkMYBDC4HkrJfLkEwNDSKTgSIThCKPyjM,157
2
+ modern_di_litestar/main.py,sha256=6JfuoGjjt8O_moxkFywzKTwN7MCWeSswI2xe4SVpCXk,2808
3
+ modern_di_litestar/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ modern_di_litestar-2.0.0.dist-info/METADATA,sha256=bdrRLJKjFS_upz4uAGkJkhqQEhBMEME-vFYz11YLQxE,989
5
+ modern_di_litestar-2.0.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ modern_di_litestar-2.0.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.28.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,6 +0,0 @@
1
- modern_di_litestar/__init__.py,sha256=9uFGF-DoOfFkMYBDC4HkrJfLkEwNDSKTgSIThCKPyjM,157
2
- modern_di_litestar/main.py,sha256=1Lf8vqfv8IqP4xKeicIboLAt62aesypKaXXIx_MrBqI,2477
3
- modern_di_litestar/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- modern_di_litestar-1.0.0a1.dist-info/METADATA,sha256=5eqyBlYD8lnhWryKOJV6mSwLcNwNWY4-cd2RYPhfbBw,940
5
- modern_di_litestar-1.0.0a1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
- modern_di_litestar-1.0.0a1.dist-info/RECORD,,