Skip to main content

Dependency Injection framework with IOC-container and scopes

Project description

modern-di

GitHub stars Context7 uv Ruff ty

Project Badges
modern-di Supported versions downloads
modern-di-fastapi Supported versions downloads
modern-di-faststream Supported versions downloads
modern-di-litestar Supported versions downloads
modern-di-typer Supported versions downloads
modern-di-pytest 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 FastAPI, FastStream, LiteStar 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.

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.

Project details


Release history Release notifications | RSS feed

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.19.0.tar.gz (18.3 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.19.0-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di-2.19.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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.19.0.tar.gz
Algorithm Hash digest
SHA256 86257ac0d2ee563674bfa4c051e659e96c06d22bb9e5cc32c6d6fb74badc5ba6
MD5 14108f84abf1cb5397c3ec538fd3d5e8
BLAKE2b-256 703e563488eabab5f915de2ad64d48926b031e337a5d4084547fb4be55ec8234

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di-2.19.0-py3-none-any.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80e5bffa84d9639c998b72b5508229b1a208d7cb0f502454cb144be30793e9ef
MD5 2e91641200e5eec877dc067ac8f90f11
BLAKE2b-256 379248e096073e7357a83bd25363818af46a16daaf52cb257aa15af30c2cdc55

See more details on using hashes here.

Supported by

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