Skip to content
modern-pythonPublic

About

Powerful dependency-injection framework with IoC container and scopes

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

66 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

968 Commits

Folders and files

Repository files navigation

modern-di

GitHub stars Context7 uv Ruff ty

Project Badges
modern-di Supported versions downloads
modern-di-aiogram Supported versions downloads
modern-di-aiohttp Supported versions downloads
modern-di-arq Supported versions downloads
modern-di-celery Supported versions downloads
modern-di-fastapi Supported versions downloads
modern-di-fastmcp Supported versions downloads
modern-di-faststream Supported versions downloads
modern-di-flask Supported versions downloads
modern-di-grpc Supported versions downloads
modern-di-litestar Supported versions downloads
modern-di-pytest Supported versions downloads
modern-di-starlette Supported versions downloads
modern-di-taskiq Supported versions downloads
modern-di-typer Supported versions downloads

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:

πŸ“š 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.

About

Powerful dependency-injection framework with IoC container and scopes

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

66 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages