modern-di 0.8.0__tar.gz → 0.10.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.8.0 → modern_di-0.10.0}/PKG-INFO +4 -3
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/container.py +6 -6
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/provider_state.py +1 -1
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/abstract.py +21 -23
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/factory.py +7 -3
- modern_di-0.10.0/modern_di/providers/injected_factory.py +45 -0
- modern_di-0.8.0/modern_di/providers/injected_factory.py +0 -29
- {modern_di-0.8.0 → modern_di-0.10.0}/.gitignore +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/__init__.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/graph.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/__init__.py +2 -2
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/container_provider.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/context_adapter.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/dict.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/list.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/resource.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/selector.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/providers/singleton.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/py.typed +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/modern_di/scope.py +0 -0
- {modern_di-0.8.0 → modern_di-0.10.0}/pyproject.toml +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: modern-di
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.10.0
|
|
4
4
|
Summary: Dependency Injection framework with IOC-container and scopes
|
|
5
5
|
Project-URL: repository, https://github.com/modern-python/modern-di
|
|
6
6
|
Project-URL: docs, https://modern-di.readthedocs.io
|
|
7
7
|
Author-email: Artur Shiriev <me@shiriev.ru>
|
|
8
|
-
License: MIT
|
|
8
|
+
License-Expression: MIT
|
|
9
9
|
Keywords: DI,dependency injector,ioc-container,mocks,python
|
|
10
10
|
Classifier: Programming Language :: Python :: 3.10
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.11
|
|
@@ -36,6 +36,7 @@ It is in development state yet and gives you the following:
|
|
|
36
36
|
- Overriding dependencies for tests.
|
|
37
37
|
- Package with zero dependencies.
|
|
38
38
|
- Integration with FastAPI and LiteStar
|
|
39
|
+
- Thread-safe and asyncio concurrency safe providers
|
|
39
40
|
|
|
40
41
|
📚 [Documentation](https://modern-di.readthedocs.io)
|
|
41
42
|
|
|
@@ -13,15 +13,15 @@ if typing.TYPE_CHECKING:
|
|
|
13
13
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
16
|
+
class Container(contextlib.AbstractAsyncContextManager["Container"], contextlib.AbstractContextManager["Container"]):
|
|
17
17
|
__slots__ = (
|
|
18
|
-
"scope",
|
|
19
|
-
"parent_container",
|
|
20
|
-
"context",
|
|
21
18
|
"_is_async",
|
|
22
|
-
"_provider_states",
|
|
23
19
|
"_overrides",
|
|
20
|
+
"_provider_states",
|
|
24
21
|
"_use_threading_lock",
|
|
22
|
+
"context",
|
|
23
|
+
"parent_container",
|
|
24
|
+
"scope",
|
|
25
25
|
)
|
|
26
26
|
|
|
27
27
|
def __init__(
|
|
@@ -37,7 +37,7 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
|
37
37
|
self.context: dict[str, typing.Any] = context or {}
|
|
38
38
|
self._is_async: bool | None = None
|
|
39
39
|
self._provider_states: dict[str, ProviderState[typing.Any]] = {}
|
|
40
|
-
self._overrides: dict[str, typing.Any] = {}
|
|
40
|
+
self._overrides: dict[str, typing.Any] = parent_container._overrides if parent_container else {} # noqa: SLF001
|
|
41
41
|
self._use_threading_lock = use_threading_lock
|
|
42
42
|
|
|
43
43
|
def _exit(self) -> None:
|
|
@@ -8,7 +8,7 @@ T_co = typing.TypeVar("T_co", covariant=True)
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class ProviderState(typing.Generic[T_co]):
|
|
11
|
-
__slots__ = "
|
|
11
|
+
__slots__ = "asyncio_lock", "context_stack", "instance", "threading_lock"
|
|
12
12
|
|
|
13
13
|
def __init__(self, use_asyncio_lock: bool, use_threading_lock: bool) -> None:
|
|
14
14
|
self.context_stack: contextlib.AsyncExitStack | contextlib.ExitStack | None = None
|
|
@@ -39,10 +39,10 @@ class AbstractProvider(typing.Generic[T_co], abc.ABC):
|
|
|
39
39
|
|
|
40
40
|
class AbstractOverrideProvider(AbstractProvider[T_co], abc.ABC):
|
|
41
41
|
def override(self, override_object: object, container: Container) -> None:
|
|
42
|
-
container.
|
|
42
|
+
container.override(self.provider_id, override_object)
|
|
43
43
|
|
|
44
44
|
def reset_override(self, container: Container) -> None:
|
|
45
|
-
container.
|
|
45
|
+
container.reset_override(self.provider_id)
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
class AbstractCreatorProvider(AbstractOverrideProvider[T_co], abc.ABC):
|
|
@@ -61,31 +61,29 @@ class AbstractCreatorProvider(AbstractOverrideProvider[T_co], abc.ABC):
|
|
|
61
61
|
self._args: typing.Final = args
|
|
62
62
|
self._kwargs: typing.Final = kwargs
|
|
63
63
|
|
|
64
|
+
def _sync_resolve_args(self, container: Container) -> list[typing.Any]:
|
|
65
|
+
return [x.sync_resolve(container) if isinstance(x, AbstractProvider) else x for x in self._args]
|
|
66
|
+
|
|
67
|
+
def _sync_resolve_kwargs(self, container: Container) -> dict[str, typing.Any]:
|
|
68
|
+
return {k: v.sync_resolve(container) if isinstance(v, AbstractProvider) else v for k, v in self._kwargs.items()}
|
|
69
|
+
|
|
64
70
|
def _sync_build_creator(self, container: Container) -> typing.Any: # noqa: ANN401
|
|
65
71
|
return self._creator(
|
|
66
|
-
*typing.cast(
|
|
67
|
-
|
|
68
|
-
),
|
|
69
|
-
**typing.cast(
|
|
70
|
-
P.kwargs,
|
|
71
|
-
{
|
|
72
|
-
k: v.sync_resolve(container) if isinstance(v, AbstractProvider) else v
|
|
73
|
-
for k, v in self._kwargs.items()
|
|
74
|
-
},
|
|
75
|
-
),
|
|
72
|
+
*typing.cast(P.args, self._sync_resolve_args(container)),
|
|
73
|
+
**typing.cast(P.kwargs, self._sync_resolve_kwargs(container)),
|
|
76
74
|
)
|
|
77
75
|
|
|
76
|
+
async def _async_resolve_args(self, container: Container) -> list[typing.Any]:
|
|
77
|
+
return [await x.async_resolve(container) if isinstance(x, AbstractProvider) else x for x in self._args]
|
|
78
|
+
|
|
79
|
+
async def _async_resolve_kwargs(self, container: Container) -> dict[str, typing.Any]:
|
|
80
|
+
return {
|
|
81
|
+
k: await v.async_resolve(container) if isinstance(v, AbstractProvider) else v
|
|
82
|
+
for k, v in self._kwargs.items()
|
|
83
|
+
}
|
|
84
|
+
|
|
78
85
|
async def _async_build_creator(self, container: Container) -> typing.Any: # noqa: ANN401
|
|
79
86
|
return self._creator(
|
|
80
|
-
*typing.cast(
|
|
81
|
-
|
|
82
|
-
[await x.async_resolve(container) if isinstance(x, AbstractProvider) else x for x in self._args],
|
|
83
|
-
),
|
|
84
|
-
**typing.cast(
|
|
85
|
-
P.kwargs,
|
|
86
|
-
{
|
|
87
|
-
k: await v.async_resolve(container) if isinstance(v, AbstractProvider) else v
|
|
88
|
-
for k, v in self._kwargs.items()
|
|
89
|
-
},
|
|
90
|
-
),
|
|
87
|
+
*typing.cast(P.args, await self._async_resolve_args(container)),
|
|
88
|
+
**typing.cast(P.kwargs, await self._async_resolve_kwargs(container)),
|
|
91
89
|
)
|
|
@@ -3,7 +3,7 @@ import typing
|
|
|
3
3
|
|
|
4
4
|
from modern_di import Container
|
|
5
5
|
from modern_di.providers.abstract import AbstractCreatorProvider
|
|
6
|
-
from modern_di.providers.injected_factory import
|
|
6
|
+
from modern_di.providers.injected_factory import AsyncInjectedFactory, SyncInjectedFactory
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
@@ -23,8 +23,12 @@ class Factory(AbstractCreatorProvider[T_co]):
|
|
|
23
23
|
super().__init__(scope, creator, *args, **kwargs)
|
|
24
24
|
|
|
25
25
|
@property
|
|
26
|
-
def
|
|
27
|
-
return
|
|
26
|
+
def sync_provider(self) -> SyncInjectedFactory[T_co]:
|
|
27
|
+
return SyncInjectedFactory(self)
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def async_provider(self) -> AsyncInjectedFactory[T_co]:
|
|
31
|
+
return AsyncInjectedFactory(self)
|
|
28
32
|
|
|
29
33
|
async def async_resolve(self, container: Container) -> T_co:
|
|
30
34
|
container = container.find_container(self.scope)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import functools
|
|
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 SyncInjectedFactory(AbstractProvider[T_co]):
|
|
13
|
+
__slots__ = [*AbstractProvider.BASE_SLOTS, "_factory_provider"]
|
|
14
|
+
|
|
15
|
+
def __init__(self, factory_provider: AbstractProvider[T_co]) -> None:
|
|
16
|
+
super().__init__(factory_provider.scope)
|
|
17
|
+
self._factory_provider = factory_provider
|
|
18
|
+
|
|
19
|
+
async def async_resolve(self, container: Container) -> typing.Callable[[], T_co]: # type: ignore[override]
|
|
20
|
+
return self.sync_resolve(container)
|
|
21
|
+
|
|
22
|
+
def sync_resolve(self, container: Container) -> typing.Callable[[], T_co]: # type: ignore[override]
|
|
23
|
+
return functools.partial(self._factory_provider.sync_resolve, container)
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def cast(self) -> typing.Callable[[], T_co]: # type: ignore[override]
|
|
27
|
+
return typing.cast(typing.Callable[[], T_co], self)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class AsyncInjectedFactory(AbstractProvider[T_co]):
|
|
31
|
+
__slots__ = [*AbstractProvider.BASE_SLOTS, "_factory_provider"]
|
|
32
|
+
|
|
33
|
+
def __init__(self, factory_provider: AbstractProvider[T_co]) -> None:
|
|
34
|
+
super().__init__(factory_provider.scope)
|
|
35
|
+
self._factory_provider = factory_provider
|
|
36
|
+
|
|
37
|
+
async def async_resolve(self, container: Container) -> typing.Callable[[], typing.Awaitable[T_co]]: # type: ignore[override]
|
|
38
|
+
return self.sync_resolve(container)
|
|
39
|
+
|
|
40
|
+
def sync_resolve(self, container: Container) -> typing.Callable[[], typing.Awaitable[T_co]]: # type: ignore[override]
|
|
41
|
+
return functools.partial(self._factory_provider.async_resolve, container)
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def cast(self) -> typing.Callable[[], typing.Awaitable[T_co]]: # type: ignore[override]
|
|
45
|
+
return typing.cast(typing.Callable[[], typing.Awaitable[T_co]], self)
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import functools
|
|
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 InjectedFactory(AbstractProvider[T_co]):
|
|
13
|
-
__slots__ = [*AbstractProvider.BASE_SLOTS, "_factory_provider"]
|
|
14
|
-
|
|
15
|
-
def __init__(self, factory_provider: AbstractProvider[T_co]) -> None:
|
|
16
|
-
super().__init__(factory_provider.scope)
|
|
17
|
-
self._factory_provider = factory_provider
|
|
18
|
-
|
|
19
|
-
async def async_resolve(self, container: Container) -> typing.Callable[[], T_co]: # type: ignore[override]
|
|
20
|
-
await self._factory_provider.async_resolve(container)
|
|
21
|
-
return functools.partial(self._factory_provider.sync_resolve, container)
|
|
22
|
-
|
|
23
|
-
def sync_resolve(self, container: Container) -> typing.Callable[[], T_co]: # type: ignore[override]
|
|
24
|
-
self._factory_provider.sync_resolve(container)
|
|
25
|
-
return functools.partial(self._factory_provider.sync_resolve, container)
|
|
26
|
-
|
|
27
|
-
@property
|
|
28
|
-
def cast(self) -> typing.Callable[[], T_co]: # type: ignore[override]
|
|
29
|
-
return typing.cast(typing.Callable[[], T_co], self)
|
|
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
|