"That Depends"
==
[![Test Coverage](https://codecov.io/gh/modern-python/that-depends/branch/main/graph/badge.svg)](https://codecov.io/gh/modern-python/that-depends)
[![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
[![Supported versions](https://img.shields.io/pypi/pyversions/that-depends.svg)](https://pypi.python.org/pypi/that-depends)
[![downloads](https://img.shields.io/pypi/dm/that-depends.svg)](https://pypistats.org/packages/that-depends)
[![GitHub stars](https://img.shields.io/github/stars/modern-python/that-depends)](https://github.com/modern-python/that-depends/stargazers)
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.
📚 [Documentation](https://that-depends.readthedocs.io)
# Quickstart
## Install
```bash
pip install that-depends
```
## Describe resources and classes:
```python
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
```python
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
```python
# 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
```python
@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
```python
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
```
Raw data
{
"_id": null,
"home_page": null,
"name": "that-depends",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.10",
"maintainer_email": null,
"keywords": "dependency injector, di, ioc-container, mocks, python",
"author": null,
"author_email": "Artur Shiriev <me@shiriev.ru>",
"download_url": "https://files.pythonhosted.org/packages/53/3e/d62f29e9cc91a27e524e801ad8fa4c4a906a2d6c038a4fd4c5dc2c2a28d6/that_depends-1.25.0.tar.gz",
"platform": null,
"description": "\"That Depends\"\n==\n[![Test Coverage](https://codecov.io/gh/modern-python/that-depends/branch/main/graph/badge.svg)](https://codecov.io/gh/modern-python/that-depends)\n[![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)\n[![Supported versions](https://img.shields.io/pypi/pyversions/that-depends.svg)](https://pypi.python.org/pypi/that-depends)\n[![downloads](https://img.shields.io/pypi/dm/that-depends.svg)](https://pypistats.org/packages/that-depends)\n[![GitHub stars](https://img.shields.io/github/stars/modern-python/that-depends)](https://github.com/modern-python/that-depends/stargazers)\n\nDependency injection framework for Python inspired by `dependency-injector`.\n\nIt is production-ready and gives you the following:\n- Simple async-first DI framework with IOC-container.\n- Python 3.10-3.12 support.\n- Full coverage by types annotations (mypy in strict mode).\n- FastAPI and LiteStar compatibility.\n- Overriding dependencies for tests.\n- Injecting dependencies in functions and coroutines without wiring.\n- Package with zero dependencies.\n\n\ud83d\udcda [Documentation](https://that-depends.readthedocs.io)\n\n# Quickstart\n## Install\n```bash\npip install that-depends\n```\n\n## Describe resources and classes:\n```python\nimport dataclasses\nimport logging\nimport typing\n\n\nlogger = logging.getLogger(__name__)\n\n\n# singleton provider with finalization\ndef create_sync_resource() -> typing.Iterator[str]:\n logger.debug(\"Resource initiated\")\n try:\n yield \"sync resource\"\n finally:\n logger.debug(\"Resource destructed\")\n\n\n# same, but async\nasync def create_async_resource() -> typing.AsyncIterator[str]:\n logger.debug(\"Async resource initiated\")\n try:\n yield \"async resource\"\n finally:\n logger.debug(\"Async resource destructed\")\n\n\n@dataclasses.dataclass(kw_only=True, slots=True)\nclass DependentFactory:\n sync_resource: str\n async_resource: str\n```\n\n## Describe IoC-container\n```python\nfrom that_depends import BaseContainer, providers\n\n\nclass DIContainer(BaseContainer):\n sync_resource = providers.Resource(create_sync_resource)\n async_resource = providers.Resource(create_async_resource)\n\n simple_factory = providers.Factory(SimpleFactory, dep1=\"text\", dep2=123)\n dependent_factory = providers.Factory(\n sync_resource=sync_resource,\n async_resource=async_resource,\n )\n```\n\n## Resolve dependencies in your code\n```python\n# async resolving by default:\nawait DIContainer.simple_factory()\n\n# sync resolving is also allowed if there is no uninitialized async resources in dependencies\nDIContainer.simple_factory.sync_resolve()\n\n# otherwise you can initialize resources beforehand one by one or in one call:\nawait DIContainer.init_resources()\n```\n\n## Resolve dependencies not described in container\n```python\n@dataclasses.dataclass(kw_only=True, slots=True)\nclass FreeFactory:\n dependent_factory: DependentFactory\n sync_resource: str\n\n# this way container will try to find providers by names and resolve them to build FreeFactory instance\nfree_factory_instance = await DIContainer.resolve(FreeFactory)\n```\n\n## Inject providers in function arguments\n```python\nimport datetime\n\nfrom that_depends import inject, Provide\n\nfrom tests import container\n\n\n@inject\nasync def some_coroutine(\n simple_factory: container.SimpleFactory = Provide[container.DIContainer.simple_factory],\n dependent_factory: container.DependentFactory = Provide[container.DIContainer.dependent_factory],\n default_zero: int = 0,\n) -> None:\n assert simple_factory.dep1\n assert isinstance(dependent_factory.async_resource, datetime.datetime)\n assert default_zero == 0\n\n@inject\ndef some_function(\n simple_factory: container.SimpleFactory = Provide[container.DIContainer.simple_factory],\n default_zero: int = 0,\n) -> None:\n assert simple_factory.dep1\n assert default_zero == 0\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Simple Dependency Injection framework",
"version": "1.25.0",
"project_urls": {
"docs": "https://that-depends.readthedocs.io",
"repository": "https://github.com/modern-python/that-depends"
},
"split_keywords": [
"dependency injector",
" di",
" ioc-container",
" mocks",
" python"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "dd71e972f93571fdf3f8e4fa68aed158b75cb41c5c9ec028aedf597d029a2f7e",
"md5": "d13233fa95e131b968114d253e69bf83",
"sha256": "e9a0a71e2dd5ced73c2a93e8819ecb81c3133c602ce67d910a1eac83d6c21f49"
},
"downloads": -1,
"filename": "that_depends-1.25.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d13233fa95e131b968114d253e69bf83",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.10",
"size": 15147,
"upload_time": "2024-11-20T15:13:25",
"upload_time_iso_8601": "2024-11-20T15:13:25.728023Z",
"url": "https://files.pythonhosted.org/packages/dd/71/e972f93571fdf3f8e4fa68aed158b75cb41c5c9ec028aedf597d029a2f7e/that_depends-1.25.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "533ed62f29e9cc91a27e524e801ad8fa4c4a906a2d6c038a4fd4c5dc2c2a28d6",
"md5": "ac5258a9d664f23c8f14ef6b72592959",
"sha256": "f037e38713994d096cf2c937f51e5773d3466c9bc8508f409469fe7051b76a0f"
},
"downloads": -1,
"filename": "that_depends-1.25.0.tar.gz",
"has_sig": false,
"md5_digest": "ac5258a9d664f23c8f14ef6b72592959",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.10",
"size": 25289,
"upload_time": "2024-11-20T15:13:26",
"upload_time_iso_8601": "2024-11-20T15:13:26.880047Z",
"url": "https://files.pythonhosted.org/packages/53/3e/d62f29e9cc91a27e524e801ad8fa4c4a906a2d6c038a4fd4c5dc2c2a28d6/that_depends-1.25.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-20 15:13:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "modern-python",
"github_project": "that-depends",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "that-depends"
}