| 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
uv add modern-di # or: pip install modern-diimport 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
📦 PyPI
📝 License
Browse the full list of templates and libraries in
modern-python — see the org profile for the categorized index.