modern-di is a dependency injection container for Python 3.11+. You declare how each object is
built, and the container builds it, fills its constructor from type annotations, keeps it for as
long as its scope lives, and runs its teardown when that scope closes.
- Constructor parameters are matched by type, and you can pass explicit arguments where a type is ambiguous.
- Scopes (APP, REQUEST and finer ones) decide how long a cached object lives, and finalizers close it when its scope ends.
container.validate()finds cycles and scope errors at startup instead of on the first request.- One override replaces a dependency for the whole container tree, so HTTP handlers, workers and direct unit tests all see the same fake.
- Typed end to end, with no type-checker plugin.
- Integrations for aiogram, aiohttp, arq, Celery, FastAPI, FastMCP, FastStream, Flask, gRPC, Litestar, Starlette, taskiq and Typer, plus a pytest plugin.
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.1.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.1.0.tar.gz | 41.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di-4.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 93.1 kB
Release files / modern_di-4.1.0.tar.gz
| Download URL | modern_di-4.1.0.tar.gz |
|---|---|
| Size | 41.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6433f29370380d178a970919b00e0b4ddb572ef06aef3c0b92ae1bde6f94a348
|
|
BLAKE2b-256 checksum How to use checksums |
e83e26677428a7f81cd8f1332c52de64e3a86f28321e112072756ad051c75bad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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.1.0-py3-none-any.whl
| Download URL | modern_di-4.1.0-py3-none-any.whl |
|---|---|
| Size | 52.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
648904361b4dc756008c5ffc7803d7fa667ee5971f23021d8790b9c87253fd8a
|
|
BLAKE2b-256 checksum How to use checksums |
f5ddf55e9f8d8b55e256684433640568011ac5d9d9cb77423f627932a373e6fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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}
|