Simple Dependency Injection framework
Project description
"That Depends"
This package is dependency injection framework for Python, mostly inspired by python-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 async and sync functions and coroutines.
- Zero dependencies.
Projects with That Depends
:
- fastapi-sqlalchemy-template - FastAPI template with sqlalchemy2 and PostgreSQL
- litestar-sqlalchemy-template - LiteStar template with sqlalchemy2 and PostgreSQL
Main decisions:
async-first
means that 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()
# and this will work
async_resource = await DIContainer.init_async_resources()
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_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
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
that_depends-1.9.0.tar.gz
(7.5 kB
view hashes)
Built Distribution
Close
Hashes for that_depends-1.9.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad995ed3b41f3deee0dea4a260fb71ec14e972135f088cbf72bb37911ce42b7b |
|
MD5 | 5204b95673ef18689fc35b39b86d1835 |
|
BLAKE2b-256 | eb72e0412a73b82393023e0dadbdff3f6437f18394738b9c62dc2decca901648 |