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
aiogram,aiohttp,arq,Celery,FastAPI,FastStream,Flask,gRPC,Litestar,Starlette,taskiq, andTyper - Pytest integration (
modern-di-pytest) — turns any DI dependency into a pytest fixture
Install
uv add modern-di # or: pip install modern-di
Quick Start
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(Settings, scope=Scope.APP)
user_repository = providers.Factory(UserRepository, scope=Scope.REQUEST)
with Container(groups=[Dependencies]) as container:
with container.build_child_container(scope=Scope.REQUEST) as request:
repo = request.resolve(UserRepository)
print(repo.settings.database_url)
See the documentation for scopes, lifecycles, finalizers, and framework integrations. modern-di is deliberately conservative — see Design decisions for what it leaves out on purpose, including auto-binding, in-package integrations, and graph rendering.
Usage examples:
- with Litestar - litestar-sqlalchemy-template
- with FastAPI - fastapi-sqlalchemy-template
📚 Documentation
📦 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.
Metadata
Release files for modern-di 3.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di-3.5.0.tar.gz | 34.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di-3.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 81.2 kB
Release files / modern_di-3.5.0.tar.gz
| Download URL | modern_di-3.5.0.tar.gz |
|---|---|
| Size | 34.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
835bff74608fa4db8e6a3d5db83cd1123b9cbeb75e4aede8907a7a4d8d2e4c62
|
|
BLAKE2b-256 checksum How to use checksums |
187ff526669ed4ad2a00e3b7608d70e64e1874bbaf486643bcd0778decfe8137
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","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}
|
Release files / modern_di-3.5.0-py3-none-any.whl
| Download URL | modern_di-3.5.0-py3-none-any.whl |
|---|---|
| Size | 47.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a105cc9a58f5443dfa98f208d28679f7b88ff1f4d51d53d44fff09e3432c0ee6
|
|
BLAKE2b-256 checksum How to use checksums |
857b3477a1582d9f12c1e0ca4d9ebfc04160a4e05dd8cfd56495ff1f2c5638fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","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}
|