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.2.tar.gz
(7.6 kB
view hashes)
Built Distribution
Close
Hashes for that_depends-1.9.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7824ef0fb665a5bcc5298de1ed5b53788bd163e455faec59868bd1bd2a9ef88e |
|
MD5 | b2a5886eeea547b6ecf57007a3befd5b |
|
BLAKE2b-256 | d56bec9cd9e7113fe5c8cf8b44c35b2dd4c25719a908c0f1bae3ec6b48292e9e |