"Modern-DI"
==
| Project | Badges |
|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| common | [](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration) [](https://github.com/modern-python/modern-di/stargazers) |
| modern-di | [](https://pypi.python.org/pypi/modern-di ) [](https://pypistats.org/packages/modern-di) |
| modern-di-fastapi | [](https://pypi.python.org/pypi/modern-di-fastapi) [](https://pypistats.org/packages/modern-di-fastapi) |
| modern-di-litestar | [](https://pypi.python.org/pypi/modern-di-litestar) [](https://pypistats.org/packages/modern-di-litestar) |
Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
It is in development state yet and gives you the following:
- DI framework with IOC-container and scopes.
- Async and sync resolving.
- Python 3.10-3.13 support.
- Full coverage by types annotations (mypy in strict mode).
- Overriding dependencies for tests.
- Package with zero dependencies.
- Integration with FastAPI and LiteStar
- Thread-safe and asyncio concurrency safe providers
📚 [Documentation](https://modern-di.readthedocs.io)
## 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 dependencies graph (IoC-container)
```python
from modern_di import BaseGraph, Scope, providers
class Dependencies(BaseGraph):
sync_resource = providers.Resource(Scope.APP, create_sync_resource)
async_resource = providers.Resource(Scope.APP, create_async_resource)
simple_factory = providers.Factory(Scope.REQUEST, SimpleFactory, dep1="text", dep2=123)
dependent_factory = providers.Factory(
Scope.REQUEST,
sync_resource=sync_resource,
async_resource=async_resource,
)
```
## Create container and resolve dependencies in your code
```python
from modern_di import Container, Scope
# init container of app scope in sync mode
with Container(scope=Scope.APP) as app_container:
# resolve sync resource
Dependencies.sync_resource.sync_resolve(app_container)
# init container of app scope in async mode
async with Container(scope=Scope.APP) as app_container:
# resolve async resource
await Dependencies.async_resource.async_resolve(app_container)
# resolve sync resource
instance1 = await Dependencies.sync_resource.async_resolve(app_container)
instance2 = Dependencies.sync_resource.sync_resolve(app_container)
assert instance1 is instance2
# create container of request scope
async with app_container.build_child_container(scope=Scope.REQUEST) as request_container:
# resolve factories of request scope
Dependencies.simple_factory.sync_resolve(request_container)
await Dependencies.dependent_factory.async_resolve(request_container)
# resources of app-scope also can be resolved here
```
Raw data
{
"_id": null,
"home_page": null,
"name": "modern-di",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.10",
"maintainer_email": null,
"keywords": "DI, dependency injector, ioc-container, mocks, python",
"author": null,
"author_email": "Artur Shiriev <me@shiriev.ru>",
"download_url": "https://files.pythonhosted.org/packages/bf/67/9636a0444471f21001d2906a6f694680547097c3d33254466f915b4c1ce3/modern_di-0.10.0.tar.gz",
"platform": null,
"description": "\"Modern-DI\"\n==\n\n| Project | Badges |\n|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| common | [](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration) [](https://github.com/modern-python/modern-di/stargazers) |\n| modern-di | [](https://pypi.python.org/pypi/modern-di ) [](https://pypistats.org/packages/modern-di) |\n| modern-di-fastapi | [](https://pypi.python.org/pypi/modern-di-fastapi) [](https://pypistats.org/packages/modern-di-fastapi) |\n| modern-di-litestar | [](https://pypi.python.org/pypi/modern-di-litestar) [](https://pypistats.org/packages/modern-di-litestar) |\n\nDependency injection framework for Python inspired by `dependency-injector` and `dishka`.\n\nIt is in development state yet and gives you the following:\n- DI framework with IOC-container and scopes.\n- Async and sync resolving.\n- Python 3.10-3.13 support.\n- Full coverage by types annotations (mypy in strict mode).\n- Overriding dependencies for tests.\n- Package with zero dependencies.\n- Integration with FastAPI and LiteStar\n- Thread-safe and asyncio concurrency safe providers\n\n\ud83d\udcda [Documentation](https://modern-di.readthedocs.io)\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 dependencies graph (IoC-container)\n```python\nfrom modern_di import BaseGraph, Scope, providers\n\n\nclass Dependencies(BaseGraph):\n sync_resource = providers.Resource(Scope.APP, create_sync_resource)\n async_resource = providers.Resource(Scope.APP, create_async_resource)\n\n simple_factory = providers.Factory(Scope.REQUEST, SimpleFactory, dep1=\"text\", dep2=123)\n dependent_factory = providers.Factory(\n Scope.REQUEST,\n sync_resource=sync_resource,\n async_resource=async_resource,\n )\n```\n\n## Create container and resolve dependencies in your code\n```python\nfrom modern_di import Container, Scope\n\n\n# init container of app scope in sync mode\nwith Container(scope=Scope.APP) as app_container:\n # resolve sync resource\n Dependencies.sync_resource.sync_resolve(app_container)\n\n\n# init container of app scope in async mode\nasync with Container(scope=Scope.APP) as app_container:\n # resolve async resource\n await Dependencies.async_resource.async_resolve(app_container)\n\n # resolve sync resource\n instance1 = await Dependencies.sync_resource.async_resolve(app_container)\n instance2 = Dependencies.sync_resource.sync_resolve(app_container)\n assert instance1 is instance2\n\n # create container of request scope\n async with app_container.build_child_container(scope=Scope.REQUEST) as request_container:\n # resolve factories of request scope\n Dependencies.simple_factory.sync_resolve(request_container)\n await Dependencies.dependent_factory.async_resolve(request_container)\n \n # resources of app-scope also can be resolved here\n\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "Dependency Injection framework with IOC-container and scopes",
"version": "0.10.0",
"project_urls": {
"docs": "https://modern-di.readthedocs.io",
"repository": "https://github.com/modern-python/modern-di"
},
"split_keywords": [
"di",
" dependency injector",
" ioc-container",
" mocks",
" python"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "be0b6a20bcb90cf9b6eb6e7ee13bdaca761c143c1a80e91a31cc3a2c7a37cfd8",
"md5": "dc4097931c340893690eff2638f1db38",
"sha256": "b8d1e098436cb6ca1961311ff48954692c90333f59fb5351b74f6970a22376ca"
},
"downloads": -1,
"filename": "modern_di-0.10.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "dc4097931c340893690eff2638f1db38",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.10",
"size": 12964,
"upload_time": "2024-12-29T12:35:40",
"upload_time_iso_8601": "2024-12-29T12:35:40.050397Z",
"url": "https://files.pythonhosted.org/packages/be/0b/6a20bcb90cf9b6eb6e7ee13bdaca761c143c1a80e91a31cc3a2c7a37cfd8/modern_di-0.10.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "bf679636a0444471f21001d2906a6f694680547097c3d33254466f915b4c1ce3",
"md5": "ea0d4b1d29382630853527e227f9bfc2",
"sha256": "9e8d93aa8f1a11e94c59596973d38c93bfbe4412a4604811399c98d696e83214"
},
"downloads": -1,
"filename": "modern_di-0.10.0.tar.gz",
"has_sig": false,
"md5_digest": "ea0d4b1d29382630853527e227f9bfc2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.10",
"size": 6644,
"upload_time": "2024-12-29T12:35:42",
"upload_time_iso_8601": "2024-12-29T12:35:42.143283Z",
"url": "https://files.pythonhosted.org/packages/bf/67/9636a0444471f21001d2906a6f694680547097c3d33254466f915b4c1ce3/modern_di-0.10.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-29 12:35:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "modern-python",
"github_project": "modern-di",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "modern-di"
}