modern-di


Namemodern-di JSON
Version 0.9.0 PyPI version JSON
download
home_pageNone
SummaryDependency Injection framework with IOC-container and scopes
upload_time2024-11-23 08:56:13
maintainerNone
docs_urlNone
authorNone
requires_python<4,>=3.10
licenseMIT
keywords di dependency injector ioc-container mocks python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            "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
- 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/a3/ac/ce69bcfdfcab9e36f38fa80be68ce9719204391d61db37f209464b647170/modern_di-0.9.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- 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": "MIT",
    "summary": "Dependency Injection framework with IOC-container and scopes",
    "version": "0.9.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": "b600eae7a29afc7f590d74dc8104d42e245cbc4729c94962a7a0d09368bbadfe",
                "md5": "4d94c0b40223fc26a204136f34daa753",
                "sha256": "841aef07fa564f1400c2c770368242a2dd662609a38c844ff3027d2f75161cbc"
            },
            "downloads": -1,
            "filename": "modern_di-0.9.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4d94c0b40223fc26a204136f34daa753",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.10",
            "size": 12838,
            "upload_time": "2024-11-23T08:56:12",
            "upload_time_iso_8601": "2024-11-23T08:56:12.479444Z",
            "url": "https://files.pythonhosted.org/packages/b6/00/eae7a29afc7f590d74dc8104d42e245cbc4729c94962a7a0d09368bbadfe/modern_di-0.9.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a3acce69bcfdfcab9e36f38fa80be68ce9719204391d61db37f209464b647170",
                "md5": "d2afab0059c38edbb88614951b334829",
                "sha256": "8a2552c4248babfc2b8a9713d36be57b8b426e5ab5b5a46626b0fdc87c7be487"
            },
            "downloads": -1,
            "filename": "modern_di-0.9.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d2afab0059c38edbb88614951b334829",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.10",
            "size": 6548,
            "upload_time": "2024-11-23T08:56:13",
            "upload_time_iso_8601": "2024-11-23T08:56:13.812504Z",
            "url": "https://files.pythonhosted.org/packages/a3/ac/ce69bcfdfcab9e36f38fa80be68ce9719204391d61db37f209464b647170/modern_di-0.9.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-23 08:56:13",
    "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"
}
        
Elapsed time: 1.26757s