Modern-DI integration for FastStream.
Full guide: FastStream integration docs
Usage example: examples/
Installation
uv add modern-di-faststream # or: pip install modern-di-faststream
Usage
setup_di registers the root container and installs a broker middleware that builds a request container per message; FromDI resolves a provider (or type) into a subscriber parameter.
import dataclasses
import faststream
from faststream.nats import NatsBroker
from modern_di import Container, Group, Scope, providers
from modern_di_faststream import FromDI, setup_di
@dataclasses.dataclass(kw_only=True)
class Settings:
debug: bool = True
@dataclasses.dataclass(kw_only=True)
class GreetingHandler:
settings: Settings # auto-injected by type
class Dependencies(Group):
settings = providers.Factory(scope=Scope.APP, creator=Settings)
handler = providers.Factory(scope=Scope.REQUEST, creator=GreetingHandler)
broker = NatsBroker()
app = faststream.FastStream(broker)
container = Container(groups=[Dependencies])
setup_di(app, container)
container.validate() # optional fail-fast; must come after setup_di registers its providers
@broker.subscriber("greetings")
async def handle(name: str, handler: GreetingHandler = FromDI(Dependencies.handler)) -> None:
print(name, handler.settings.debug)
The current StreamMessage is resolvable within DI via the pre-built faststream_message_provider context provider.
API
setup_di(app, container)— stores the container in the app context and registers startup/shutdown lifecycle hooks: on startup it reopens the container and adds the DI middleware to every broker of the app (including one added viaapp.add_brokeraftersetup_di, or by anon_startuphook registered beforesetup_di), after shutdown it closes the container. Raises at startup if the app has no broker by thenFromDI(dependency, *, use_cache=True, cast=False)— FastStreamDependsthat resolves a provider (or type) from the request container. RaisesRuntimeErrornamingsetup_diwhen a message reaches it without the middleware installedfetch_di_container(app)— returns the root container from the app contextfaststream_message_provider—ContextProviderfor the currentfaststream.StreamMessage
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with IoC container and scopes.
Browse the full list of templates and libraries in
modern-python — see the org profile for the categorized index.
Metadata
Release files for modern-di-faststream 3.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_faststream-3.0.3.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_faststream-3.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.6 kB
Release files / modern_di_faststream-3.0.3.tar.gz
| Download URL | modern_di_faststream-3.0.3.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
561a4f9d8d17d57f52a239092fba0024e81f19c03b646a0ca0ef9581ab228eb1
|
|
BLAKE2b-256 checksum How to use checksums |
8981977a122a307c8c467021f2db343a99b4550cccae2110feab95a1ba6a5637
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / modern_di_faststream-3.0.3-py3-none-any.whl
| Download URL | modern_di_faststream-3.0.3-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9689cf50fb165cb5ff7512752834ae674db2aea5f239290bd022e4da3f909e1a
|
|
BLAKE2b-256 checksum How to use checksums |
9c2c89e15fa9a8d9ab3b34ca6f7c300f3966b1091609d20ae096a0eede00e126
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|