https://github.com/modern-python/modern-di
Powerful dependency-injection framework with IoC container and scopes
https://github.com/modern-python/modern-di
asyncio dependency-injection di ioc ioc-container mocks modern-di python scopes
Last synced: 18 days ago
JSON representation
Powerful dependency-injection framework with IoC container and scopes
- Host: GitHub
- URL: https://github.com/modern-python/modern-di
- Owner: modern-python
- License: mit
- Created: 2024-10-27T18:08:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-06-30T16:27:23.000Z (19 days ago)
- Last Synced: 2026-06-30T16:27:50.963Z (19 days ago)
- Topics: asyncio, dependency-injection, di, ioc, ioc-container, mocks, modern-di, python, scopes
- Language: Python
- Homepage: https://modern-di.modern-python.org
- Size: 1.33 MB
- Stars: 57
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Roadmap: ROADMAP.md
Awesome Lists containing this project
- awesome-dependency-injection-in-python - Modern DI - powerful DI-framework with scopes and IoC-container [🐍, MIT License]. (Software / DI Frameworks / Containers)
- awesome-fastapi - modern-di - Dependency injection framework with IoC container and scopes, with a [FastAPI integration](https://github.com/modern-python/modern-di-fastapi). (Third-Party Extensions / Dependency Injection)
- awesome-litestar - `modern-di` - Dependency injection framework with IoC container and scopes, including [Litestar integration](https://github.com/modern-python/modern-di-litestar). (Third-Party Extensions / General)
README
[](https://github.com/modern-python/modern-di/stargazers)
[](https://context7.com/modern-python/modern-di)
[](https://github.com/astral-sh/uv)
[](https://github.com/astral-sh/ruff)
[](https://github.com/astral-sh/ty)
| Project | Badges |
|---------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| modern-di | [](https://pypi.python.org/pypi/modern-di ) [](https://pepy.tech/projects/modern-di) |
| [modern-di-fastapi](https://github.com/modern-python/modern-di-fastapi) | [](https://pypi.python.org/pypi/modern-di-fastapi) [](https://pepy.tech/projects/modern-di-fastapi) |
| [modern-di-faststream ](https://github.com/modern-python/modern-di-faststream) | [](https://pypi.python.org/pypi/modern-di-faststream) [](https://pepy.tech/projects/modern-di-faststream) |
| [modern-di-litestar ](https://github.com/modern-python/modern-di-litestar) | [](https://pypi.python.org/pypi/modern-di-litestar) [](https://pepy.tech/projects/modern-di-litestar) |
| [modern-di-typer](https://github.com/modern-python/modern-di-typer) | [](https://pypi.python.org/pypi/modern-di-typer) [](https://pepy.tech/projects/modern-di-typer) |
| [modern-di-pytest](https://github.com/modern-python/modern-di-pytest) | [](https://pypi.python.org/pypi/modern-di-pytest) [](https://pepy.tech/projects/modern-di-pytest) |
`modern-di` is a python dependency injection framework which supports the following:
- Automatic dependency graph based on type annotations
- Also, explicit dependencies are allowed where needed
- Scopes and context management
- Python 3.10+ support
- Fully typed and tested
- Integrations with `FastAPI`, `FastStream`, `LiteStar` and `Typer`
- Pytest integration (`modern-di-pytest`) — turns any DI dependency into a pytest fixture
## Install
```bash
uv add modern-di # or: pip install modern-di
```
## Quick Start
```python
import dataclasses
from modern_di import Container, Group, Scope, providers
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
class Settings:
database_url: str = "postgresql+asyncpg://localhost/app"
@dataclasses.dataclass(kw_only=True, slots=True)
class UserRepository:
settings: Settings # auto-injected by type
class Dependencies(Group):
settings = providers.Factory(scope=Scope.APP, creator=Settings)
user_repository = providers.Factory(scope=Scope.REQUEST, creator=UserRepository)
with Container(groups=[Dependencies], validate=True) as container:
with container.build_child_container(scope=Scope.REQUEST) as request:
repo = request.resolve(UserRepository)
print(repo.settings.database_url)
```
See the [documentation](https://modern-di.modern-python.org) for scopes, lifecycles, finalizers, and framework integrations.
Usage examples:
- with LiteStar - [litestar-sqlalchemy-template](https://github.com/modern-python/litestar-sqlalchemy-template)
- with FastAPI - [fastapi-sqlalchemy-template](https://github.com/modern-python/fastapi-sqlalchemy-template)
## 📚 [Documentation](https://modern-di.modern-python.org)
## 📦 [PyPI](https://pypi.org/project/modern-di)
## 📝 [License](LICENSE)
## Part of `modern-python`
Browse the full list of templates and libraries in
[`modern-python`](https://github.com/modern-python) — see the org profile for the categorized index.