Metadata-Version: 2.4
Name: modern-di-faststream
Version: 2.10.0
Summary: modern-di integration for FastStream
Keywords: dependency-injection,di,ioc-container,modern-di,faststream,python
Author: Artur Shiriev
Author-email: Artur Shiriev <me@shiriev.ru>
License-Expression: MIT
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Classifier: Topic :: Software Development :: Libraries
Requires-Dist: faststream>=0.7,<0.8
Requires-Dist: modern-di>=2.28,<3
Requires-Python: >=3.10, <4
Project-URL: Homepage, https://modern-di.modern-python.org
Project-URL: Documentation, https://modern-di.modern-python.org/integrations/faststream/
Project-URL: Repository, https://github.com/modern-python/modern-di-faststream
Project-URL: Issues, https://github.com/modern-python/modern-di-faststream/issues
Project-URL: Changelog, https://github.com/modern-python/modern-di-faststream/releases
Description-Content-Type: text/markdown

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)"  srcset="https://raw.githubusercontent.com/modern-python/.github/main/brand/projects/modern-di-faststream/lockup-dark.svg">
    <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/modern-python/.github/main/brand/projects/modern-di-faststream/lockup-light.svg">
    <img alt="modern-di-faststream" src="https://raw.githubusercontent.com/modern-python/.github/main/brand/projects/modern-di-faststream/lockup.png" width="420">
  </picture>
</p>

[![PyPI version](https://img.shields.io/pypi/v/modern-di-faststream.svg)](https://pypi.org/project/modern-di-faststream/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/modern-di-faststream.svg)](https://pypi.org/project/modern-di-faststream/)
[![Downloads](https://static.pepy.tech/badge/modern-di-faststream/month)](https://pepy.tech/projects/modern-di-faststream)
[![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](https://github.com/modern-python/modern-di-faststream/actions/workflows/ci.yml)
[![CI](https://github.com/modern-python/modern-di-faststream/actions/workflows/ci.yml/badge.svg)](https://github.com/modern-python/modern-di-faststream/actions/workflows/ci.yml)
[![License](https://img.shields.io/github/license/modern-python/modern-di-faststream.svg)](https://github.com/modern-python/modern-di-faststream/blob/main/LICENSE)
[![GitHub stars](https://img.shields.io/github/stars/modern-python/modern-di-faststream)](https://github.com/modern-python/modern-di-faststream/stargazers)
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![ty](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ty/main/assets/badge/v0.json)](https://github.com/astral-sh/ty)

[Modern-DI](https://github.com/modern-python/modern-di) integration for [FastStream](https://faststream.ag2.ai).

Full guide: [FastStream integration docs](https://modern-di.modern-python.org/integrations/faststream/)

## Installation

```bash
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.

```python
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_provider` — `ContextProvider` for the current `faststream.StreamMessage`

## 📦 [PyPI](https://pypi.org/project/modern-di-faststream)

## 📝 [License](LICENSE)

## Part of `modern-python`

Built on [`modern-di`](https://github.com/modern-python/modern-di), a dependency-injection framework with IoC container and scopes.

Browse the full list of templates and libraries in
[`modern-python`](https://github.com/modern-python) — see the org profile for the categorized index.
