modern-di-faststream 1.0.1__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,12 +7,16 @@ 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, Scope, providers
11
11
 
12
12
 
13
13
  T_co = typing.TypeVar("T_co", covariant=True)
14
14
  P = typing.ParamSpec("P")
15
15
 
16
+
17
+ faststream_message = providers.ContextProvider(scope=Scope.REQUEST, context_type=faststream.StreamMessage)
18
+
19
+
16
20
  _major, _minor, *_ = version("faststream").split(".")
17
21
  _OLD_MIDDLEWARES = int(_major) == 0 and int(_minor) < 6 # noqa: PLR2004
18
22
 
@@ -20,7 +24,7 @@ _OLD_MIDDLEWARES = int(_major) == 0 and int(_minor) < 6 # noqa: PLR2004
20
24
  class _DIMiddlewareFactory:
21
25
  __slots__ = ("di_container",)
22
26
 
23
- def __init__(self, di_container: AsyncContainer) -> None:
27
+ def __init__(self, di_container: Container) -> None:
24
28
  self.di_container = di_container
25
29
 
26
30
  def __call__(self, *args: P.args, **kwargs: P.kwargs) -> "_DiMiddleware[P]":
@@ -28,7 +32,7 @@ class _DIMiddlewareFactory:
28
32
 
29
33
 
30
34
  class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
31
- def __init__(self, di_container: AsyncContainer, *args: P.args, **kwargs: P.kwargs) -> None:
35
+ def __init__(self, di_container: Container, *args: P.args, **kwargs: P.kwargs) -> None:
32
36
  self.di_container = di_container
33
37
  super().__init__(*args, **kwargs) # type: ignore[arg-type]
34
38
 
@@ -37,14 +41,17 @@ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
37
41
  call_next: Callable[[typing.Any], Awaitable[typing.Any]],
38
42
  msg: faststream.StreamMessage[typing.Any],
39
43
  ) -> typing.AsyncIterator[DecodedMessage]:
40
- async with self.di_container.build_child_container(
44
+ request_container = self.di_container.build_child_container(
41
45
  scope=modern_di.Scope.REQUEST, context={faststream.StreamMessage: msg}
42
- ) as request_container:
46
+ )
47
+ try:
43
48
  with self.faststream_context.scope("request_container", request_container):
44
49
  return typing.cast(
45
50
  typing.AsyncIterator[DecodedMessage],
46
51
  await call_next(msg),
47
52
  )
53
+ finally:
54
+ await request_container.close_async()
48
55
 
49
56
  if _OLD_MIDDLEWARES: # pragma: no cover
50
57
 
@@ -59,21 +66,20 @@ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
59
66
  return self.context
60
67
 
61
68
 
62
- def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> AsyncContainer:
63
- return typing.cast(AsyncContainer, app_.context.get("di_container"))
69
+ def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> Container:
70
+ return typing.cast(Container, app_.context.get("di_container"))
64
71
 
65
72
 
66
73
  def setup_di(
67
74
  app: faststream.FastStream | AsgiFastStream,
68
- container: AsyncContainer,
69
- ) -> AsyncContainer:
75
+ container: Container,
76
+ ) -> Container:
70
77
  if not app.broker:
71
78
  msg = "Broker must be defined to setup DI"
72
79
  raise RuntimeError(msg)
73
80
 
81
+ container.providers_registry.add_providers(faststream_message=faststream_message)
74
82
  app.context.set_global("di_container", container)
75
- app.on_startup(container.enter)
76
- app.after_shutdown(container.close)
77
83
  app.broker.add_middleware(_DIMiddlewareFactory(container))
78
84
  return container
79
85
 
@@ -83,10 +89,10 @@ class Dependency(typing.Generic[T_co]):
83
89
  dependency: providers.AbstractProvider[T_co] | type[T_co]
84
90
 
85
91
  async def __call__(self, context: faststream.ContextRepo) -> T_co:
86
- request_container: modern_di.AsyncContainer = context.get("request_container")
92
+ request_container: Container = context.get("request_container")
87
93
  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)
94
+ return request_container.resolve_provider(self.dependency)
95
+ return request_container.resolve(dependency_type=self.dependency)
90
96
 
91
97
 
92
98
  def FromDI( # noqa: N802
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: modern-di-faststream
3
- Version: 1.0.1
3
+ Version: 2.0.0
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
@@ -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: faststream<1,>=0.5
19
- Requires-Dist: modern-di>=1.0.0alpha
19
+ Requires-Dist: modern-di>=2.0.0alpha1
20
20
  Description-Content-Type: text/markdown
21
21
 
22
22
  "Modern-DI-FastStream"
@@ -0,0 +1,6 @@
1
+ modern_di_faststream/__init__.py,sha256=ENQRyE_73MsxFjo608cUUb5EN1pFPC5HkS5Z3wZ5IC0,132
2
+ modern_di_faststream/main.py,sha256=LtvUtHAnzzb6vSTrZh5CZgQBmvcYQNJW5Z6cCxalazc,3601
3
+ modern_di_faststream/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ modern_di_faststream-2.0.0.dist-info/METADATA,sha256=tREqboiDtpiY6Kz4ji6KyuTyDLrxu4nwkmurD8CehPU,1009
5
+ modern_di_faststream-2.0.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ modern_di_faststream-2.0.0.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- modern_di_faststream/__init__.py,sha256=ENQRyE_73MsxFjo608cUUb5EN1pFPC5HkS5Z3wZ5IC0,132
2
- modern_di_faststream/main.py,sha256=-Sysi1rvq9mQvBNqvu3O0Lvn3EuDg2dCexOfRucRJZ4,3469
3
- modern_di_faststream/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- modern_di_faststream-1.0.1.dist-info/METADATA,sha256=ob52SZTD2oOCmbWFZ-iSkpPrxZDJYqrTCzFoaxj6SPs,1008
5
- modern_di_faststream-1.0.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
- modern_di_faststream-1.0.1.dist-info/RECORD,,