modern-di 0.3.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of modern-di might be problematic. Click here for more details.
- modern_di-0.4.0/PKG-INFO +39 -0
- modern_di-0.4.0/README.md +20 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/graph.py +3 -3
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/providers/__init__.py +6 -5
- modern_di-0.3.0/modern_di/providers/base.py → modern_di-0.4.0/modern_di/providers/abstract.py +13 -10
- modern_di-0.4.0/modern_di/providers/dict.py +23 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/providers/factory.py +3 -3
- modern_di-0.4.0/modern_di/providers/list.py +23 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/providers/resource.py +3 -3
- modern_di-0.4.0/modern_di/providers/selector.py +39 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/providers/singleton.py +3 -3
- modern_di-0.4.0/tests/providers/test_dict.py +24 -0
- modern_di-0.4.0/tests/providers/test_list.py +24 -0
- modern_di-0.4.0/tests/providers/test_selector.py +45 -0
- modern_di-0.3.0/PKG-INFO +0 -37
- modern_di-0.3.0/README.md +0 -18
- modern_di-0.3.0/modern_di/providers/context_adapter.py +0 -31
- modern_di-0.3.0/tests/resolvers/test_context_adapter.py +0 -40
- {modern_di-0.3.0 → modern_di-0.4.0}/.github/workflows/core-ci.yml +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/.github/workflows/fastapi-ci.yml +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/.github/workflows/publish.yml +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/.gitignore +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/.readthedocs.yaml +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/Justfile +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/LICENSE +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/docs/conf.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/docs/dev/contributing.md +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/docs/dev/development-notes.md +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/docs/index.md +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/docs/requirements.txt +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/Justfile +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/README.md +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/modern_di_fastapi/__init__.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/modern_di_fastapi/depends.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/modern_di_fastapi/middleware.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/pyproject.toml +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/tests/__init__.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/integrations/fastapi/tests/test_fastapi_di.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/__init__.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/container.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/provider_state.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/py.typed +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/modern_di/scope.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/pyproject.toml +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/tests/__init__.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/tests/creators.py +0 -0
- {modern_di-0.3.0/tests/resolvers → modern_di-0.4.0/tests/providers}/__init__.py +0 -0
- {modern_di-0.3.0/tests/resolvers → modern_di-0.4.0/tests/providers}/test_factory.py +0 -0
- {modern_di-0.3.0/tests/resolvers → modern_di-0.4.0/tests/providers}/test_resource.py +0 -0
- {modern_di-0.3.0/tests/resolvers → modern_di-0.4.0/tests/providers}/test_singleton.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/tests/test_container.py +0 -0
- {modern_di-0.3.0 → modern_di-0.4.0}/tests/test_graph.py +0 -0
modern_di-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: modern-di
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Dependency Injection framework with IOC-container and scopes
|
|
5
|
+
Project-URL: repository, https://github.com/modern-python/modern-di
|
|
6
|
+
Project-URL: docs, https://modern-di.readthedocs.io
|
|
7
|
+
Author-email: Artur Shiriev <me@shiriev.ru>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: dependency injector,di,ioc-container,mocks,python
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: <4,>=3.10
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
"Modern-DI"
|
|
21
|
+
==
|
|
22
|
+
|
|
23
|
+
| Project | Badges |
|
|
24
|
+
|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
25
|
+
| common | [](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration) [](https://github.com/modern-python/modern-di/stargazers) |
|
|
26
|
+
| modern-di | [](https://pypi.python.org/pypi/modern-di ) [](https://pypistats.org/packages/modern-di) |
|
|
27
|
+
| modern-di-fastapi | [](https://pypi.python.org/pypi/modern-di-fastapi) [](https://pypistats.org/packages/modern-di-fastapi) |
|
|
28
|
+
|
|
29
|
+
Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
|
|
30
|
+
|
|
31
|
+
It is in early development state and gives you the following:
|
|
32
|
+
- DI framework with IOC-container and scopes.
|
|
33
|
+
- Async and sync resolving.
|
|
34
|
+
- Python 3.10-3.13 support.
|
|
35
|
+
- Full coverage by types annotations (mypy in strict mode).
|
|
36
|
+
- Overriding dependencies for tests.
|
|
37
|
+
- Package with zero dependencies.
|
|
38
|
+
|
|
39
|
+
📚 [Documentation](https://modern-di.readthedocs.io)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"Modern-DI"
|
|
2
|
+
==
|
|
3
|
+
|
|
4
|
+
| Project | Badges |
|
|
5
|
+
|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
6
|
+
| common | [](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration) [](https://github.com/modern-python/modern-di/stargazers) |
|
|
7
|
+
| modern-di | [](https://pypi.python.org/pypi/modern-di ) [](https://pypistats.org/packages/modern-di) |
|
|
8
|
+
| modern-di-fastapi | [](https://pypi.python.org/pypi/modern-di-fastapi) [](https://pypistats.org/packages/modern-di-fastapi) |
|
|
9
|
+
|
|
10
|
+
Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
|
|
11
|
+
|
|
12
|
+
It is in early development state and gives you the following:
|
|
13
|
+
- DI framework with IOC-container and scopes.
|
|
14
|
+
- Async and sync resolving.
|
|
15
|
+
- Python 3.10-3.13 support.
|
|
16
|
+
- Full coverage by types annotations (mypy in strict mode).
|
|
17
|
+
- Overriding dependencies for tests.
|
|
18
|
+
- Package with zero dependencies.
|
|
19
|
+
|
|
20
|
+
📚 [Documentation](https://modern-di.readthedocs.io)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import typing
|
|
2
2
|
|
|
3
3
|
from modern_di import Container
|
|
4
|
-
from modern_di.providers import
|
|
4
|
+
from modern_di.providers.abstract import AbstractCreatorProvider, AbstractProvider
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
if typing.TYPE_CHECKING:
|
|
@@ -29,11 +29,11 @@ class BaseGraph:
|
|
|
29
29
|
@classmethod
|
|
30
30
|
async def async_resolve_creators(cls, container: Container) -> None:
|
|
31
31
|
for provider in cls.get_providers().values():
|
|
32
|
-
if isinstance(provider,
|
|
32
|
+
if isinstance(provider, AbstractCreatorProvider) and provider.scope == container.scope:
|
|
33
33
|
await provider.async_resolve(container)
|
|
34
34
|
|
|
35
35
|
@classmethod
|
|
36
36
|
def sync_resolve_creators(cls, container: Container) -> None:
|
|
37
37
|
for provider in cls.get_providers().values():
|
|
38
|
-
if isinstance(provider,
|
|
38
|
+
if isinstance(provider, AbstractCreatorProvider) and provider.scope == container.scope:
|
|
39
39
|
provider.sync_resolve(container)
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
from modern_di.providers.
|
|
2
|
-
from modern_di.providers.context_adapter import ContextAdapter
|
|
1
|
+
from modern_di.providers.dict import Dict
|
|
3
2
|
from modern_di.providers.factory import Factory
|
|
3
|
+
from modern_di.providers.list import List
|
|
4
4
|
from modern_di.providers.resource import Resource
|
|
5
|
+
from modern_di.providers.selector import Selector
|
|
5
6
|
from modern_di.providers.singleton import Singleton
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
__all__ = [
|
|
9
|
-
"AbstractProvider",
|
|
10
|
-
"BaseCreatorProvider",
|
|
11
|
-
"ContextAdapter",
|
|
12
10
|
"Factory",
|
|
11
|
+
"Dict",
|
|
12
|
+
"List",
|
|
13
|
+
"Selector",
|
|
13
14
|
"Singleton",
|
|
14
15
|
"Resource",
|
|
15
16
|
]
|
modern_di-0.3.0/modern_di/providers/base.py → modern_di-0.4.0/modern_di/providers/abstract.py
RENAMED
|
@@ -27,18 +27,25 @@ class AbstractProvider(typing.Generic[T_co], abc.ABC):
|
|
|
27
27
|
def sync_resolve(self, container: Container) -> T_co:
|
|
28
28
|
"""Resolve dependency synchronously."""
|
|
29
29
|
|
|
30
|
+
@property
|
|
31
|
+
def cast(self) -> T_co:
|
|
32
|
+
return typing.cast(T_co, self)
|
|
33
|
+
|
|
34
|
+
def _check_providers_scope(self, providers: typing.Iterable[typing.Any]) -> None:
|
|
35
|
+
if any(x.scope > self.scope for x in providers if isinstance(x, AbstractProvider)):
|
|
36
|
+
msg = "Scope of dependency cannot be more than scope of dependent"
|
|
37
|
+
raise RuntimeError(msg)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class AbstractOverrideProvider(AbstractProvider[T_co], abc.ABC):
|
|
30
41
|
def override(self, override_object: object, container: Container) -> None:
|
|
31
42
|
container.override(self.provider_id, override_object)
|
|
32
43
|
|
|
33
44
|
def reset_override(self, container: Container) -> None:
|
|
34
45
|
container.reset_override(self.provider_id)
|
|
35
46
|
|
|
36
|
-
@property
|
|
37
|
-
def cast(self) -> T_co:
|
|
38
|
-
return typing.cast(T_co, self)
|
|
39
|
-
|
|
40
47
|
|
|
41
|
-
class
|
|
48
|
+
class AbstractCreatorProvider(AbstractOverrideProvider[T_co], abc.ABC):
|
|
42
49
|
BASE_SLOTS: typing.ClassVar = [*AbstractProvider.BASE_SLOTS, "_args", "_kwargs", "_creator"]
|
|
43
50
|
|
|
44
51
|
def __init__(
|
|
@@ -49,11 +56,7 @@ class BaseCreatorProvider(AbstractProvider[T_co], abc.ABC):
|
|
|
49
56
|
**kwargs: P.kwargs,
|
|
50
57
|
) -> None:
|
|
51
58
|
super().__init__(scope)
|
|
52
|
-
|
|
53
|
-
if any(x.scope > self.scope for x in itertools.chain(args, kwargs.values()) if isinstance(x, AbstractProvider)):
|
|
54
|
-
msg = "Scope of dependency cannot be more than scope of dependent"
|
|
55
|
-
raise RuntimeError(msg)
|
|
56
|
-
|
|
59
|
+
self._check_providers_scope(itertools.chain(args, kwargs.values()))
|
|
57
60
|
self._creator: typing.Final = creator
|
|
58
61
|
self._args: typing.Final = args
|
|
59
62
|
self._kwargs: typing.Final = kwargs
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from modern_di import Container
|
|
5
|
+
from modern_di.providers.abstract import AbstractProvider
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Dict(AbstractProvider[dict[str, T_co]]):
|
|
12
|
+
__slots__ = [*AbstractProvider.BASE_SLOTS, "_providers"]
|
|
13
|
+
|
|
14
|
+
def __init__(self, scope: enum.IntEnum, **providers: AbstractProvider[T_co]) -> None:
|
|
15
|
+
super().__init__(scope)
|
|
16
|
+
self._check_providers_scope(providers.values())
|
|
17
|
+
self._providers: typing.Final = providers
|
|
18
|
+
|
|
19
|
+
async def async_resolve(self, container: Container) -> dict[str, T_co]:
|
|
20
|
+
return {key: await provider.async_resolve(container) for key, provider in self._providers.items()}
|
|
21
|
+
|
|
22
|
+
def sync_resolve(self, container: Container) -> dict[str, T_co]:
|
|
23
|
+
return {key: provider.sync_resolve(container) for key, provider in self._providers.items()}
|
|
@@ -2,15 +2,15 @@ import enum
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
from modern_di import Container
|
|
5
|
-
from modern_di.providers import
|
|
5
|
+
from modern_di.providers.abstract import AbstractCreatorProvider
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
9
|
P = typing.ParamSpec("P")
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
class Factory(
|
|
13
|
-
__slots__ = [*
|
|
12
|
+
class Factory(AbstractCreatorProvider[T_co]):
|
|
13
|
+
__slots__ = [*AbstractCreatorProvider.BASE_SLOTS, "_creator"]
|
|
14
14
|
|
|
15
15
|
def __init__(
|
|
16
16
|
self,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from modern_di import Container
|
|
5
|
+
from modern_di.providers.abstract import AbstractProvider
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class List(AbstractProvider[list[T_co]]):
|
|
12
|
+
__slots__ = [*AbstractProvider.BASE_SLOTS, "_providers"]
|
|
13
|
+
|
|
14
|
+
def __init__(self, scope: enum.IntEnum, *providers: AbstractProvider[T_co]) -> None:
|
|
15
|
+
super().__init__(scope)
|
|
16
|
+
self._check_providers_scope(providers)
|
|
17
|
+
self._providers: typing.Final = providers
|
|
18
|
+
|
|
19
|
+
async def async_resolve(self, container: Container) -> list[T_co]:
|
|
20
|
+
return [await x.async_resolve(container) for x in self._providers]
|
|
21
|
+
|
|
22
|
+
def sync_resolve(self, container: Container) -> list[T_co]:
|
|
23
|
+
return [x.sync_resolve(container) for x in self._providers]
|
|
@@ -4,15 +4,15 @@ import inspect
|
|
|
4
4
|
import typing
|
|
5
5
|
|
|
6
6
|
from modern_di import Container
|
|
7
|
-
from modern_di.providers import
|
|
7
|
+
from modern_di.providers.abstract import AbstractCreatorProvider
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
11
11
|
P = typing.ParamSpec("P")
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
class Resource(
|
|
15
|
-
__slots__ = [*
|
|
14
|
+
class Resource(AbstractCreatorProvider[T_co]):
|
|
15
|
+
__slots__ = [*AbstractCreatorProvider.BASE_SLOTS, "_creator", "_args", "_kwargs", "_is_async"]
|
|
16
16
|
|
|
17
17
|
def _is_creator_async(
|
|
18
18
|
self,
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from modern_di import Container
|
|
5
|
+
from modern_di.providers.abstract import AbstractProvider
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
P = typing.ParamSpec("P")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Selector(AbstractProvider[T_co]):
|
|
13
|
+
__slots__ = [*AbstractProvider.BASE_SLOTS, "_function", "_providers"]
|
|
14
|
+
|
|
15
|
+
def __init__(
|
|
16
|
+
self, scope: enum.IntEnum, function: typing.Callable[..., str], **providers: AbstractProvider[T_co]
|
|
17
|
+
) -> None:
|
|
18
|
+
super().__init__(scope)
|
|
19
|
+
self._check_providers_scope(providers.values())
|
|
20
|
+
self._function: typing.Final = function
|
|
21
|
+
self._providers: typing.Final = providers
|
|
22
|
+
|
|
23
|
+
async def async_resolve(self, container: Container) -> T_co:
|
|
24
|
+
container = container.find_container(self.scope)
|
|
25
|
+
selected_key = self._function(**container.context)
|
|
26
|
+
if selected_key not in self._providers:
|
|
27
|
+
msg = f"No provider matches {selected_key}"
|
|
28
|
+
raise RuntimeError(msg)
|
|
29
|
+
|
|
30
|
+
return await self._providers[selected_key].async_resolve(container)
|
|
31
|
+
|
|
32
|
+
def sync_resolve(self, container: Container) -> T_co:
|
|
33
|
+
container = container.find_container(self.scope)
|
|
34
|
+
selected_key = self._function(**container.context)
|
|
35
|
+
if selected_key not in self._providers:
|
|
36
|
+
msg = f"No provider matches {selected_key}"
|
|
37
|
+
raise RuntimeError(msg)
|
|
38
|
+
|
|
39
|
+
return self._providers[selected_key].sync_resolve(container)
|
|
@@ -2,15 +2,15 @@ import enum
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
from modern_di import Container
|
|
5
|
-
from modern_di.providers import
|
|
5
|
+
from modern_di.providers.abstract import AbstractCreatorProvider
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
9
|
P = typing.ParamSpec("P")
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
class Singleton(
|
|
13
|
-
__slots__ = [*
|
|
12
|
+
class Singleton(AbstractCreatorProvider[T_co]):
|
|
13
|
+
__slots__ = [*AbstractCreatorProvider.BASE_SLOTS, "_creator"]
|
|
14
14
|
|
|
15
15
|
def __init__(
|
|
16
16
|
self,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from modern_di import Container, Scope, providers
|
|
4
|
+
from tests.creators import create_async_resource, create_sync_resource
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
async_resource = providers.Resource(Scope.APP, create_async_resource)
|
|
8
|
+
sync_resource = providers.Resource(Scope.APP, create_sync_resource)
|
|
9
|
+
mapping = providers.Dict(Scope.APP, dep1=async_resource, dep2=sync_resource)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def test_dict() -> None:
|
|
13
|
+
async with Container(scope=Scope.APP, context={"option": "app"}) as app_container:
|
|
14
|
+
mapping1 = await mapping.async_resolve(app_container)
|
|
15
|
+
mapping2 = mapping.sync_resolve(app_container)
|
|
16
|
+
resource1 = await async_resource.async_resolve(app_container)
|
|
17
|
+
resource2 = sync_resource.sync_resolve(app_container)
|
|
18
|
+
assert mapping1 == mapping2 == {"dep1": resource1, "dep2": resource2}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def test_dict_wrong_scope() -> None:
|
|
22
|
+
request_factory_ = providers.Factory(Scope.REQUEST, lambda: "")
|
|
23
|
+
with pytest.raises(RuntimeError, match="Scope of dependency cannot be more than scope of dependent"):
|
|
24
|
+
providers.Dict(Scope.APP, dep1=request_factory_)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from modern_di import Container, Scope, providers
|
|
4
|
+
from tests.creators import create_async_resource, create_sync_resource
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
async_resource = providers.Resource(Scope.APP, create_async_resource)
|
|
8
|
+
sync_resource = providers.Resource(Scope.APP, create_sync_resource)
|
|
9
|
+
sequence = providers.List(Scope.APP, async_resource, sync_resource)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def test_list() -> None:
|
|
13
|
+
async with Container(scope=Scope.APP, context={"option": "app"}) as app_container:
|
|
14
|
+
sequence1 = await sequence.async_resolve(app_container)
|
|
15
|
+
sequence2 = sequence.sync_resolve(app_container)
|
|
16
|
+
resource1 = await async_resource.async_resolve(app_container)
|
|
17
|
+
resource2 = sync_resource.sync_resolve(app_container)
|
|
18
|
+
assert sequence1 == sequence2 == [resource1, resource2]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def test_list_wrong_scope() -> None:
|
|
22
|
+
request_factory_ = providers.Factory(Scope.REQUEST, lambda: "")
|
|
23
|
+
with pytest.raises(RuntimeError, match="Scope of dependency cannot be more than scope of dependent"):
|
|
24
|
+
providers.List(Scope.APP, request_factory_)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from modern_di import Container, Scope, providers
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def selector_function(*, option: str, **_: object) -> "str":
|
|
7
|
+
return option
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
app_factory = providers.Factory(Scope.APP, lambda: "app")
|
|
11
|
+
request_factory = providers.Factory(Scope.APP, lambda: "request")
|
|
12
|
+
app_selector = providers.Selector(Scope.APP, selector_function, app=app_factory, request=request_factory)
|
|
13
|
+
request_selector = providers.Selector(Scope.REQUEST, selector_function, app=app_factory, request=request_factory)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async def test_selector() -> None:
|
|
17
|
+
async with Container(scope=Scope.APP, context={"option": "app"}) as app_container:
|
|
18
|
+
instance1 = await app_selector.async_resolve(app_container)
|
|
19
|
+
instance2 = app_selector.sync_resolve(app_container)
|
|
20
|
+
assert instance1 == instance2 == "app"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def test_selector_in_request_scope() -> None:
|
|
24
|
+
async with (
|
|
25
|
+
Container(scope=Scope.APP) as app_container,
|
|
26
|
+
app_container.build_child_container(context={"option": "request"}) as request_container,
|
|
27
|
+
):
|
|
28
|
+
instance1 = await request_selector.async_resolve(request_container)
|
|
29
|
+
instance2 = request_selector.sync_resolve(request_container)
|
|
30
|
+
assert instance1 == instance2 == "request"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
async def test_selector_no_match() -> None:
|
|
34
|
+
async with Container(scope=Scope.APP, context={"option": "wrong"}) as app_container:
|
|
35
|
+
with pytest.raises(RuntimeError, match="No provider matches wrong"):
|
|
36
|
+
await app_selector.async_resolve(app_container)
|
|
37
|
+
|
|
38
|
+
with pytest.raises(RuntimeError, match="No provider matches wrong"):
|
|
39
|
+
app_selector.sync_resolve(app_container)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
async def test_selector_wrong_scope() -> None:
|
|
43
|
+
request_factory_ = providers.Factory(Scope.REQUEST, lambda: "")
|
|
44
|
+
with pytest.raises(RuntimeError, match="Scope of dependency cannot be more than scope of dependent"):
|
|
45
|
+
providers.Selector(Scope.APP, lambda: "", request=request_factory_)
|
modern_di-0.3.0/PKG-INFO
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.3
|
|
2
|
-
Name: modern-di
|
|
3
|
-
Version: 0.3.0
|
|
4
|
-
Summary: Dependency Injection framework with IOC-container and scopes
|
|
5
|
-
Project-URL: repository, https://github.com/modern-python/modern-di
|
|
6
|
-
Project-URL: docs, https://modern-di.readthedocs.io
|
|
7
|
-
Author-email: Artur Shiriev <me@shiriev.ru>
|
|
8
|
-
License-Expression: MIT
|
|
9
|
-
License-File: LICENSE
|
|
10
|
-
Keywords: dependency injector,di,ioc-container,mocks,python
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
-
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
-
Classifier: Typing :: Typed
|
|
17
|
-
Requires-Python: <4,>=3.10
|
|
18
|
-
Description-Content-Type: text/markdown
|
|
19
|
-
|
|
20
|
-
"Modern-DI"
|
|
21
|
-
==
|
|
22
|
-
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
|
|
23
|
-
[](https://pypi.python.org/pypi/modern-di)
|
|
24
|
-
[](https://pypistats.org/packages/modern-di)
|
|
25
|
-
[](https://github.com/modern-python/modern-di/stargazers)
|
|
26
|
-
|
|
27
|
-
Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
|
|
28
|
-
|
|
29
|
-
It is in early development state and gives you the following:
|
|
30
|
-
- DI framework with IOC-container and scopes.
|
|
31
|
-
- Async and sync resolving.
|
|
32
|
-
- Python 3.10-3.13 support.
|
|
33
|
-
- Full coverage by types annotations (mypy in strict mode).
|
|
34
|
-
- Overriding dependencies for tests.
|
|
35
|
-
- Package with zero dependencies.
|
|
36
|
-
|
|
37
|
-
📚 [Documentation](https://modern-di.readthedocs.io)
|
modern_di-0.3.0/README.md
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"Modern-DI"
|
|
2
|
-
==
|
|
3
|
-
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
|
|
4
|
-
[](https://pypi.python.org/pypi/modern-di)
|
|
5
|
-
[](https://pypistats.org/packages/modern-di)
|
|
6
|
-
[](https://github.com/modern-python/modern-di/stargazers)
|
|
7
|
-
|
|
8
|
-
Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
|
|
9
|
-
|
|
10
|
-
It is in early development state and gives you the following:
|
|
11
|
-
- DI framework with IOC-container and scopes.
|
|
12
|
-
- Async and sync resolving.
|
|
13
|
-
- Python 3.10-3.13 support.
|
|
14
|
-
- Full coverage by types annotations (mypy in strict mode).
|
|
15
|
-
- Overriding dependencies for tests.
|
|
16
|
-
- Package with zero dependencies.
|
|
17
|
-
|
|
18
|
-
📚 [Documentation](https://modern-di.readthedocs.io)
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import enum
|
|
2
|
-
import typing
|
|
3
|
-
|
|
4
|
-
from modern_di import Container
|
|
5
|
-
from modern_di.providers import AbstractProvider
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
-
P = typing.ParamSpec("P")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class ContextAdapter(AbstractProvider[T_co]):
|
|
13
|
-
__slots__ = [*AbstractProvider.BASE_SLOTS, "_function"]
|
|
14
|
-
|
|
15
|
-
def __init__(
|
|
16
|
-
self,
|
|
17
|
-
scope: enum.IntEnum,
|
|
18
|
-
function: typing.Callable[..., T_co],
|
|
19
|
-
) -> None:
|
|
20
|
-
super().__init__(scope)
|
|
21
|
-
self._function = function
|
|
22
|
-
|
|
23
|
-
async def async_resolve(self, container: Container) -> T_co:
|
|
24
|
-
return self.sync_resolve(container)
|
|
25
|
-
|
|
26
|
-
def sync_resolve(self, container: Container) -> T_co:
|
|
27
|
-
container = container.find_container(self.scope)
|
|
28
|
-
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
29
|
-
return typing.cast(T_co, override)
|
|
30
|
-
|
|
31
|
-
return self._function(**container.context)
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import datetime
|
|
2
|
-
|
|
3
|
-
from modern_di import Container, Scope, providers
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def context_adapter_function(*, now: datetime.datetime, **_: object) -> datetime.datetime:
|
|
7
|
-
return now
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
context_adapter = providers.ContextAdapter(Scope.APP, context_adapter_function)
|
|
11
|
-
request_context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_function)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
async def test_context_adapter() -> None:
|
|
15
|
-
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
16
|
-
async with Container(scope=Scope.APP, context={"now": now}) as app_container:
|
|
17
|
-
instance1 = await context_adapter.async_resolve(app_container)
|
|
18
|
-
instance2 = context_adapter.sync_resolve(app_container)
|
|
19
|
-
assert instance1 is instance2 is now
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
async def test_context_adapter_in_request_scope() -> None:
|
|
23
|
-
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
24
|
-
async with (
|
|
25
|
-
Container(scope=Scope.APP) as app_container,
|
|
26
|
-
app_container.build_child_container(context={"now": now}) as request_container,
|
|
27
|
-
):
|
|
28
|
-
instance1 = await request_context_adapter.async_resolve(request_container)
|
|
29
|
-
instance2 = request_context_adapter.sync_resolve(request_container)
|
|
30
|
-
assert instance1 is instance2 is now
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
async def test_context_adapter_override() -> None:
|
|
34
|
-
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
35
|
-
async with Container(scope=Scope.APP, context={"now": now}) as app_container:
|
|
36
|
-
instance1 = await context_adapter.async_resolve(app_container)
|
|
37
|
-
context_adapter.override(datetime.datetime.now(tz=datetime.timezone.utc), container=app_container)
|
|
38
|
-
instance2 = context_adapter.sync_resolve(app_container)
|
|
39
|
-
assert instance1 is now
|
|
40
|
-
assert instance2 is not now
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|