modern-di-faststream 2.0.0a3__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-2.0.0a3/.gitignore +22 -0
- modern_di_faststream-2.0.0a3/PKG-INFO +27 -0
- modern_di_faststream-2.0.0a3/README.md +6 -0
- modern_di_faststream-2.0.0a3/modern_di_faststream/__init__.py +4 -0
- modern_di_faststream-2.0.0a3/modern_di_faststream/main.py +101 -0
- modern_di_faststream-2.0.0a3/modern_di_faststream/py.typed +0 -0
- modern_di_faststream-2.0.0a3/pyproject.toml +51 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Generic things
|
|
2
|
+
*.pyc
|
|
3
|
+
*~
|
|
4
|
+
__pycache__/*
|
|
5
|
+
*.swp
|
|
6
|
+
*.sqlite3
|
|
7
|
+
*.map
|
|
8
|
+
.vscode
|
|
9
|
+
.idea
|
|
10
|
+
.DS_Store
|
|
11
|
+
.env
|
|
12
|
+
.mypy_cache
|
|
13
|
+
.pytest_cache
|
|
14
|
+
.ruff_cache
|
|
15
|
+
.coverage
|
|
16
|
+
htmlcov/
|
|
17
|
+
coverage.xml
|
|
18
|
+
pytest.xml
|
|
19
|
+
dist/
|
|
20
|
+
.python-version
|
|
21
|
+
.venv
|
|
22
|
+
uv.lock
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: modern-di-faststream
|
|
3
|
+
Version: 2.0.0a3
|
|
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,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,51 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "modern-di-faststream"
|
|
3
|
+
description = "Modern-DI integration for FastStream"
|
|
4
|
+
authors = [{ name = "Artur Shiriev", email = "me@shiriev.ru" }]
|
|
5
|
+
requires-python = ">=3.10,<4"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
keywords = ["DI", "dependency injector", "ioc-container", "FastStream", "python"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Programming Language :: Python :: 3.10",
|
|
11
|
+
"Programming Language :: Python :: 3.11",
|
|
12
|
+
"Programming Language :: Python :: 3.12",
|
|
13
|
+
"Programming Language :: Python :: 3.13",
|
|
14
|
+
"Programming Language :: Python :: 3.14",
|
|
15
|
+
"Typing :: Typed",
|
|
16
|
+
"Topic :: Software Development :: Libraries",
|
|
17
|
+
]
|
|
18
|
+
dependencies = ["faststream>=0.5,<1", "modern-di>=2.0.0alpha1"]
|
|
19
|
+
version = "2.0.0a3"
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
repository = "https://github.com/modern-python/modern-di"
|
|
23
|
+
docs = "https://modern-di.readthedocs.io"
|
|
24
|
+
|
|
25
|
+
[dependency-groups]
|
|
26
|
+
dev = [
|
|
27
|
+
"faststream[nats]",
|
|
28
|
+
"pytest",
|
|
29
|
+
"pytest-cov",
|
|
30
|
+
"pytest-asyncio",
|
|
31
|
+
"ruff",
|
|
32
|
+
"mypy",
|
|
33
|
+
"typing-extensions",
|
|
34
|
+
"httpx",
|
|
35
|
+
"asgi-lifespan",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[build-system]
|
|
39
|
+
requires = ["hatchling"]
|
|
40
|
+
build-backend = "hatchling.build"
|
|
41
|
+
|
|
42
|
+
[tool.hatch.build]
|
|
43
|
+
include = ["modern_di_faststream"]
|
|
44
|
+
|
|
45
|
+
[tool.pytest.ini_options]
|
|
46
|
+
addopts = "--cov=. --cov-report term-missing"
|
|
47
|
+
asyncio_mode = "auto"
|
|
48
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
49
|
+
|
|
50
|
+
[tool.coverage.report]
|
|
51
|
+
exclude_also = ["if typing.TYPE_CHECKING:"]
|