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 3.6.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.6.0.tar.gz | 34.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di-3.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 81.3 kB
Release files / modern_di-3.6.0.tar.gz
| Download URL | modern_di-3.6.0.tar.gz |
|---|---|
| Size | 34.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba46648471087ce8e7295e22e7b7969bf109a23121eca52cc7542a7ece24a202
|
|
BLAKE2b-256 checksum How to use checksums |
6afdbc6df1b5d3aff2bda7e07c2d53aed39ecf671dd9ae013f139080c3084c26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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.6.0-py3-none-any.whl
| Download URL | modern_di-3.6.0-py3-none-any.whl |
|---|---|
| Size | 47.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e1ca9a51aa4c8d7940da347b95fb04a1948cc0536909b0927f689445738f2eeb
|
|
BLAKE2b-256 checksum How to use checksums |
24a6d26f10f83dd1ca7b9908843f4ecf70f63df34d6ab1ae56bda80eb41588de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}
|