Skip to main content

Simple Dependency Injection framework

Project description

"That Depends"

PyPI version Supported versions GitHub license GitHub Actions Workflow Status Doc GitHub stars

This package is dependency injection framework for Python, mostly inspired by python-dependency-injector.

📚 Documentation

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 async and sync functions and coroutines.
  • Zero dependencies.

Projects with That Depends:

Main decisions:

  1. async-first means that by default, dependency resolving is async:
some_dependency = await DIContainer.dependent_factory()
  1. Sync resolving is also possible, but will fail in case of async dependencies:
sync_resource = DIContainer.sync_resource.sync_resolve()  # this will work
async_resource = DIContainer.async_resource.sync_resolve()  # this will fail with RuntimeError

# but this will work
async_resource = await DIContainer.async_resource()
async_resource = DIContainer.async_resource.sync_resolve()

# and this will work
async_resource = await DIContainer.init_async_resources()
async_resource = DIContainer.async_resource.sync_resolve()
  1. No wiring for injections in function arguments -> achieved by decision that only one instance of container is supported
from tests import container
from that_depends import Provide, inject, inject_to_sync

@inject
async def some_coroutine(
    simple_factory: container.SimpleFactory = Provide[container.DIContainer.simple_factory],
) -> None:
    assert simple_factory.dep1

@inject_to_sync
def some_sync_function(
    simple_factory: container.SimpleFactory = Provide[container.DIContainer.simple_factory],
) -> None:
    assert simple_factory.dep1

Quickstart

Install

pip install that-depends

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.9.2.tar.gz (7.6 kB view hashes)

Uploaded Source

Built Distribution

that_depends-1.9.2-py3-none-any.whl (10.0 kB view hashes)

Uploaded Python 3

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