This package is dependency injection framework for Python, mostly inspired by python-dependency-injector
.
It is production-ready and gives you the following:
- Fully-async simple DI framework with IOC-container.
- Python 3.10-3.12 support.
- Full coverage by types annotations (mypy in strict mode).
- FastAPI and Litestar compatibility.
- Zero dependencies.
- Overriding dependencies for tests.
- fastapi-sqlalchemy-template - FastAPI template with sqlalchemy2 and PostgreSQL
- litestar-sqlalchemy-template - LiteStar template with sqlalchemy2 and PostgreSQL
- By default, dependency resolving is async:
some_dependency = await DIContainer.dependent_factory()
- 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()
- 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
async def some_function(
simple_factory: container.SimpleFactory = Provide[container.DIContainer.simple_factory],
) -> None:
assert simple_factory.dep1
pip install that-depends