"Modern-DI"
==
| Project | Badges |
|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| common | [![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration) [![GitHub stars](https://img.shields.io/github/stars/modern-python/modern-di)](https://github.com/modern-python/modern-di/stargazers) |
| modern-di | [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di.svg)](https://pypi.python.org/pypi/modern-di ) [![downloads](https://img.shields.io/pypi/dm/modern-di.svg)](https://pypistats.org/packages/modern-di) |
| modern-di-fastapi | [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di-fastapi.svg)](https://pypi.python.org/pypi/modern-di-fastapi) [![downloads](https://img.shields.io/pypi/dm/modern-di-fastapi.svg)](https://pypistats.org/packages/modern-di-fastapi) |
| modern-di-litestar | [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di-litestar.svg)](https://pypi.python.org/pypi/modern-di-litestar) [![downloads](https://img.shields.io/pypi/dm/modern-di-litestar.svg)](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
📚 [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/8b/11/a26e76f29afe5fe6d70ab55766cb406a8bcc84eae4e851ad7d5bbb1f07cb/modern_di-0.8.0.tar.gz",
"platform": null,
"description": "\"Modern-DI\"\n==\n\n| Project | Badges |\n|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| common | [![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration) [![GitHub stars](https://img.shields.io/github/stars/modern-python/modern-di)](https://github.com/modern-python/modern-di/stargazers) |\n| modern-di | [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di.svg)](https://pypi.python.org/pypi/modern-di ) [![downloads](https://img.shields.io/pypi/dm/modern-di.svg)](https://pypistats.org/packages/modern-di) |\n| modern-di-fastapi | [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di-fastapi.svg)](https://pypi.python.org/pypi/modern-di-fastapi) [![downloads](https://img.shields.io/pypi/dm/modern-di-fastapi.svg)](https://pypistats.org/packages/modern-di-fastapi) |\n| modern-di-litestar | [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di-litestar.svg)](https://pypi.python.org/pypi/modern-di-litestar) [![downloads](https://img.shields.io/pypi/dm/modern-di-litestar.svg)](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\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": "MIT",
"summary": "Dependency Injection framework with IOC-container and scopes",
"version": "0.8.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": "2075f0affe281afc964b42036af8ea5acacfec53a9af2af574998e9c0c3b2813",
"md5": "73cf4d3ce3e3db3690d1f04fbbf0bd7d",
"sha256": "258e36c5fc058a0167dbef43ca8795b4d3cdd5e89446081c7da8799cc3db7ecb"
},
"downloads": -1,
"filename": "modern_di-0.8.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "73cf4d3ce3e3db3690d1f04fbbf0bd7d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.10",
"size": 12792,
"upload_time": "2024-11-17T10:45:53",
"upload_time_iso_8601": "2024-11-17T10:45:53.878101Z",
"url": "https://files.pythonhosted.org/packages/20/75/f0affe281afc964b42036af8ea5acacfec53a9af2af574998e9c0c3b2813/modern_di-0.8.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "8b11a26e76f29afe5fe6d70ab55766cb406a8bcc84eae4e851ad7d5bbb1f07cb",
"md5": "fde5abe0f13599e248d790f149042986",
"sha256": "29026d949d80f9f6903bcf9181ce24a36369426a374084561e112a6aba5d8360"
},
"downloads": -1,
"filename": "modern_di-0.8.0.tar.gz",
"has_sig": false,
"md5_digest": "fde5abe0f13599e248d790f149042986",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.10",
"size": 6504,
"upload_time": "2024-11-17T10:45:55",
"upload_time_iso_8601": "2024-11-17T10:45:55.452275Z",
"url": "https://files.pythonhosted.org/packages/8b/11/a26e76f29afe5fe6d70ab55766cb406a8bcc84eae4e851ad7d5bbb1f07cb/modern_di-0.8.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-17 10:45:55",
"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"
}