Skip to main content

Modern-DI integration for FastStream

Project description

modern-di-faststream

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

Modern-DI integration for FastStream.

Installation

uv add modern-di-faststream      # or: pip install modern-di-faststream

Usage

setup_di registers the container and installs a broker middleware that builds a per-message child container; 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], validate=True)
setup_di(app, container)


@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, registers a shutdown hook, and adds the DI middleware to the broker
  • FromDI(dependency, *, use_cache=True, cast=False) — FastStream Depends that resolves a provider (or type) from the per-message child container
  • fetch_di_container(app) — returns the app-scoped container from the app context
  • faststream_message_providerContextProvider for the current faststream.StreamMessage

📦 PyPI

📝 License

Part of modern-python

Browse the full list of templates and libraries in modern-python — see the org profile for the categorized index.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modern_di_faststream-2.8.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modern_di_faststream-2.8.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_faststream-2.8.2.tar.gz.

File metadata

  • Download URL: modern_di_faststream-2.8.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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}

File hashes

Hashes for modern_di_faststream-2.8.2.tar.gz
Algorithm Hash digest
SHA256 916cebfa3739a0382585bd21638df0be94f38227608a07168d8d3fa98c1e5473
MD5 79b0ce2d6531f2c81e673b5b34c62347
BLAKE2b-256 60ce1efbc8dcb35c75ff1cb980ba8a587ef4d604eac3d990380654d016ccb54e

See more details on using hashes here.

File details

Details for the file modern_di_faststream-2.8.2-py3-none-any.whl.

File metadata

  • Download URL: modern_di_faststream-2.8.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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}

File hashes

Hashes for modern_di_faststream-2.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fd69b8495443c408485e8054a2c7585165cf3f0bf6660aec555be09458ce5cde
MD5 5c6d8937723280f9edc5fa260bf0cd71
BLAKE2b-256 2466972405bb2560ecaac5f8600bc0645f66d39624b0407a605369f38a51cacc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page