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 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.9.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.9.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_faststream-2.9.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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.9.0.tar.gz
Algorithm Hash digest
SHA256 9c2d0b4bd482643d1c4b2d6e6cb8d7544d340e4a1b3dd3c53ff5069627be4679
MD5 95f86740deb025bd301f2e8bd279f9e8
BLAKE2b-256 07d0a8f69d9a0bed520311c2d325f6e64065bafa47eed5785a1f15211595214f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_faststream-2.9.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5587f49bf703732159f55a3efadd7c9e00c34688cd3d3279dc7a7dc5a5dd4040
MD5 584e861a66f6db3a51925731ffecd20a
BLAKE2b-256 6f1441bbc86a10e10cc49108076c9761ba9e70bd18ae1a2f49e438cacc09d732

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