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.4.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.4.0.tar.gz | 38.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di-3.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 86.7 kB
Release files / modern_di-3.4.0.tar.gz
| Download URL | modern_di-3.4.0.tar.gz |
|---|---|
| Size | 38.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aa54216ba17be2effd2d5434b4d27da4e9b700bfd335eccdad3082b443ffd33e
|
|
BLAKE2b-256 checksum How to use checksums |
e9c7bd476c166f41a16236370cf4ef84c7d1225cf2cfab7b0981407f23a71555
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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.4.0-py3-none-any.whl
| Download URL | modern_di-3.4.0-py3-none-any.whl |
|---|---|
| Size | 48.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
02c5bfc762ae4113ef12a16cc8297b80253e3c823a010fb0220b26847281fbdc
|
|
BLAKE2b-256 checksum How to use checksums |
344611c30c998fbd3ef3bc38923c2c3a3895f46bfa85fba3dc214045f7459800
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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}
|