Dependency Injection framework with IOC-container and scopes
Project description
modern-di
| Project | Badges |
|---|---|
| modern-di | |
| modern-di-fastapi | |
| modern-di-faststream | |
| modern-di-litestar | |
| modern-di-typer | |
| modern-di-pytest |
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
FastAPI,FastStream,LiteStarandTyper - 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(scope=Scope.APP, creator=Settings)
user_repository = providers.Factory(scope=Scope.REQUEST, creator=UserRepository)
with Container(groups=[Dependencies], validate=True) 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.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modern_di-2.18.0.tar.gz.
File metadata
- Download URL: modern_di-2.18.0.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ddb3fafaf61726c1e5c2bc9273d85c4c0f87461fac2f559a27f7104a01d0bdd
|
|
| MD5 |
fe78efb3094ce046f748848d1a3bdfc2
|
|
| BLAKE2b-256 |
e03a1f92015fa3385b810f72d7eba950951e2ec67bd3f97093175f0277a7236f
|
File details
Details for the file modern_di-2.18.0-py3-none-any.whl.
File metadata
- Download URL: modern_di-2.18.0-py3-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3c0130aa02f5206faefed319d092d52827bfe5413132052159f2190a378ba3b
|
|
| MD5 |
6ae3c03d557e654f1dd21813a56bf8fa
|
|
| BLAKE2b-256 |
48229369b496a53e2bccafceddfbf10cfe4e23b36fe24e3307fd6964f03caa2a
|