Skip to main content

modern-di

GitHub stars Context7 uv Ruff ty

Project Badges
modern-di Supported versions downloads
modern-di-aiohttp Supported versions downloads
modern-di-fastapi Supported versions downloads
modern-di-faststream Supported versions downloads
modern-di-litestar Supported versions downloads
modern-di-pytest Supported versions downloads
modern-di-starlette 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 aiohttp, FastAPI, FastStream, Litestar, Starlette 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(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. 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-2.25.0.tar.gz (22.8 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-2.25.0-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di-2.25.0.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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-2.25.0.tar.gz
Algorithm Hash digest
SHA256 3de58003f88bcbf79dab08853f23eec4687f235a01ef7f1de781e215a5a1a1ac
MD5 e370e7f0be7a5cda8032e6e3665ec3cf
BLAKE2b-256 c7c067f673f26897a0d3ad8653622965f04096e353994d7ac39907186be8790a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di-2.25.0-py3-none-any.whl
  • Upload date:
  • Size: 30.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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-2.25.0-py3-none-any.whl
Algorithm Hash digest
SHA256 397d6e91cc765f3508cbbe923539b7a4a870cbc258f425fb561f523010937c0d
MD5 f0127f91f0c83e51a3d6d602fae48e26
BLAKE2b-256 72e7b4ac2136917c181eb612c0dbf3f1108e2f811a3be768101d16b0a8a0aff4

See more details on using hashes here.

Release history Release notifications | RSS feed

Supported by

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