modern-di-litestar 1.1.0__py3-none-any.whl → 2.0.0a0__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.
@@ -1,4 +1,3 @@
1
- import contextlib
2
1
  import dataclasses
3
2
  import typing
4
3
 
@@ -7,40 +6,32 @@ from litestar.config.app import AppConfig
7
6
  from litestar.di import Provide
8
7
  from litestar.params import Dependency
9
8
  from litestar.plugins import InitPlugin
10
- from modern_di import AsyncContainer, providers
11
- from modern_di import Scope as DIScope
9
+ from modern_di import Container, providers
10
+ from modern_di.scope import Scope as DIScope
12
11
 
13
12
 
14
13
  T_co = typing.TypeVar("T_co", covariant=True)
15
14
 
16
15
 
17
- def fetch_di_container(app_: litestar.Litestar) -> AsyncContainer:
18
- return typing.cast(AsyncContainer, app_.state.di_container)
19
-
20
-
21
- @contextlib.asynccontextmanager
22
- async def _lifespan_manager(app_: litestar.Litestar) -> typing.AsyncIterator[None]:
23
- container = fetch_di_container(app_)
24
- async with container:
25
- yield
16
+ def fetch_di_container(app_: litestar.Litestar) -> Container:
17
+ return typing.cast(Container, app_.state.di_container)
26
18
 
27
19
 
28
20
  class ModernDIPlugin(InitPlugin):
29
21
  __slots__ = ("container",)
30
22
 
31
- def __init__(self, container: AsyncContainer) -> None:
23
+ def __init__(self, container: Container) -> None:
32
24
  self.container = container
33
25
 
34
26
  def on_app_init(self, app_config: AppConfig) -> AppConfig:
35
27
  app_config.state.di_container = self.container
36
- app_config.dependencies["di_container"] = Provide(build_di_container)
37
- app_config.lifespan.append(_lifespan_manager)
28
+ app_config.dependencies["di_container"] = Provide(build_di_container, sync_to_thread=False)
38
29
  return app_config
39
30
 
40
31
 
41
- async def build_di_container(
32
+ def build_di_container(
42
33
  request: litestar.Request[typing.Any, typing.Any, typing.Any],
43
- ) -> typing.AsyncIterator[AsyncContainer]:
34
+ ) -> Container:
44
35
  context: dict[type[typing.Any], typing.Any] = {}
45
36
  scope: DIScope | None
46
37
  if isinstance(request, litestar.WebSocket):
@@ -49,9 +40,7 @@ async def build_di_container(
49
40
  else:
50
41
  context[litestar.Request] = request
51
42
  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
43
+ return fetch_di_container(request.app).build_child_container(context=context, scope=scope)
55
44
 
56
45
 
57
46
  @dataclasses.dataclass(slots=True, frozen=True)
@@ -59,12 +48,11 @@ class _Dependency(typing.Generic[T_co]):
59
48
  dependency: providers.AbstractProvider[T_co] | type[T_co]
60
49
 
61
50
  async def __call__(
62
- self, di_container: typing.Annotated[AsyncContainer | None, Dependency(skip_validation=True)] = None
51
+ self, di_container: typing.Annotated[Container, Dependency(skip_validation=True)]
63
52
  ) -> T_co | None:
64
- assert di_container
65
53
  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)
54
+ return di_container.resolve_provider(self.dependency)
55
+ return di_container.resolve(dependency_type=self.dependency)
68
56
 
69
57
 
70
58
  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.1.0
3
+ Version: 2.0.0a0
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
@@ -0,0 +1,6 @@
1
+ modern_di_litestar/__init__.py,sha256=9uFGF-DoOfFkMYBDC4HkrJfLkEwNDSKTgSIThCKPyjM,157
2
+ modern_di_litestar/main.py,sha256=ZOVBp9tywNuhRGfwsHqaANMXuo5nO0dCRATVwv__dJ4,2022
3
+ modern_di_litestar/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ modern_di_litestar-2.0.0a0.dist-info/METADATA,sha256=DPtVkr-DkjN0AamQNuznkmeGKd-J9AZgoiMtI5QK0_A,991
5
+ modern_di_litestar-2.0.0a0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ modern_di_litestar-2.0.0a0.dist-info/RECORD,,
@@ -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.1.0.dist-info/METADATA,sha256=Ivm8hV4Psij5dCAqYYd3go3a2PxVbSf46AuDx9VOqac,989
5
- modern_di_litestar-1.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
- modern_di_litestar-1.1.0.dist-info/RECORD,,