modern-di 0.2.1__tar.gz → 0.3.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.2.1 → modern_di-0.3.0}/.github/workflows/publish.yml +5 -1
- {modern_di-0.2.1 → modern_di-0.3.0}/PKG-INFO +1 -1
- {modern_di-0.2.1 → modern_di-0.3.0}/docs/dev/development-notes.md +2 -2
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/modern_di_fastapi/depends.py +4 -4
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/tests/test_fastapi_di.py +4 -4
- {modern_di-0.2.1 → modern_di-0.3.0}/modern_di/__init__.py +1 -1
- {modern_di-0.2.1 → modern_di-0.3.0}/modern_di/container.py +21 -21
- modern_di-0.3.0/modern_di/graph.py +39 -0
- modern_di-0.2.1/modern_di/resolver_state.py → modern_di-0.3.0/modern_di/provider_state.py +3 -3
- modern_di-0.3.0/modern_di/providers/__init__.py +15 -0
- {modern_di-0.2.1/modern_di/resolvers → modern_di-0.3.0/modern_di/providers}/base.py +12 -12
- {modern_di-0.2.1/modern_di/resolvers → modern_di-0.3.0/modern_di/providers}/context_adapter.py +4 -4
- {modern_di-0.2.1/modern_di/resolvers → modern_di-0.3.0/modern_di/providers}/factory.py +5 -5
- {modern_di-0.2.1/modern_di/resolvers → modern_di-0.3.0/modern_di/providers}/resource.py +25 -25
- modern_di-0.3.0/modern_di/providers/singleton.py +56 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/resolvers/test_context_adapter.py +3 -3
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/resolvers/test_factory.py +5 -5
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/resolvers/test_resource.py +5 -5
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/resolvers/test_singleton.py +7 -7
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/test_container.py +1 -1
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/test_graph.py +7 -7
- modern_di-0.2.1/modern_di/graph.py +0 -39
- modern_di-0.2.1/modern_di/resolvers/__init__.py +0 -15
- modern_di-0.2.1/modern_di/resolvers/singleton.py +0 -56
- {modern_di-0.2.1 → modern_di-0.3.0}/.github/workflows/core-ci.yml +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/.github/workflows/fastapi-ci.yml +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/.gitignore +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/.readthedocs.yaml +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/Justfile +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/LICENSE +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/README.md +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/docs/conf.py +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/docs/dev/contributing.md +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/docs/index.md +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/docs/requirements.txt +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/Justfile +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/README.md +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/modern_di_fastapi/__init__.py +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/modern_di_fastapi/middleware.py +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/pyproject.toml +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/integrations/fastapi/tests/__init__.py +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/modern_di/py.typed +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/modern_di/scope.py +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/pyproject.toml +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/__init__.py +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/creators.py +0 -0
- {modern_di-0.2.1 → modern_di-0.3.0}/tests/resolvers/__init__.py +0 -0
|
@@ -23,6 +23,9 @@ jobs:
|
|
|
23
23
|
publish-fastapi:
|
|
24
24
|
if: startsWith(github.ref_name, 'fastapi')
|
|
25
25
|
runs-on: ubuntu-latest
|
|
26
|
+
defaults:
|
|
27
|
+
run:
|
|
28
|
+
working-directory: integrations/fastapi
|
|
26
29
|
steps:
|
|
27
30
|
- uses: actions/checkout@v4
|
|
28
31
|
- uses: extractions/setup-just@v2
|
|
@@ -31,7 +34,8 @@ jobs:
|
|
|
31
34
|
enable-cache: true
|
|
32
35
|
cache-dependency-glob: "pyproject.toml"
|
|
33
36
|
- run: |
|
|
34
|
-
|
|
37
|
+
export SETUPTOOLS_SCM_PRETEND_VERSION=$(echo "${{ github.ref_name }}" | sed 's/^fastapi-//; s/-.*$//')
|
|
38
|
+
echo "SETUPTOOLS_SCM_PRETEND_VERSION=$SETUPTOOLS_SCM_PRETEND_VERSION"
|
|
35
39
|
just publish
|
|
36
40
|
env:
|
|
37
41
|
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: modern-di
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.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
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
- context stacks for resources
|
|
10
10
|
- overrides
|
|
11
11
|
|
|
12
|
-
###
|
|
12
|
+
### Providers
|
|
13
13
|
- completely stateless
|
|
14
14
|
- if dependency is already saved or overridden in `Container`, returns it
|
|
15
15
|
- otherwise build dependency and save it to `Container`
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
### Graph
|
|
19
19
|
- Cannot be instantiated
|
|
20
|
-
- Contains graph of `
|
|
20
|
+
- Contains graph of `Providers`
|
|
21
21
|
- Can initialize its resources and factories to container
|
|
22
22
|
|
|
23
23
|
### Questions
|
|
@@ -2,7 +2,7 @@ import dataclasses
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
import fastapi
|
|
5
|
-
import
|
|
5
|
+
from modern_di import Container, providers
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
@@ -10,15 +10,15 @@ T_co = typing.TypeVar("T_co", covariant=True)
|
|
|
10
10
|
|
|
11
11
|
@dataclasses.dataclass(slots=True, frozen=True)
|
|
12
12
|
class Dependency(typing.Generic[T_co]):
|
|
13
|
-
dependency:
|
|
13
|
+
dependency: providers.AbstractProvider[T_co]
|
|
14
14
|
|
|
15
15
|
async def __call__(self, request: fastapi.Request) -> T_co:
|
|
16
16
|
return await self.dependency.async_resolve(request.state.modern_di_container)
|
|
17
17
|
|
|
18
18
|
|
|
19
|
-
def FromDI(dependency:
|
|
19
|
+
def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
20
20
|
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def setup_modern_di(container:
|
|
23
|
+
def setup_modern_di(container: Container, app: fastapi.FastAPI) -> None:
|
|
24
24
|
app.state.modern_di_container = container
|
|
@@ -7,7 +7,7 @@ import httpx
|
|
|
7
7
|
import modern_di
|
|
8
8
|
import pytest
|
|
9
9
|
from asgi_lifespan import LifespanManager
|
|
10
|
-
from modern_di import Scope,
|
|
10
|
+
from modern_di import Scope, providers
|
|
11
11
|
from starlette import status
|
|
12
12
|
from starlette.requests import Request
|
|
13
13
|
|
|
@@ -41,9 +41,9 @@ def context_adapter_function(*, request: Request, **_: object) -> str:
|
|
|
41
41
|
return request.method
|
|
42
42
|
|
|
43
43
|
|
|
44
|
-
app_factory =
|
|
45
|
-
request_factory =
|
|
46
|
-
context_adapter =
|
|
44
|
+
app_factory = providers.Factory(Scope.APP, SimpleCreator, dep1="original")
|
|
45
|
+
request_factory = providers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
|
|
46
|
+
context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_function)
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
@app.get("/")
|
|
@@ -3,7 +3,7 @@ import enum
|
|
|
3
3
|
import types
|
|
4
4
|
import typing
|
|
5
5
|
|
|
6
|
-
from modern_di.
|
|
6
|
+
from modern_di.provider_state import ProviderState
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
if typing.TYPE_CHECKING:
|
|
@@ -14,7 +14,7 @@ T_co = typing.TypeVar("T_co", covariant=True)
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
17
|
-
__slots__ = "scope", "parent_container", "context", "_is_async", "
|
|
17
|
+
__slots__ = "scope", "parent_container", "context", "_is_async", "_provider_states", "_overrides"
|
|
18
18
|
|
|
19
19
|
def __init__(
|
|
20
20
|
self,
|
|
@@ -31,12 +31,12 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
|
31
31
|
self.parent_container = parent_container
|
|
32
32
|
self.context: dict[str, typing.Any] = context or {}
|
|
33
33
|
self._is_async: bool | None = None
|
|
34
|
-
self.
|
|
34
|
+
self._provider_states: dict[str, ProviderState[typing.Any]] = {}
|
|
35
35
|
self._overrides: dict[str, typing.Any] = {}
|
|
36
36
|
|
|
37
37
|
def _exit(self) -> None:
|
|
38
38
|
self._is_async = None
|
|
39
|
-
self.
|
|
39
|
+
self._provider_states = {}
|
|
40
40
|
self._overrides = {}
|
|
41
41
|
|
|
42
42
|
def _check_entered(self) -> None:
|
|
@@ -61,30 +61,30 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
|
61
61
|
container = typing.cast("typing_extensions.Self", container.parent_container)
|
|
62
62
|
return container
|
|
63
63
|
|
|
64
|
-
def
|
|
65
|
-
self,
|
|
66
|
-
) ->
|
|
64
|
+
def fetch_provider_state(
|
|
65
|
+
self, provider_id: str, is_async_resource: bool = False, is_lock_required: bool = False
|
|
66
|
+
) -> ProviderState[typing.Any]:
|
|
67
67
|
self._check_entered()
|
|
68
68
|
if is_async_resource and self._is_async is False:
|
|
69
69
|
msg = "Resolving async resource in sync container is not allowed"
|
|
70
70
|
raise RuntimeError(msg)
|
|
71
71
|
|
|
72
|
-
if
|
|
73
|
-
self.
|
|
72
|
+
if provider_id not in self._provider_states:
|
|
73
|
+
self._provider_states[provider_id] = ProviderState(is_lock_required=is_lock_required)
|
|
74
74
|
|
|
75
|
-
return self.
|
|
75
|
+
return self._provider_states[provider_id]
|
|
76
76
|
|
|
77
|
-
def override(self,
|
|
78
|
-
self._overrides[
|
|
77
|
+
def override(self, provider_id: str, override_object: object) -> None:
|
|
78
|
+
self._overrides[provider_id] = override_object
|
|
79
79
|
|
|
80
|
-
def fetch_override(self,
|
|
81
|
-
return self._overrides.get(
|
|
80
|
+
def fetch_override(self, provider_id: str) -> object | None:
|
|
81
|
+
return self._overrides.get(provider_id)
|
|
82
82
|
|
|
83
|
-
def reset_override(self,
|
|
84
|
-
if
|
|
83
|
+
def reset_override(self, provider_id: str | None = None) -> None:
|
|
84
|
+
if provider_id is None:
|
|
85
85
|
self._overrides = {}
|
|
86
86
|
else:
|
|
87
|
-
self._overrides.pop(
|
|
87
|
+
self._overrides.pop(provider_id, None)
|
|
88
88
|
|
|
89
89
|
async def __aenter__(self) -> "Container":
|
|
90
90
|
self._is_async = True
|
|
@@ -97,8 +97,8 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
|
97
97
|
traceback: types.TracebackType | None,
|
|
98
98
|
) -> None:
|
|
99
99
|
self._check_entered()
|
|
100
|
-
for
|
|
101
|
-
await
|
|
100
|
+
for provider_state in reversed(self._provider_states.values()):
|
|
101
|
+
await provider_state.async_tear_down()
|
|
102
102
|
self._exit()
|
|
103
103
|
|
|
104
104
|
def __enter__(self) -> "Container":
|
|
@@ -112,6 +112,6 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
|
112
112
|
traceback: types.TracebackType | None,
|
|
113
113
|
) -> None:
|
|
114
114
|
self._check_entered()
|
|
115
|
-
for
|
|
116
|
-
|
|
115
|
+
for provider_state in reversed(self._provider_states.values()):
|
|
116
|
+
provider_state.sync_tear_down()
|
|
117
117
|
self._exit()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import typing
|
|
2
|
+
|
|
3
|
+
from modern_di import Container
|
|
4
|
+
from modern_di.providers import AbstractProvider, BaseCreatorProvider
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
if typing.TYPE_CHECKING:
|
|
8
|
+
import typing_extensions
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
T = typing.TypeVar("T")
|
|
12
|
+
P = typing.ParamSpec("P")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class BaseGraph:
|
|
16
|
+
providers: dict[str, AbstractProvider[typing.Any]]
|
|
17
|
+
|
|
18
|
+
def __new__(cls, *_: typing.Any, **__: typing.Any) -> "typing_extensions.Self": # noqa: ANN401
|
|
19
|
+
msg = f"{cls.__name__} cannot not be instantiated"
|
|
20
|
+
raise RuntimeError(msg)
|
|
21
|
+
|
|
22
|
+
@classmethod
|
|
23
|
+
def get_providers(cls) -> dict[str, AbstractProvider[typing.Any]]:
|
|
24
|
+
if not hasattr(cls, "providers"):
|
|
25
|
+
cls.providers = {k: v for k, v in cls.__dict__.items() if isinstance(v, AbstractProvider)}
|
|
26
|
+
|
|
27
|
+
return cls.providers
|
|
28
|
+
|
|
29
|
+
@classmethod
|
|
30
|
+
async def async_resolve_creators(cls, container: Container) -> None:
|
|
31
|
+
for provider in cls.get_providers().values():
|
|
32
|
+
if isinstance(provider, BaseCreatorProvider) and provider.scope == container.scope:
|
|
33
|
+
await provider.async_resolve(container)
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def sync_resolve_creators(cls, container: Container) -> None:
|
|
37
|
+
for provider in cls.get_providers().values():
|
|
38
|
+
if isinstance(provider, BaseCreatorProvider) and provider.scope == container.scope:
|
|
39
|
+
provider.sync_resolve(container)
|
|
@@ -6,13 +6,13 @@ import typing
|
|
|
6
6
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
class
|
|
10
|
-
__slots__ = "context_stack", "instance", "
|
|
9
|
+
class ProviderState(typing.Generic[T_co]):
|
|
10
|
+
__slots__ = "context_stack", "instance", "provider_lock"
|
|
11
11
|
|
|
12
12
|
def __init__(self, is_lock_required: bool) -> None:
|
|
13
13
|
self.context_stack: contextlib.AsyncExitStack | contextlib.ExitStack | None = None
|
|
14
14
|
self.instance: T_co | None = None
|
|
15
|
-
self.
|
|
15
|
+
self.provider_lock: typing.Final = asyncio.Lock() if is_lock_required else None
|
|
16
16
|
|
|
17
17
|
async def async_tear_down(self) -> None:
|
|
18
18
|
if self.context_stack is None:
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from modern_di.providers.base import AbstractProvider, BaseCreatorProvider
|
|
2
|
+
from modern_di.providers.context_adapter import ContextAdapter
|
|
3
|
+
from modern_di.providers.factory import Factory
|
|
4
|
+
from modern_di.providers.resource import Resource
|
|
5
|
+
from modern_di.providers.singleton import Singleton
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"AbstractProvider",
|
|
10
|
+
"BaseCreatorProvider",
|
|
11
|
+
"ContextAdapter",
|
|
12
|
+
"Factory",
|
|
13
|
+
"Singleton",
|
|
14
|
+
"Resource",
|
|
15
|
+
]
|
|
@@ -12,12 +12,12 @@ R = typing.TypeVar("R")
|
|
|
12
12
|
P = typing.ParamSpec("P")
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
class
|
|
16
|
-
BASE_SLOTS: typing.ClassVar = ["scope", "
|
|
15
|
+
class AbstractProvider(typing.Generic[T_co], abc.ABC):
|
|
16
|
+
BASE_SLOTS: typing.ClassVar = ["scope", "provider_id"]
|
|
17
17
|
|
|
18
18
|
def __init__(self, scope: enum.IntEnum) -> None:
|
|
19
19
|
self.scope = scope
|
|
20
|
-
self.
|
|
20
|
+
self.provider_id: typing.Final = str(uuid.uuid4())
|
|
21
21
|
|
|
22
22
|
@abc.abstractmethod
|
|
23
23
|
async def async_resolve(self, container: Container) -> T_co:
|
|
@@ -28,18 +28,18 @@ class AbstractResolver(typing.Generic[T_co], abc.ABC):
|
|
|
28
28
|
"""Resolve dependency synchronously."""
|
|
29
29
|
|
|
30
30
|
def override(self, override_object: object, container: Container) -> None:
|
|
31
|
-
container.override(self.
|
|
31
|
+
container.override(self.provider_id, override_object)
|
|
32
32
|
|
|
33
33
|
def reset_override(self, container: Container) -> None:
|
|
34
|
-
container.reset_override(self.
|
|
34
|
+
container.reset_override(self.provider_id)
|
|
35
35
|
|
|
36
36
|
@property
|
|
37
37
|
def cast(self) -> T_co:
|
|
38
38
|
return typing.cast(T_co, self)
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
class
|
|
42
|
-
BASE_SLOTS: typing.ClassVar = [*
|
|
41
|
+
class BaseCreatorProvider(AbstractProvider[T_co], abc.ABC):
|
|
42
|
+
BASE_SLOTS: typing.ClassVar = [*AbstractProvider.BASE_SLOTS, "_args", "_kwargs", "_creator"]
|
|
43
43
|
|
|
44
44
|
def __init__(
|
|
45
45
|
self,
|
|
@@ -50,7 +50,7 @@ class BaseCreatorResolver(AbstractResolver[T_co], abc.ABC):
|
|
|
50
50
|
) -> None:
|
|
51
51
|
super().__init__(scope)
|
|
52
52
|
|
|
53
|
-
if any(x.scope > self.scope for x in itertools.chain(args, kwargs.values()) if isinstance(x,
|
|
53
|
+
if any(x.scope > self.scope for x in itertools.chain(args, kwargs.values()) if isinstance(x, AbstractProvider)):
|
|
54
54
|
msg = "Scope of dependency cannot be more than scope of dependent"
|
|
55
55
|
raise RuntimeError(msg)
|
|
56
56
|
|
|
@@ -61,12 +61,12 @@ class BaseCreatorResolver(AbstractResolver[T_co], abc.ABC):
|
|
|
61
61
|
def _sync_build_creator(self, container: Container) -> typing.Any: # noqa: ANN401
|
|
62
62
|
return self._creator(
|
|
63
63
|
*typing.cast(
|
|
64
|
-
P.args, [x.sync_resolve(container) if isinstance(x,
|
|
64
|
+
P.args, [x.sync_resolve(container) if isinstance(x, AbstractProvider) else x for x in self._args]
|
|
65
65
|
),
|
|
66
66
|
**typing.cast(
|
|
67
67
|
P.kwargs,
|
|
68
68
|
{
|
|
69
|
-
k: v.sync_resolve(container) if isinstance(v,
|
|
69
|
+
k: v.sync_resolve(container) if isinstance(v, AbstractProvider) else v
|
|
70
70
|
for k, v in self._kwargs.items()
|
|
71
71
|
},
|
|
72
72
|
),
|
|
@@ -76,12 +76,12 @@ class BaseCreatorResolver(AbstractResolver[T_co], abc.ABC):
|
|
|
76
76
|
return self._creator(
|
|
77
77
|
*typing.cast(
|
|
78
78
|
P.args,
|
|
79
|
-
[await x.async_resolve(container) if isinstance(x,
|
|
79
|
+
[await x.async_resolve(container) if isinstance(x, AbstractProvider) else x for x in self._args],
|
|
80
80
|
),
|
|
81
81
|
**typing.cast(
|
|
82
82
|
P.kwargs,
|
|
83
83
|
{
|
|
84
|
-
k: await v.async_resolve(container) if isinstance(v,
|
|
84
|
+
k: await v.async_resolve(container) if isinstance(v, AbstractProvider) else v
|
|
85
85
|
for k, v in self._kwargs.items()
|
|
86
86
|
},
|
|
87
87
|
),
|
{modern_di-0.2.1/modern_di/resolvers → modern_di-0.3.0/modern_di/providers}/context_adapter.py
RENAMED
|
@@ -2,15 +2,15 @@ import enum
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
from modern_di import Container
|
|
5
|
-
from modern_di.
|
|
5
|
+
from modern_di.providers import AbstractProvider
|
|
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 ContextAdapter(
|
|
13
|
-
__slots__ = [*
|
|
12
|
+
class ContextAdapter(AbstractProvider[T_co]):
|
|
13
|
+
__slots__ = [*AbstractProvider.BASE_SLOTS, "_function"]
|
|
14
14
|
|
|
15
15
|
def __init__(
|
|
16
16
|
self,
|
|
@@ -25,7 +25,7 @@ class ContextAdapter(AbstractResolver[T_co]):
|
|
|
25
25
|
|
|
26
26
|
def sync_resolve(self, container: Container) -> T_co:
|
|
27
27
|
container = container.find_container(self.scope)
|
|
28
|
-
if (override := container.fetch_override(self.
|
|
28
|
+
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
29
29
|
return typing.cast(T_co, override)
|
|
30
30
|
|
|
31
31
|
return self._function(**container.context)
|
|
@@ -2,15 +2,15 @@ import enum
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
from modern_di import Container
|
|
5
|
-
from modern_di.
|
|
5
|
+
from modern_di.providers import BaseCreatorProvider
|
|
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(BaseCreatorProvider[T_co]):
|
|
13
|
+
__slots__ = [*BaseCreatorProvider.BASE_SLOTS, "_creator"]
|
|
14
14
|
|
|
15
15
|
def __init__(
|
|
16
16
|
self,
|
|
@@ -23,14 +23,14 @@ class Factory(BaseCreatorResolver[T_co]):
|
|
|
23
23
|
|
|
24
24
|
async def async_resolve(self, container: Container) -> T_co:
|
|
25
25
|
container = container.find_container(self.scope)
|
|
26
|
-
if (override := container.fetch_override(self.
|
|
26
|
+
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
27
27
|
return typing.cast(T_co, override)
|
|
28
28
|
|
|
29
29
|
return typing.cast(T_co, await self._async_build_creator(container))
|
|
30
30
|
|
|
31
31
|
def sync_resolve(self, container: Container) -> T_co:
|
|
32
32
|
container = container.find_container(self.scope)
|
|
33
|
-
if (override := container.fetch_override(self.
|
|
33
|
+
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
34
34
|
return typing.cast(T_co, override)
|
|
35
35
|
|
|
36
36
|
return typing.cast(T_co, self._sync_build_creator(container))
|
|
@@ -4,15 +4,15 @@ import inspect
|
|
|
4
4
|
import typing
|
|
5
5
|
|
|
6
6
|
from modern_di import Container
|
|
7
|
-
from modern_di.
|
|
7
|
+
from modern_di.providers import BaseCreatorProvider
|
|
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(BaseCreatorProvider[T_co]):
|
|
15
|
+
__slots__ = [*BaseCreatorProvider.BASE_SLOTS, "_creator", "_args", "_kwargs", "_is_async"]
|
|
16
16
|
|
|
17
17
|
def _is_creator_async(
|
|
18
18
|
self,
|
|
@@ -42,43 +42,43 @@ class Resource(BaseCreatorResolver[T_co]):
|
|
|
42
42
|
|
|
43
43
|
async def async_resolve(self, container: Container) -> T_co:
|
|
44
44
|
container = container.find_container(self.scope)
|
|
45
|
-
if (override := container.fetch_override(self.
|
|
45
|
+
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
46
46
|
return typing.cast(T_co, override)
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
self.
|
|
48
|
+
provider_state = container.fetch_provider_state(
|
|
49
|
+
self.provider_id, is_async_resource=self._is_async, is_lock_required=True
|
|
50
50
|
)
|
|
51
|
-
if
|
|
52
|
-
return typing.cast(T_co,
|
|
51
|
+
if provider_state.instance is not None:
|
|
52
|
+
return typing.cast(T_co, provider_state.instance)
|
|
53
53
|
|
|
54
|
-
assert
|
|
55
|
-
await
|
|
54
|
+
assert provider_state.provider_lock
|
|
55
|
+
await provider_state.provider_lock.acquire()
|
|
56
56
|
|
|
57
57
|
try:
|
|
58
|
-
if
|
|
59
|
-
return typing.cast(T_co,
|
|
58
|
+
if provider_state.instance is not None:
|
|
59
|
+
return typing.cast(T_co, provider_state.instance)
|
|
60
60
|
|
|
61
61
|
_intermediate_ = await self._async_build_creator(container)
|
|
62
62
|
|
|
63
63
|
if self._is_creator_async(self._creator): # type: ignore[arg-type]
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
provider_state.context_stack = contextlib.AsyncExitStack()
|
|
65
|
+
provider_state.instance = await provider_state.context_stack.enter_async_context(_intermediate_)
|
|
66
66
|
else:
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
provider_state.context_stack = contextlib.ExitStack()
|
|
68
|
+
provider_state.instance = provider_state.context_stack.enter_context(_intermediate_)
|
|
69
69
|
finally:
|
|
70
|
-
|
|
70
|
+
provider_state.provider_lock.release()
|
|
71
71
|
|
|
72
|
-
return typing.cast(T_co,
|
|
72
|
+
return typing.cast(T_co, provider_state.instance)
|
|
73
73
|
|
|
74
74
|
def sync_resolve(self, container: Container) -> T_co:
|
|
75
75
|
container = container.find_container(self.scope)
|
|
76
|
-
if (override := container.fetch_override(self.
|
|
76
|
+
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
77
77
|
return typing.cast(T_co, override)
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
if
|
|
81
|
-
return typing.cast(T_co,
|
|
79
|
+
provider_state = container.fetch_provider_state(self.provider_id)
|
|
80
|
+
if provider_state.instance is not None:
|
|
81
|
+
return typing.cast(T_co, provider_state.instance)
|
|
82
82
|
|
|
83
83
|
if self._is_async:
|
|
84
84
|
msg = "Async resource cannot be resolved synchronously"
|
|
@@ -86,9 +86,9 @@ class Resource(BaseCreatorResolver[T_co]):
|
|
|
86
86
|
|
|
87
87
|
_intermediate_ = self._sync_build_creator(container)
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
provider_state.context_stack = contextlib.ExitStack()
|
|
90
|
+
provider_state.instance = provider_state.context_stack.enter_context(
|
|
91
91
|
typing.cast(contextlib.AbstractContextManager[typing.Any], _intermediate_)
|
|
92
92
|
)
|
|
93
93
|
|
|
94
|
-
return typing.cast(T_co,
|
|
94
|
+
return typing.cast(T_co, provider_state.instance)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from modern_di import Container
|
|
5
|
+
from modern_di.providers import BaseCreatorProvider
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
P = typing.ParamSpec("P")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Singleton(BaseCreatorProvider[T_co]):
|
|
13
|
+
__slots__ = [*BaseCreatorProvider.BASE_SLOTS, "_creator"]
|
|
14
|
+
|
|
15
|
+
def __init__(
|
|
16
|
+
self,
|
|
17
|
+
scope: enum.IntEnum,
|
|
18
|
+
creator: typing.Callable[P, T_co],
|
|
19
|
+
*args: P.args,
|
|
20
|
+
**kwargs: P.kwargs,
|
|
21
|
+
) -> None:
|
|
22
|
+
super().__init__(scope, creator, *args, **kwargs)
|
|
23
|
+
|
|
24
|
+
async def async_resolve(self, container: Container) -> T_co:
|
|
25
|
+
container = container.find_container(self.scope)
|
|
26
|
+
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
27
|
+
return typing.cast(T_co, override)
|
|
28
|
+
|
|
29
|
+
provider_state = container.fetch_provider_state(self.provider_id, is_lock_required=True)
|
|
30
|
+
if provider_state.instance is not None:
|
|
31
|
+
return typing.cast(T_co, provider_state.instance)
|
|
32
|
+
|
|
33
|
+
assert provider_state.provider_lock
|
|
34
|
+
await provider_state.provider_lock.acquire()
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
if provider_state.instance is not None:
|
|
38
|
+
return typing.cast(T_co, provider_state.instance)
|
|
39
|
+
|
|
40
|
+
provider_state.instance = typing.cast(T_co, await self._async_build_creator(container))
|
|
41
|
+
finally:
|
|
42
|
+
provider_state.provider_lock.release()
|
|
43
|
+
|
|
44
|
+
return provider_state.instance
|
|
45
|
+
|
|
46
|
+
def sync_resolve(self, container: Container) -> T_co:
|
|
47
|
+
container = container.find_container(self.scope)
|
|
48
|
+
if (override := container.fetch_override(self.provider_id)) is not None:
|
|
49
|
+
return typing.cast(T_co, override)
|
|
50
|
+
|
|
51
|
+
provider_state = container.fetch_provider_state(self.provider_id)
|
|
52
|
+
if provider_state.instance is not None:
|
|
53
|
+
return typing.cast(T_co, provider_state.instance)
|
|
54
|
+
|
|
55
|
+
provider_state.instance = self._sync_build_creator(container)
|
|
56
|
+
return typing.cast(T_co, provider_state.instance)
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import datetime
|
|
2
2
|
|
|
3
|
-
from modern_di import Container, Scope,
|
|
3
|
+
from modern_di import Container, Scope, providers
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
def context_adapter_function(*, now: datetime.datetime, **_: object) -> datetime.datetime:
|
|
7
7
|
return now
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
context_adapter =
|
|
11
|
-
request_context_adapter =
|
|
10
|
+
context_adapter = providers.ContextAdapter(Scope.APP, context_adapter_function)
|
|
11
|
+
request_context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_function)
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
async def test_context_adapter() -> None:
|
|
@@ -2,7 +2,7 @@ import dataclasses
|
|
|
2
2
|
|
|
3
3
|
import pytest
|
|
4
4
|
|
|
5
|
-
from modern_di import Container, Scope,
|
|
5
|
+
from modern_di import Container, Scope, providers
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
@dataclasses.dataclass(kw_only=True, slots=True)
|
|
@@ -15,8 +15,8 @@ class DependentCreator:
|
|
|
15
15
|
dep1: SimpleCreator
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
app_factory =
|
|
19
|
-
request_factory =
|
|
18
|
+
app_factory = providers.Factory(Scope.APP, SimpleCreator, dep1="original")
|
|
19
|
+
request_factory = providers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
async def test_app_factory() -> None:
|
|
@@ -82,6 +82,6 @@ async def test_factory_overridden() -> None:
|
|
|
82
82
|
async def test_factory_wrong_dependency_scope() -> None:
|
|
83
83
|
def some_factory(_: SimpleCreator) -> None: ...
|
|
84
84
|
|
|
85
|
-
request_factory_ =
|
|
85
|
+
request_factory_ = providers.Factory(Scope.REQUEST, SimpleCreator, dep1="original")
|
|
86
86
|
with pytest.raises(RuntimeError, match="Scope of dependency cannot be more than scope of dependent"):
|
|
87
|
-
|
|
87
|
+
providers.Singleton(Scope.APP, some_factory, request_factory_.cast)
|
|
@@ -3,12 +3,12 @@ import typing
|
|
|
3
3
|
|
|
4
4
|
import pytest
|
|
5
5
|
|
|
6
|
-
from modern_di import Container, Scope,
|
|
6
|
+
from modern_di import Container, Scope, providers
|
|
7
7
|
from tests.creators import create_async_resource, create_sync_resource
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
async_resource =
|
|
11
|
-
sync_resource =
|
|
10
|
+
async_resource = providers.Resource(Scope.APP, create_async_resource)
|
|
11
|
+
sync_resource = providers.Resource(Scope.APP, create_sync_resource)
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
async def test_async_resource() -> None:
|
|
@@ -97,7 +97,7 @@ async def test_async_resource_race_condition() -> None:
|
|
|
97
97
|
await asyncio.sleep(0)
|
|
98
98
|
yield ""
|
|
99
99
|
|
|
100
|
-
resource =
|
|
100
|
+
resource = providers.Resource(Scope.APP, create_resource)
|
|
101
101
|
|
|
102
102
|
async def resolve_resource(container: Container) -> str:
|
|
103
103
|
return await resource.async_resolve(container)
|
|
@@ -110,7 +110,7 @@ async def test_async_resource_race_condition() -> None:
|
|
|
110
110
|
|
|
111
111
|
async def test_resource_unsupported_creator() -> None:
|
|
112
112
|
with pytest.raises(RuntimeError, match="Unsupported resource type"):
|
|
113
|
-
|
|
113
|
+
providers.Resource(Scope.APP, None) # type: ignore[arg-type]
|
|
114
114
|
|
|
115
115
|
|
|
116
116
|
async def test_async_resource_sync_resolve() -> None:
|
|
@@ -4,7 +4,7 @@ import typing
|
|
|
4
4
|
|
|
5
5
|
import pytest
|
|
6
6
|
|
|
7
|
-
from modern_di import Container, Scope,
|
|
7
|
+
from modern_di import Container, Scope, providers
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
@dataclasses.dataclass(kw_only=True, slots=True)
|
|
@@ -17,8 +17,8 @@ class DependentCreator:
|
|
|
17
17
|
dep1: SimpleCreator
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
app_singleton =
|
|
21
|
-
request_singleton =
|
|
20
|
+
app_singleton = providers.Singleton(Scope.APP, SimpleCreator, dep1="original")
|
|
21
|
+
request_singleton = providers.Singleton(Scope.REQUEST, DependentCreator, dep1=app_singleton.cast)
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
async def test_app_singleton() -> None:
|
|
@@ -94,8 +94,8 @@ async def test_singleton_race_condition() -> None:
|
|
|
94
94
|
await asyncio.sleep(0)
|
|
95
95
|
yield ""
|
|
96
96
|
|
|
97
|
-
resource =
|
|
98
|
-
factory_with_resource =
|
|
97
|
+
resource = providers.Resource(Scope.APP, create_resource)
|
|
98
|
+
factory_with_resource = providers.Singleton(Scope.APP, SimpleCreator, dep1=resource.cast)
|
|
99
99
|
|
|
100
100
|
async def resolve_factory(container: Container) -> SimpleCreator:
|
|
101
101
|
return await factory_with_resource.async_resolve(container)
|
|
@@ -110,6 +110,6 @@ async def test_singleton_race_condition() -> None:
|
|
|
110
110
|
async def test_singleton_wrong_dependency_scope() -> None:
|
|
111
111
|
def some_factory(_: SimpleCreator) -> None: ...
|
|
112
112
|
|
|
113
|
-
request_singleton_ =
|
|
113
|
+
request_singleton_ = providers.Singleton(Scope.REQUEST, SimpleCreator, dep1="original")
|
|
114
114
|
with pytest.raises(RuntimeError, match="Scope of dependency cannot be more than scope of dependent"):
|
|
115
|
-
|
|
115
|
+
providers.Singleton(Scope.APP, some_factory, request_singleton_.cast)
|
|
@@ -13,7 +13,7 @@ def test_container_wrong_init() -> None:
|
|
|
13
13
|
def test_container_not_opened() -> None:
|
|
14
14
|
container = Container(scope=Scope.APP)
|
|
15
15
|
with pytest.raises(RuntimeError, match="Enter the context first"):
|
|
16
|
-
container.
|
|
16
|
+
container.fetch_provider_state("some_id")
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
async def test_container_build_child_async() -> None:
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import pytest
|
|
2
2
|
|
|
3
|
-
from modern_di import BaseGraph, Container, Scope,
|
|
3
|
+
from modern_di import BaseGraph, Container, Scope, providers
|
|
4
4
|
from tests.creators import create_async_resource, create_sync_resource
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
async_resource =
|
|
8
|
-
sync_resource =
|
|
7
|
+
async_resource = providers.Resource(Scope.APP, create_async_resource)
|
|
8
|
+
sync_resource = providers.Resource(Scope.APP, create_sync_resource)
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
async def test_graph_async_resolve_creators() -> None:
|
|
@@ -16,8 +16,8 @@ async def test_graph_async_resolve_creators() -> None:
|
|
|
16
16
|
async with Container(scope=Scope.APP) as app_container:
|
|
17
17
|
await DIGraph.async_resolve_creators(app_container)
|
|
18
18
|
|
|
19
|
-
assert len(app_container.
|
|
20
|
-
assert len(app_container.
|
|
19
|
+
assert len(app_container._provider_states) == 2 # noqa: SLF001, PLR2004
|
|
20
|
+
assert len(app_container._provider_states) == 0 # noqa: SLF001
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
def test_graph_sync_resolve_creators() -> None:
|
|
@@ -27,8 +27,8 @@ def test_graph_sync_resolve_creators() -> None:
|
|
|
27
27
|
with Container(scope=Scope.APP) as app_container:
|
|
28
28
|
DIGraph.sync_resolve_creators(app_container)
|
|
29
29
|
|
|
30
|
-
assert len(app_container.
|
|
31
|
-
assert len(app_container.
|
|
30
|
+
assert len(app_container._provider_states) == 1 # noqa: SLF001
|
|
31
|
+
assert len(app_container._provider_states) == 0 # noqa: SLF001
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
def test_graph_cannot_be_instantiated() -> None:
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import typing
|
|
2
|
-
|
|
3
|
-
from modern_di import Container
|
|
4
|
-
from modern_di.resolvers import AbstractResolver, BaseCreatorResolver
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if typing.TYPE_CHECKING:
|
|
8
|
-
import typing_extensions
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
T = typing.TypeVar("T")
|
|
12
|
-
P = typing.ParamSpec("P")
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class BaseGraph:
|
|
16
|
-
resolvers: dict[str, AbstractResolver[typing.Any]]
|
|
17
|
-
|
|
18
|
-
def __new__(cls, *_: typing.Any, **__: typing.Any) -> "typing_extensions.Self": # noqa: ANN401
|
|
19
|
-
msg = f"{cls.__name__} cannot not be instantiated"
|
|
20
|
-
raise RuntimeError(msg)
|
|
21
|
-
|
|
22
|
-
@classmethod
|
|
23
|
-
def get_resolvers(cls) -> dict[str, AbstractResolver[typing.Any]]:
|
|
24
|
-
if not hasattr(cls, "resolvers"):
|
|
25
|
-
cls.resolvers = {k: v for k, v in cls.__dict__.items() if isinstance(v, AbstractResolver)}
|
|
26
|
-
|
|
27
|
-
return cls.resolvers
|
|
28
|
-
|
|
29
|
-
@classmethod
|
|
30
|
-
async def async_resolve_creators(cls, container: Container) -> None:
|
|
31
|
-
for resolver in cls.get_resolvers().values():
|
|
32
|
-
if isinstance(resolver, BaseCreatorResolver) and resolver.scope == container.scope:
|
|
33
|
-
await resolver.async_resolve(container)
|
|
34
|
-
|
|
35
|
-
@classmethod
|
|
36
|
-
def sync_resolve_creators(cls, container: Container) -> None:
|
|
37
|
-
for resolver in cls.get_resolvers().values():
|
|
38
|
-
if isinstance(resolver, BaseCreatorResolver) and resolver.scope == container.scope:
|
|
39
|
-
resolver.sync_resolve(container)
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
from modern_di.resolvers.base import AbstractResolver, BaseCreatorResolver
|
|
2
|
-
from modern_di.resolvers.context_adapter import ContextAdapter
|
|
3
|
-
from modern_di.resolvers.factory import Factory
|
|
4
|
-
from modern_di.resolvers.resource import Resource
|
|
5
|
-
from modern_di.resolvers.singleton import Singleton
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
__all__ = [
|
|
9
|
-
"AbstractResolver",
|
|
10
|
-
"BaseCreatorResolver",
|
|
11
|
-
"ContextAdapter",
|
|
12
|
-
"Factory",
|
|
13
|
-
"Singleton",
|
|
14
|
-
"Resource",
|
|
15
|
-
]
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import enum
|
|
2
|
-
import typing
|
|
3
|
-
|
|
4
|
-
from modern_di import Container
|
|
5
|
-
from modern_di.resolvers import BaseCreatorResolver
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
-
P = typing.ParamSpec("P")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class Singleton(BaseCreatorResolver[T_co]):
|
|
13
|
-
__slots__ = [*BaseCreatorResolver.BASE_SLOTS, "_creator"]
|
|
14
|
-
|
|
15
|
-
def __init__(
|
|
16
|
-
self,
|
|
17
|
-
scope: enum.IntEnum,
|
|
18
|
-
creator: typing.Callable[P, T_co],
|
|
19
|
-
*args: P.args,
|
|
20
|
-
**kwargs: P.kwargs,
|
|
21
|
-
) -> None:
|
|
22
|
-
super().__init__(scope, creator, *args, **kwargs)
|
|
23
|
-
|
|
24
|
-
async def async_resolve(self, container: Container) -> T_co:
|
|
25
|
-
container = container.find_container(self.scope)
|
|
26
|
-
if (override := container.fetch_override(self.resolver_id)) is not None:
|
|
27
|
-
return typing.cast(T_co, override)
|
|
28
|
-
|
|
29
|
-
resolver_state = container.fetch_resolver_state(self.resolver_id, is_lock_required=True)
|
|
30
|
-
if resolver_state.instance is not None:
|
|
31
|
-
return typing.cast(T_co, resolver_state.instance)
|
|
32
|
-
|
|
33
|
-
assert resolver_state.resolver_lock
|
|
34
|
-
await resolver_state.resolver_lock.acquire()
|
|
35
|
-
|
|
36
|
-
try:
|
|
37
|
-
if resolver_state.instance is not None:
|
|
38
|
-
return typing.cast(T_co, resolver_state.instance)
|
|
39
|
-
|
|
40
|
-
resolver_state.instance = typing.cast(T_co, await self._async_build_creator(container))
|
|
41
|
-
finally:
|
|
42
|
-
resolver_state.resolver_lock.release()
|
|
43
|
-
|
|
44
|
-
return resolver_state.instance
|
|
45
|
-
|
|
46
|
-
def sync_resolve(self, container: Container) -> T_co:
|
|
47
|
-
container = container.find_container(self.scope)
|
|
48
|
-
if (override := container.fetch_override(self.resolver_id)) is not None:
|
|
49
|
-
return typing.cast(T_co, override)
|
|
50
|
-
|
|
51
|
-
resolver_state = container.fetch_resolver_state(self.resolver_id)
|
|
52
|
-
if resolver_state.instance is not None:
|
|
53
|
-
return typing.cast(T_co, resolver_state.instance)
|
|
54
|
-
|
|
55
|
-
resolver_state.instance = self._sync_build_creator(container)
|
|
56
|
-
return typing.cast(T_co, resolver_state.instance)
|
|
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
|