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.2.2

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.2.2
File Size Uploaded Provenance Actions
modern_di-4.2.2.tar.gz 43.4 kB Without provenance Details for modern_di-4.2.2.tar.gz

Built distribution (wheel)

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

Total release size: 98.0 kB

Release files / modern_di-4.2.2.tar.gz

Download URL modern_di-4.2.2.tar.gz
Size 43.4 kB
Tags Source
SHA-256 checksum
How to use checksums
35c44c219b480836e577494521a08db81ad1c0aaa1fc1fbf9258f9b69c059d5b
BLAKE2b-256 checksum
How to use checksums
b260206dfa2d4defcf537566aa3fd5acc63f85deb3397c57ea589da7c2dc63f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.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}

Release files / modern_di-4.2.2-py3-none-any.whl

Download URL modern_di-4.2.2-py3-none-any.whl
Size 54.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cc0e29860bd2058d6a93d809d6c79e78766146a701599fde9620ce1018f7a45b
BLAKE2b-256 checksum
How to use checksums
69208fabfd0e712482b184b93fd4b6790a434290625324f8ea2c50c780e29bb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.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}

Security for modern-di 4.2.2

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.2.2 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