modern-di-faststream 1.1.0__tar.gz → 2.0.0__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.
- {modern_di_faststream-1.1.0 → modern_di_faststream-2.0.0}/PKG-INFO +2 -2
- {modern_di_faststream-1.1.0 → modern_di_faststream-2.0.0}/modern_di_faststream/main.py +20 -14
- {modern_di_faststream-1.1.0 → modern_di_faststream-2.0.0}/pyproject.toml +2 -2
- {modern_di_faststream-1.1.0 → modern_di_faststream-2.0.0}/.gitignore +0 -0
- {modern_di_faststream-1.1.0 → modern_di_faststream-2.0.0}/README.md +0 -0
- {modern_di_faststream-1.1.0 → modern_di_faststream-2.0.0}/modern_di_faststream/__init__.py +0 -0
- {modern_di_faststream-1.1.0 → modern_di_faststream-2.0.0}/modern_di_faststream/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: modern-di-faststream
|
|
3
|
-
Version:
|
|
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>=
|
|
19
|
+
Requires-Dist: modern-di>=2.0.0alpha1
|
|
20
20
|
Description-Content-Type: text/markdown
|
|
21
21
|
|
|
22
22
|
"Modern-DI-FastStream"
|
|
@@ -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
|
|
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:
|
|
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:
|
|
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
|
-
|
|
44
|
+
request_container = self.di_container.build_child_container(
|
|
41
45
|
scope=modern_di.Scope.REQUEST, context={faststream.StreamMessage: msg}
|
|
42
|
-
)
|
|
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) ->
|
|
63
|
-
return typing.cast(
|
|
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:
|
|
69
|
-
) ->
|
|
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:
|
|
92
|
+
request_container: Container = context.get("request_container")
|
|
87
93
|
if isinstance(self.dependency, providers.AbstractProvider):
|
|
88
|
-
return
|
|
89
|
-
return
|
|
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
|
|
@@ -15,8 +15,8 @@ classifiers = [
|
|
|
15
15
|
"Typing :: Typed",
|
|
16
16
|
"Topic :: Software Development :: Libraries",
|
|
17
17
|
]
|
|
18
|
-
dependencies = ["faststream>=0.5,<1", "modern-di>=
|
|
19
|
-
version = "
|
|
18
|
+
dependencies = ["faststream>=0.5,<1", "modern-di>=2.0.0alpha1"]
|
|
19
|
+
version = "2.0.0"
|
|
20
20
|
|
|
21
21
|
[project.urls]
|
|
22
22
|
repository = "https://github.com/modern-python/modern-di"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|