Simple Dependency Injection framework
Project description
"That Depends"
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.
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
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.23.0.tar.gz
(24.2 kB
view details)
Built Distribution
File details
Details for the file that_depends-1.23.0.tar.gz
.
File metadata
- Download URL: that_depends-1.23.0.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ceefa407f305db4566fa8d13e1e53bb88b24339d50eaef2a0411fa0f77f63d61 |
|
MD5 | 9202e3fee6d3e0b6a7751a723fc966e1 |
|
BLAKE2b-256 | 71a8b5128c40e4b8938b8cc77be6f1f3a80422dedd413d8b30e9314e85661441 |
File details
Details for the file that_depends-1.23.0-py3-none-any.whl
.
File metadata
- Download URL: that_depends-1.23.0-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d130c015d42af45b69e49dfa482c2343c8e374455077384b9a18d3358b5b9bf5 |
|
MD5 | 8b91273e9141ac3a9dffcc4b8d7878c2 |
|
BLAKE2b-256 | 61bfbbf236df77e6383f9ae4693ce555336cf507f508abb077a53c5c005486b1 |