Skip to main content

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-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 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 aiogram, aiohttp, arq, Celery, FastAPI, FastStream, Flask, gRPC, Litestar, Starlette, taskiq, and Typer
  • 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(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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modern_di-3.3.0.tar.gz (38.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modern_di-3.3.0-py3-none-any.whl (48.2 kB view details)

Uploaded Python 3

File details

Details for the file modern_di-3.3.0.tar.gz.

File metadata

  • Download URL: modern_di-3.3.0.tar.gz
  • Upload date:
  • Size: 38.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","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

Hashes for modern_di-3.3.0.tar.gz
Algorithm Hash digest
SHA256 70db87c5c91110276ead755b5d6f0e4ff1c3749a98091adb227cc8d69e6dec23
MD5 42a361b517038b731cef616b44239bd5
BLAKE2b-256 7fae6907f8bf714ce1cf8ab449f2f077a28538c8f0ef96076b534420344653d9

See more details on using hashes here.

File details

Details for the file modern_di-3.3.0-py3-none-any.whl.

File metadata

  • Download URL: modern_di-3.3.0-py3-none-any.whl
  • Upload date:
  • Size: 48.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","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

Hashes for modern_di-3.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bac3fbf20494cac3064e92becbd0e66bf469e7e6ab741268f8a3202d2e2c4686
MD5 1769e535bd27a5bd8f8cd5bc2a418869
BLAKE2b-256 ac7eabf6320b1205a0a8444f8a3226f434fc90e71245784412016c7e073d01ad

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.3.0

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page