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-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 modern-di-pytest, which turns dependencies into pytest fixtures.

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:

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 4.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modern-di 4.3.1
File Size Uploaded Provenance Actions
modern_di-4.3.1.tar.gz 44.9 kB Without provenance Details for modern_di-4.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for modern-di 4.3.1
File Interpreter ABI Platform Actions
modern_di-4.3.1-py3-none-any.whl Python 3 none any Details for modern_di-4.3.1-py3-none-any.whl

Total release size: 101.0 kB

Release files / modern_di-4.3.1.tar.gz

Download URL modern_di-4.3.1.tar.gz
Size 44.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3910cb6064783d58824166e87b638d0b644846ac19e4c6f2d5134603bb28cfad
BLAKE2b-256 checksum
How to use checksums
d5572ffad4cbaa97bd69d1f29b87c61085da59c57a2565e8c885c45fb4828837
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","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-4.3.1-py3-none-any.whl

Download URL modern_di-4.3.1-py3-none-any.whl
Size 56.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
131991cf0b5e790d0d1601a10f9dcbb67bc02c2ef8e7e9e34b82bc7a29ca97e3
BLAKE2b-256 checksum
How to use checksums
69fdef9301d0553ebac8c844241c8b2766c2fcfcee84a721580524ae551f7613
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","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}

Security for modern-di 4.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

4.3.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page