modern-di-faststream 2.0.0a4__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.
@@ -0,0 +1,4 @@
1
+ from modern_di_faststream.main import FromDI, fetch_di_container, setup_di
2
+
3
+
4
+ __all__ = ["FromDI", "fetch_di_container", "setup_di"]
@@ -0,0 +1,101 @@
1
+ import dataclasses
2
+ import typing
3
+ from collections.abc import Awaitable, Callable
4
+ from importlib.metadata import version
5
+
6
+ import faststream
7
+ import modern_di
8
+ from faststream.asgi import AsgiFastStream
9
+ from faststream.types import DecodedMessage
10
+ from modern_di import Container, Scope, providers
11
+
12
+
13
+ T_co = typing.TypeVar("T_co", covariant=True)
14
+ P = typing.ParamSpec("P")
15
+
16
+
17
+ faststream_message = providers.ContextProvider(scope=Scope.REQUEST, context_type=faststream.StreamMessage)
18
+
19
+
20
+ _major, _minor, *_ = version("faststream").split(".")
21
+ _OLD_MIDDLEWARES = int(_major) == 0 and int(_minor) < 6 # noqa: PLR2004
22
+
23
+
24
+ class _DIMiddlewareFactory:
25
+ __slots__ = ("di_container",)
26
+
27
+ def __init__(self, di_container: Container) -> None:
28
+ self.di_container = di_container
29
+
30
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> "_DiMiddleware[P]":
31
+ return _DiMiddleware(self.di_container, *args, **kwargs)
32
+
33
+
34
+ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
35
+ def __init__(self, di_container: Container, *args: P.args, **kwargs: P.kwargs) -> None:
36
+ self.di_container = di_container
37
+ super().__init__(*args, **kwargs) # type: ignore[arg-type]
38
+
39
+ async def consume_scope(
40
+ self,
41
+ call_next: Callable[[typing.Any], Awaitable[typing.Any]],
42
+ msg: faststream.StreamMessage[typing.Any],
43
+ ) -> typing.AsyncIterator[DecodedMessage]:
44
+ request_container = self.di_container.build_child_container(
45
+ scope=modern_di.Scope.REQUEST, context={faststream.StreamMessage: msg}
46
+ )
47
+ try:
48
+ with self.faststream_context.scope("request_container", request_container):
49
+ return typing.cast(
50
+ typing.AsyncIterator[DecodedMessage],
51
+ await call_next(msg),
52
+ )
53
+ finally:
54
+ await request_container.close_async()
55
+
56
+ if _OLD_MIDDLEWARES: # pragma: no cover
57
+
58
+ @property
59
+ def faststream_context(self) -> faststream.ContextRepo:
60
+ return typing.cast(faststream.ContextRepo, faststream.context) # type: ignore[attr-defined]
61
+
62
+ else:
63
+
64
+ @property
65
+ def faststream_context(self) -> faststream.ContextRepo:
66
+ return self.context
67
+
68
+
69
+ def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> Container:
70
+ return typing.cast(Container, app_.context.get("di_container"))
71
+
72
+
73
+ def setup_di(
74
+ app: faststream.FastStream | AsgiFastStream,
75
+ container: Container,
76
+ ) -> Container:
77
+ if not app.broker:
78
+ msg = "Broker must be defined to setup DI"
79
+ raise RuntimeError(msg)
80
+
81
+ container.providers_registry.add_providers(faststream_message=faststream_message)
82
+ app.context.set_global("di_container", container)
83
+ app.broker.add_middleware(_DIMiddlewareFactory(container))
84
+ return container
85
+
86
+
87
+ @dataclasses.dataclass(slots=True, frozen=True)
88
+ class Dependency(typing.Generic[T_co]):
89
+ dependency: providers.AbstractProvider[T_co] | type[T_co]
90
+
91
+ async def __call__(self, context: faststream.ContextRepo) -> T_co:
92
+ request_container: Container = context.get("request_container")
93
+ if isinstance(self.dependency, providers.AbstractProvider):
94
+ return request_container.resolve_provider(self.dependency)
95
+ return request_container.resolve(dependency_type=self.dependency)
96
+
97
+
98
+ def FromDI( # noqa: N802
99
+ dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True, cast: bool = False
100
+ ) -> T_co:
101
+ return typing.cast(T_co, faststream.Depends(dependency=Dependency(dependency), use_cache=use_cache, cast=cast))
File without changes
@@ -0,0 +1,27 @@
1
+ Metadata-Version: 2.4
2
+ Name: modern-di-faststream
3
+ Version: 2.0.0a4
4
+ Summary: Modern-DI integration for FastStream
5
+ Project-URL: repository, https://github.com/modern-python/modern-di
6
+ Project-URL: docs, https://modern-di.readthedocs.io
7
+ Author-email: Artur Shiriev <me@shiriev.ru>
8
+ License-Expression: MIT
9
+ Keywords: DI,FastStream,dependency injector,ioc-container,python
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Programming Language :: Python :: 3.14
15
+ Classifier: Topic :: Software Development :: Libraries
16
+ Classifier: Typing :: Typed
17
+ Requires-Python: <4,>=3.10
18
+ Requires-Dist: faststream<1,>=0.5
19
+ Requires-Dist: modern-di>=2.0.0alpha1
20
+ Description-Content-Type: text/markdown
21
+
22
+ "Modern-DI-FastStream"
23
+ ==
24
+
25
+ Integration of [Modern-DI](https://github.com/modern-python/modern-di) to FastStream
26
+
27
+ 📚 [Documentation](https://modern-di.readthedocs.io)
@@ -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.0a4.dist-info/METADATA,sha256=pvC9NqJzbdMj8oMKmN4-yezK1u-8EGM9ZE0BraAVBcY,1011
5
+ modern_di_faststream-2.0.0a4.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ modern_di_faststream-2.0.0a4.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any