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.

Full guide: FastStream integration docs

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 startup/shutdown lifecycle hooks (reopen on startup, close after shutdown), 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

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.

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.10.0.tar.gz (4.4 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.10.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_faststream-2.10.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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.10.0.tar.gz
Algorithm Hash digest
SHA256 2a124aac8b2a9d30ec1da2732daeac85f3c824bbe661d6caa94f80ae0c2038ae
MD5 4fe7f3fd65067a69625cbaa84ab57673
BLAKE2b-256 883cbb8c7f0030b700f902481ec8ed431ff5b5ca0fe0fab4f566b733230ae569

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_faststream-2.10.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8c78613dde61e9a3c1bed1cdfc49a007cd1c736eac5ab26015d284649478e1b
MD5 a699bdc01833f71adc02aebe5c9ed184
BLAKE2b-256 a8392489b7f514b75581dfee771857ac006714e818082e484c73ceaea3b8c46b

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