Skip to main content

Dependency Injection framework with IOC-container and scopes

Project description

"Modern-DI"

Project Badges
common MyPy Strict GitHub stars
modern-di Supported versions downloads
modern-di-fastapi Supported versions downloads
modern-di-litestar Supported versions downloads

Dependency injection framework for Python inspired by dependency-injector and dishka.

It is in development state yet and gives you the following:

  • DI framework with IOC-container and scopes.
  • Async and sync resolving.
  • Python 3.10-3.13 support.
  • Full coverage by types annotations (mypy in strict mode).
  • Overriding dependencies for tests.
  • Package with zero dependencies.
  • Integration with FastAPI and LiteStar
  • Thread-safe and asyncio concurrency safe providers

📚 Documentation

Describe resources and classes:

import dataclasses
import logging
import typing


logger = logging.getLogger(__name__)


# singleton provider with finalization
def create_sync_resource() -> typing.Iterator[str]:
    logger.debug("Resource initiated")
    try:
        yield "sync resource"
    finally:
        logger.debug("Resource destructed")


# same, but async
async def create_async_resource() -> typing.AsyncIterator[str]:
    logger.debug("Async resource initiated")
    try:
        yield "async resource"
    finally:
        logger.debug("Async resource destructed")


@dataclasses.dataclass(kw_only=True, slots=True)
class DependentFactory:
    sync_resource: str
    async_resource: str

Describe dependencies graph (IoC-container)

from modern_di import BaseGraph, Scope, providers


class Dependencies(BaseGraph):
    sync_resource = providers.Resource(Scope.APP, create_sync_resource)
    async_resource = providers.Resource(Scope.APP, create_async_resource)

    simple_factory = providers.Factory(Scope.REQUEST, SimpleFactory, dep1="text", dep2=123)
    dependent_factory = providers.Factory(
        Scope.REQUEST,
        sync_resource=sync_resource,
        async_resource=async_resource,
    )

Create container and resolve dependencies in your code

from modern_di import Container, Scope


# init container of app scope in sync mode
with Container(scope=Scope.APP) as app_container:
    # resolve sync resource
    Dependencies.sync_resource.sync_resolve(app_container)


# init container of app scope in async mode
async with Container(scope=Scope.APP) as app_container:
    # resolve async resource
    await Dependencies.async_resource.async_resolve(app_container)

    # resolve sync resource
    instance1 = await Dependencies.sync_resource.async_resolve(app_container)
    instance2 = Dependencies.sync_resource.sync_resolve(app_container)
    assert instance1 is instance2

    # create container of request scope
    async with app_container.build_child_container(scope=Scope.REQUEST) as request_container:
        # resolve factories of request scope
        Dependencies.simple_factory.sync_resolve(request_container)
        await Dependencies.dependent_factory.async_resolve(request_container)
        
        # resources of app-scope also can be resolved here

Project details


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-0.9.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

modern_di-0.9.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di-0.9.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for modern_di-0.9.0.tar.gz
Algorithm Hash digest
SHA256 8a2552c4248babfc2b8a9713d36be57b8b426e5ab5b5a46626b0fdc87c7be487
MD5 d2afab0059c38edbb88614951b334829
BLAKE2b-256 a3acce69bcfdfcab9e36f38fa80be68ce9719204391d61db37f209464b647170

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for modern_di-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 841aef07fa564f1400c2c770368242a2dd662609a38c844ff3027d2f75161cbc
MD5 4d94c0b40223fc26a204136f34daa753
BLAKE2b-256 b600eae7a29afc7f590d74dc8104d42e245cbc4729c94962a7a0d09368bbadfe

See more details on using hashes here.

Supported by

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