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.11+ support
- Fully typed and tested
- Integrations with
aiogram,aiohttp,arq,Celery,FastAPI,FastStream,Flask,gRPC,Litestar,Starlette,taskiq, andTyper - Pytest integration (
modern-di-pytest) that 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 4.0.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-4.0.0.tar.gz | 37.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di-4.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 87.3 kB
Release files / modern_di-4.0.0.tar.gz
| Download URL | modern_di-4.0.0.tar.gz |
|---|---|
| Size | 37.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5554fa458dfb59ab0325a57b1e550c3e35524f4236604884529980f9dc1f5424
|
|
BLAKE2b-256 checksum How to use checksums |
58e39c92577190555fbc78de895ae30e5093290decd682cd31e170d1a01d8a71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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-4.0.0-py3-none-any.whl
| Download URL | modern_di-4.0.0-py3-none-any.whl |
|---|---|
| Size | 50.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0634d9a9e5159d92572c93090c0f9d939291c2de9b0b86de925ba7e0c91f30ce
|
|
BLAKE2b-256 checksum How to use checksums |
230d3bf3ef9c0fda1e44ab7ed4e6e81a24d4428d0ff85ea1a01f3ab54ec6c956
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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}
|