modern-di-faststream 0.5.2__py3-none-any.whl → 1.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.
@@ -1,5 +1,4 @@
1
1
  import dataclasses
2
- import enum
3
2
  import typing
4
3
  from collections.abc import Awaitable, Callable
5
4
  from importlib.metadata import version
@@ -8,7 +7,7 @@ import faststream
8
7
  import modern_di
9
8
  from faststream.asgi import AsgiFastStream
10
9
  from faststream.types import DecodedMessage
11
- from modern_di import Container, Scope, providers
10
+ from modern_di import AsyncContainer, providers
12
11
 
13
12
 
14
13
  T_co = typing.TypeVar("T_co", covariant=True)
@@ -21,7 +20,7 @@ _OLD_MIDDLEWARES = int(_major) == 0 and int(_minor) < 6 # noqa: PLR2004
21
20
  class _DIMiddlewareFactory:
22
21
  __slots__ = ("di_container",)
23
22
 
24
- def __init__(self, di_container: Container) -> None:
23
+ def __init__(self, di_container: AsyncContainer) -> None:
25
24
  self.di_container = di_container
26
25
 
27
26
  def __call__(self, *args: P.args, **kwargs: P.kwargs) -> "_DiMiddleware[P]":
@@ -29,7 +28,7 @@ class _DIMiddlewareFactory:
29
28
 
30
29
 
31
30
  class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
32
- def __init__(self, di_container: Container, *args: P.args, **kwargs: P.kwargs) -> None:
31
+ def __init__(self, di_container: AsyncContainer, *args: P.args, **kwargs: P.kwargs) -> None:
33
32
  self.di_container = di_container
34
33
  super().__init__(*args, **kwargs) # type: ignore[arg-type]
35
34
 
@@ -39,7 +38,7 @@ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
39
38
  msg: faststream.StreamMessage[typing.Any],
40
39
  ) -> typing.AsyncIterator[DecodedMessage]:
41
40
  async with self.di_container.build_child_container(
42
- scope=modern_di.Scope.REQUEST, context={"message": msg}
41
+ scope=modern_di.Scope.REQUEST, context={faststream.StreamMessage: msg}
43
42
  ) as request_container:
44
43
  with self.faststream_context.scope("request_container", request_container):
45
44
  return typing.cast(
@@ -53,41 +52,44 @@ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
53
52
  def faststream_context(self) -> faststream.ContextRepo:
54
53
  return typing.cast(faststream.ContextRepo, faststream.context) # type: ignore[attr-defined]
55
54
 
56
- else: # pragma: no cover
55
+ else:
57
56
 
58
57
  @property
59
58
  def faststream_context(self) -> faststream.ContextRepo:
60
59
  return self.context
61
60
 
62
61
 
63
- def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> Container:
64
- return typing.cast(Container, app_.context.get("di_container"))
62
+ def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> AsyncContainer:
63
+ return typing.cast(AsyncContainer, app_.context.get("di_container"))
65
64
 
66
65
 
67
66
  def setup_di(
68
- app: faststream.FastStream | AsgiFastStream, scope: enum.IntEnum = Scope.APP, container: Container | None = None
69
- ) -> Container:
67
+ app: faststream.FastStream | AsgiFastStream,
68
+ container: AsyncContainer,
69
+ ) -> AsyncContainer:
70
70
  if not app.broker:
71
71
  msg = "Broker must be defined to setup DI"
72
72
  raise RuntimeError(msg)
73
73
 
74
- if not container:
75
- container = Container(scope=scope)
76
74
  app.context.set_global("di_container", container)
77
- app.on_startup(container.async_enter)
78
- app.after_shutdown(container.async_close)
75
+ app.on_startup(container.enter)
76
+ app.after_shutdown(container.close)
79
77
  app.broker.add_middleware(_DIMiddlewareFactory(container))
80
78
  return container
81
79
 
82
80
 
83
81
  @dataclasses.dataclass(slots=True, frozen=True)
84
82
  class Dependency(typing.Generic[T_co]):
85
- dependency: providers.AbstractProvider[T_co]
83
+ dependency: providers.AbstractProvider[T_co] | type[T_co]
86
84
 
87
85
  async def __call__(self, context: faststream.ContextRepo) -> T_co:
88
- request_container: modern_di.Container = context.get("request_container")
89
- return await self.dependency.async_resolve(request_container)
86
+ request_container: modern_di.AsyncContainer = context.get("request_container")
87
+ 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)
90
90
 
91
91
 
92
- def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True, cast: bool = False) -> T_co: # noqa: N802
92
+ def FromDI( # noqa: N802
93
+ dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True, cast: bool = False
94
+ ) -> T_co:
93
95
  return typing.cast(T_co, faststream.Depends(dependency=Dependency(dependency), use_cache=use_cache, cast=cast))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: modern-di-faststream
3
- Version: 0.5.2
3
+ Version: 1.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
@@ -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: faststream<1,>=0.5
18
- Requires-Dist: modern-di>=0.13.0
19
+ Requires-Dist: modern-di>=1.0.0alpha
19
20
  Description-Content-Type: text/markdown
20
21
 
21
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=-Sysi1rvq9mQvBNqvu3O0Lvn3EuDg2dCexOfRucRJZ4,3469
3
+ modern_di_faststream/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ modern_di_faststream-1.0.0.dist-info/METADATA,sha256=jNQ1aJEHuaKGmv57uTIdTsoA0LQQlp6OqnYCo0q-few,1008
5
+ modern_di_faststream-1.0.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ modern_di_faststream-1.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_faststream/__init__.py,sha256=ENQRyE_73MsxFjo608cUUb5EN1pFPC5HkS5Z3wZ5IC0,132
2
- modern_di_faststream/main.py,sha256=fbdpzBVedcrCi7iUZOGqTd-nwhJwbWg3gcJVcrpPKxI,3385
3
- modern_di_faststream/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- modern_di_faststream-0.5.2.dist-info/METADATA,sha256=bhr7r0Yxofg1tPe8Pid0eXk0vStw8aWuc3JRspPvS4s,953
5
- modern_di_faststream-0.5.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
- modern_di_faststream-0.5.2.dist-info/RECORD,,