modern-di-faststream 1.0.0a5__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,95 @@
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 AsyncContainer, providers
11
+
12
+
13
+ T_co = typing.TypeVar("T_co", covariant=True)
14
+ P = typing.ParamSpec("P")
15
+
16
+ _major, _minor, *_ = version("faststream").split(".")
17
+ _OLD_MIDDLEWARES = int(_major) == 0 and int(_minor) < 6 # noqa: PLR2004
18
+
19
+
20
+ class _DIMiddlewareFactory:
21
+ __slots__ = ("di_container",)
22
+
23
+ def __init__(self, di_container: AsyncContainer) -> None:
24
+ self.di_container = di_container
25
+
26
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> "_DiMiddleware[P]":
27
+ return _DiMiddleware(self.di_container, *args, **kwargs)
28
+
29
+
30
+ class _DiMiddleware(faststream.BaseMiddleware, typing.Generic[P]):
31
+ def __init__(self, di_container: AsyncContainer, *args: P.args, **kwargs: P.kwargs) -> None:
32
+ self.di_container = di_container
33
+ super().__init__(*args, **kwargs) # type: ignore[arg-type]
34
+
35
+ async def consume_scope(
36
+ self,
37
+ call_next: Callable[[typing.Any], Awaitable[typing.Any]],
38
+ msg: faststream.StreamMessage[typing.Any],
39
+ ) -> typing.AsyncIterator[DecodedMessage]:
40
+ async with self.di_container.build_child_container(
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
+ )
48
+
49
+ if _OLD_MIDDLEWARES: # pragma: no cover
50
+
51
+ @property
52
+ def faststream_context(self) -> faststream.ContextRepo:
53
+ return typing.cast(faststream.ContextRepo, faststream.context) # type: ignore[attr-defined]
54
+
55
+ else:
56
+
57
+ @property
58
+ def faststream_context(self) -> faststream.ContextRepo:
59
+ return self.context
60
+
61
+
62
+ def fetch_di_container(app_: faststream.FastStream | AsgiFastStream) -> AsyncContainer:
63
+ return typing.cast(AsyncContainer, app_.context.get("di_container"))
64
+
65
+
66
+ def setup_di(
67
+ app: faststream.FastStream | AsgiFastStream,
68
+ container: AsyncContainer,
69
+ ) -> AsyncContainer:
70
+ if not app.broker:
71
+ msg = "Broker must be defined to setup DI"
72
+ raise RuntimeError(msg)
73
+
74
+ app.context.set_global("di_container", container)
75
+ app.on_startup(container.enter)
76
+ app.after_shutdown(container.close)
77
+ app.broker.add_middleware(_DIMiddlewareFactory(container))
78
+ return container
79
+
80
+
81
+ @dataclasses.dataclass(slots=True, frozen=True)
82
+ class Dependency(typing.Generic[T_co]):
83
+ dependency: providers.AbstractProvider[T_co] | type[T_co]
84
+
85
+ async def __call__(self, context: faststream.ContextRepo) -> T_co:
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
+
91
+
92
+ def FromDI( # noqa: N802
93
+ dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True, cast: bool = False
94
+ ) -> T_co:
95
+ return typing.cast(T_co, faststream.Depends(dependency=Dependency(dependency), use_cache=use_cache, cast=cast))
File without changes
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: modern-di-faststream
3
+ Version: 1.0.0a5
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: Topic :: Software Development :: Libraries
15
+ Classifier: Typing :: Typed
16
+ Requires-Python: <4,>=3.10
17
+ Requires-Dist: faststream<1,>=0.5
18
+ Requires-Dist: modern-di>=1.0.0alpha
19
+ Description-Content-Type: text/markdown
20
+
21
+ "Modern-DI-FastStream"
22
+ ==
23
+
24
+ Integration of [Modern-DI](https://github.com/modern-python/modern-di) to FastStream
25
+
26
+ 📚 [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=-Sysi1rvq9mQvBNqvu3O0Lvn3EuDg2dCexOfRucRJZ4,3469
3
+ modern_di_faststream/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ modern_di_faststream-1.0.0a5.dist-info/METADATA,sha256=zJSSjmE9CFEflb3xf9McTFPV0JkPsfjXaG7SljNiurw,959
5
+ modern_di_faststream-1.0.0a5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ modern_di_faststream-1.0.0a5.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any