modern-di-faststream 1.1.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-faststream
3
- Version: 1.1.0
3
+ Version: 2.0.0a0
4
4
  Summary: Modern-DI integration for FastStream
5
5
  Project-URL: repository, https://github.com/modern-python/modern-di
6
6
  Project-URL: docs, https://modern-di.readthedocs.io
@@ -7,7 +7,7 @@ import faststream
7
7
  import modern_di
8
8
  from faststream.asgi import AsgiFastStream
9
9
  from faststream.types import DecodedMessage
10
- from modern_di import AsyncContainer, providers
10
+ from modern_di import Container, providers
11
11
 
12
12
 
13
13
  T_co = typing.TypeVar("T_co", covariant=True)
@@ -20,7 +20,7 @@ _OLD_MIDDLEWARES = int(_major) == 0 and int(_minor) < 6 # noqa: PLR2004
20
20
  class _DIMiddlewareFactory:
21
21
  __slots__ = ("di_container",)
22
22
 
23
- def __init__(self, di_container: AsyncContainer) -> None:
23
+ def __init__(self, di_container: Container) -> None:
24
24
  self.di_container = di_container
25
25
 
26
26
  def __call__(self, *args: P.args, **kwargs: P.kwargs) -> "_DiMiddleware[P]":
@@ -28,7 +28,7 @@ class _DIMiddlewareFactory:
28
28
 
29
29
 
30
30
  class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
31
- def __init__(self, di_container: AsyncContainer, *args: P.args, **kwargs: P.kwargs) -> None:
31
+ def __init__(self, di_container: Container, *args: P.args, **kwargs: P.kwargs) -> None:
32
32
  self.di_container = di_container
33
33
  super().__init__(*args, **kwargs) # type: ignore[arg-type]
34
34
 
@@ -37,14 +37,14 @@ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
37
37
  call_next: Callable[[typing.Any], Awaitable[typing.Any]],
38
38
  msg: faststream.StreamMessage[typing.Any],
39
39
  ) -> typing.AsyncIterator[DecodedMessage]:
40
- async with self.di_container.build_child_container(
40
+ request_container = self.di_container.build_child_container(
41
41
  scope=modern_di.Scope.REQUEST, context={faststream.StreamMessage: msg}
42
- ) as request_container:
43
- with self.faststream_context.scope("request_container", request_container):
44
- return typing.cast(
45
- typing.AsyncIterator[DecodedMessage],
46
- await call_next(msg),
47
- )
42
+ )
43
+ with self.faststream_context.scope("request_container", request_container):
44
+ return typing.cast(
45
+ typing.AsyncIterator[DecodedMessage],
46
+ await call_next(msg),
47
+ )
48
48
 
49
49
  if _OLD_MIDDLEWARES: # pragma: no cover
50
50
 
@@ -59,21 +59,19 @@ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
59
59
  return self.context
60
60
 
61
61
 
62
- def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> AsyncContainer:
63
- return typing.cast(AsyncContainer, app_.context.get("di_container"))
62
+ def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> Container:
63
+ return typing.cast(Container, app_.context.get("di_container"))
64
64
 
65
65
 
66
66
  def setup_di(
67
67
  app: faststream.FastStream | AsgiFastStream,
68
- container: AsyncContainer,
69
- ) -> AsyncContainer:
68
+ container: Container,
69
+ ) -> Container:
70
70
  if not app.broker:
71
71
  msg = "Broker must be defined to setup DI"
72
72
  raise RuntimeError(msg)
73
73
 
74
74
  app.context.set_global("di_container", container)
75
- app.on_startup(container.enter)
76
- app.after_shutdown(container.close)
77
75
  app.broker.add_middleware(_DIMiddlewareFactory(container))
78
76
  return container
79
77
 
@@ -83,10 +81,10 @@ class Dependency(typing.Generic[T_co]):
83
81
  dependency: providers.AbstractProvider[T_co] | type[T_co]
84
82
 
85
83
  async def __call__(self, context: faststream.ContextRepo) -> T_co:
86
- request_container: modern_di.AsyncContainer = context.get("request_container")
84
+ request_container: Container = context.get("request_container")
87
85
  if isinstance(self.dependency, providers.AbstractProvider):
88
- return await request_container.resolve_provider(self.dependency)
89
- return await request_container.resolve(dependency_type=self.dependency)
86
+ return request_container.resolve_provider(self.dependency)
87
+ return request_container.resolve(dependency_type=self.dependency)
90
88
 
91
89
 
92
90
  def FromDI( # noqa: N802
@@ -16,7 +16,7 @@ classifiers = [
16
16
  "Topic :: Software Development :: Libraries",
17
17
  ]
18
18
  dependencies = ["faststream>=0.5,<1", "modern-di>=1.0.0alpha"]
19
- version = "1.1.0"
19
+ version = "2.0.0a0"
20
20
 
21
21
  [project.urls]
22
22
  repository = "https://github.com/modern-python/modern-di"