Skip to main content

Simple Dependency Injection framework

Project description

"That Depends"

Test Coverage MyPy Strict Supported versions downloads GitHub stars

Dependency injection framework for Python inspired by dependency-injector.

It is production-ready and gives you the following:

  • Simple async-first DI framework with IOC-container.
  • Python 3.10-3.12 support.
  • Full coverage by types annotations (mypy in strict mode).
  • FastAPI and LiteStar compatibility.
  • Overriding dependencies for tests.
  • Injecting dependencies in functions and coroutines without wiring.
  • Package with zero dependencies.

📚 Documentation

Quickstart

Install

pip install that-depends

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 IoC-container

from that_depends import BaseContainer, providers


class DIContainer(BaseContainer):
    sync_resource = providers.Resource(create_sync_resource)
    async_resource = providers.Resource(create_async_resource)

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

Resolve dependencies in your code

# async resolving by default:
await DIContainer.simple_factory()

# sync resolving is also allowed if there is no uninitialized async resources in dependencies
DIContainer.simple_factory.sync_resolve()

# otherwise you can initialize resources beforehand one by one or in one call:
await DIContainer.init_resources()

Resolve dependencies not described in container

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

# this way container will try to find providers by names and resolve them to build FreeFactory instance
free_factory_instance = await DIContainer.resolve(FreeFactory)

Inject providers in function arguments

import datetime

from that_depends import inject, Provide

from tests import container


@inject
async def some_coroutine(
    simple_factory: container.SimpleFactory = Provide[container.DIContainer.simple_factory],
    dependent_factory: container.DependentFactory = Provide[container.DIContainer.dependent_factory],
    default_zero: int = 0,
) -> None:
    assert simple_factory.dep1
    assert isinstance(dependent_factory.async_resource, datetime.datetime)
    assert default_zero == 0

@inject
def some_function(
    simple_factory: container.SimpleFactory = Provide[container.DIContainer.simple_factory],
    default_zero: int = 0,
) -> None:
    assert simple_factory.dep1
    assert default_zero == 0

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

that_depends-1.25.0.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

that_depends-1.25.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file that_depends-1.25.0.tar.gz.

File metadata

  • Download URL: that_depends-1.25.0.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.3

File hashes

Hashes for that_depends-1.25.0.tar.gz
Algorithm Hash digest
SHA256 f037e38713994d096cf2c937f51e5773d3466c9bc8508f409469fe7051b76a0f
MD5 ac5258a9d664f23c8f14ef6b72592959
BLAKE2b-256 533ed62f29e9cc91a27e524e801ad8fa4c4a906a2d6c038a4fd4c5dc2c2a28d6

See more details on using hashes here.

File details

Details for the file that_depends-1.25.0-py3-none-any.whl.

File metadata

File hashes

Hashes for that_depends-1.25.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9a0a71e2dd5ced73c2a93e8819ecb81c3133c602ce67d910a1eac83d6c21f49
MD5 d13233fa95e131b968114d253e69bf83
BLAKE2b-256 dd71e972f93571fdf3f8e4fa68aed158b75cb41c5c9ec028aedf597d029a2f7e

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