modern-di 0.2.0__py3-none-any.whl → 0.2.1__py3-none-any.whl

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/container.py CHANGED
@@ -14,15 +14,22 @@ T_co = typing.TypeVar("T_co", covariant=True)
14
14
 
15
15
 
16
16
  class Container(contextlib.AbstractAsyncContextManager["Container"]):
17
- __slots__ = "scope", "parent_container", "_is_async", "_resolver_states", "_overrides"
17
+ __slots__ = "scope", "parent_container", "context", "_is_async", "_resolver_states", "_overrides"
18
18
 
19
- def __init__(self, *, scope: enum.IntEnum, parent_container: typing.Optional["Container"] = None) -> None:
19
+ def __init__(
20
+ self,
21
+ *,
22
+ scope: enum.IntEnum,
23
+ parent_container: typing.Optional["Container"] = None,
24
+ context: dict[str, typing.Any] | None = None,
25
+ ) -> None:
20
26
  if scope.value != 1 and parent_container is None:
21
27
  msg = "Only first scope can be used without parent_container"
22
28
  raise RuntimeError(msg)
23
29
 
24
30
  self.scope = scope
25
31
  self.parent_container = parent_container
32
+ self.context: dict[str, typing.Any] = context or {}
26
33
  self._is_async: bool | None = None
27
34
  self._resolver_states: dict[str, ResolverState[typing.Any]] = {}
28
35
  self._overrides: dict[str, typing.Any] = {}
@@ -30,13 +37,14 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
30
37
  def _exit(self) -> None:
31
38
  self._is_async = None
32
39
  self._resolver_states = {}
40
+ self._overrides = {}
33
41
 
34
42
  def _check_entered(self) -> None:
35
43
  if self._is_async is None:
36
44
  msg = "Enter the context first"
37
45
  raise RuntimeError(msg)
38
46
 
39
- def build_child_container(self) -> "typing_extensions.Self":
47
+ def build_child_container(self, context: dict[str, typing.Any] | None = None) -> "typing_extensions.Self":
40
48
  self._check_entered()
41
49
 
42
50
  try:
@@ -45,7 +53,7 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
45
53
  msg = f"Max scope is reached, {self.scope.name}"
46
54
  raise RuntimeError(msg) from exc
47
55
 
48
- return self.__class__(scope=new_scope, parent_container=self)
56
+ return self.__class__(scope=new_scope, parent_container=self, context=context)
49
57
 
50
58
  def find_container(self, scope: enum.IntEnum) -> "typing_extensions.Self":
51
59
  container = self
@@ -1,4 +1,5 @@
1
1
  from modern_di.resolvers.base import AbstractResolver, BaseCreatorResolver
2
+ from modern_di.resolvers.context_adapter import ContextAdapter
2
3
  from modern_di.resolvers.factory import Factory
3
4
  from modern_di.resolvers.resource import Resource
4
5
  from modern_di.resolvers.singleton import Singleton
@@ -7,6 +8,7 @@ from modern_di.resolvers.singleton import Singleton
7
8
  __all__ = [
8
9
  "AbstractResolver",
9
10
  "BaseCreatorResolver",
11
+ "ContextAdapter",
10
12
  "Factory",
11
13
  "Singleton",
12
14
  "Resource",
@@ -0,0 +1,31 @@
1
+ import enum
2
+ import typing
3
+
4
+ from modern_di import Container
5
+ from modern_di.resolvers import AbstractResolver
6
+
7
+
8
+ T_co = typing.TypeVar("T_co", covariant=True)
9
+ P = typing.ParamSpec("P")
10
+
11
+
12
+ class ContextAdapter(AbstractResolver[T_co]):
13
+ __slots__ = [*AbstractResolver.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.resolver_id)) is not None:
29
+ return typing.cast(T_co, override)
30
+
31
+ return self._function(**container.context)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: modern-di
3
- Version: 0.2.0
3
+ Version: 0.2.1
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
@@ -19,7 +19,6 @@ Description-Content-Type: text/markdown
19
19
 
20
20
  "Modern-DI"
21
21
  ==
22
- [![Test Coverage](https://codecov.io/gh/modern-python/modern-di/branch/main/graph/badge.svg)](https://codecov.io/gh/modern-python/modern-di)
23
22
  [![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
24
23
  [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di.svg)](https://pypi.python.org/pypi/modern-di)
25
24
  [![downloads](https://img.shields.io/pypi/dm/modern-di.svg)](https://pypistats.org/packages/modern-di)
@@ -27,7 +26,7 @@ Description-Content-Type: text/markdown
27
26
 
28
27
  Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
29
28
 
30
- It is production-ready and gives you the following:
29
+ It is in early development state and gives you the following:
31
30
  - DI framework with IOC-container and scopes.
32
31
  - Async and sync resolving.
33
32
  - Python 3.10-3.13 support.
@@ -1,15 +1,16 @@
1
1
  modern_di/__init__.py,sha256=2xhgfqi4gpzPrlfam5HSwTFRroWmYIybDgsSYD9pPNg,194
2
- modern_di/container.py,sha256=o-6_eoSGpOrIZlAXDryISWmgnSLq7u0YAVRXHvkWU0s,3742
2
+ modern_di/container.py,sha256=1fHHEKuEI_0Eml6FLj65aXK_5BiofVKRbQQFuvW_MO0,3998
3
3
  modern_di/graph.py,sha256=V6LUbtcZb64jBD-GJB2bJijEcPBHSYEEI9WZGflrFpQ,1335
4
4
  modern_di/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  modern_di/resolver_state.py,sha256=rRIoyWPK7XtMeWPBFuEe8t81WdrHbj5-jtpOqZWf0jY,1206
6
6
  modern_di/scope.py,sha256=nnrTLnFT_dMDLVIwvJHcGlJsB2k1WIHusrRSGQIMEsg,97
7
- modern_di/resolvers/__init__.py,sha256=560FLaWh7hjqp5ghOHsEDWl8wmoPGXZ-UUO9GgYusgc,340
7
+ modern_di/resolvers/__init__.py,sha256=xX3DCm7q_9EuDiiKurXkfWXQiZN7zwGhEHlqvcneWxg,425
8
8
  modern_di/resolvers/base.py,sha256=BMZbYfkIzAW8-8CQ9aRVIu5D8zw9owKv4nz-z0EMYmg,2897
9
+ modern_di/resolvers/context_adapter.py,sha256=LbqtphHOxSNXY9Zr2bKZEr-ghkUqByFgyXNL6c6d7N0,885
9
10
  modern_di/resolvers/factory.py,sha256=tMOxpXp-zaXiSsDGUp68L2mtcfF7K02ZdF-LglH4phs,1164
10
11
  modern_di/resolvers/resource.py,sha256=8qtyIxspbtW7Q3Yo1Y6risNamG0H3XZNU0G9t-lfZjk,3628
11
12
  modern_di/resolvers/singleton.py,sha256=k70nEaeBqsu-uHQFqsn8XZh-aw2tQR-J5ImLED6GQGg,1978
12
- modern_di-0.2.0.dist-info/METADATA,sha256=uLMl5mJ78gixE_23iXUhMXKo2hZQao-Oc3zPkv2AtOY,1846
13
- modern_di-0.2.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
14
- modern_di-0.2.0.dist-info/licenses/LICENSE,sha256=aA5_eJwDKqnGvL7PbkPb9x5f-VGIqZ9cvWWkeGqeD90,1070
15
- modern_di-0.2.0.dist-info/RECORD,,
13
+ modern_di-0.2.1.dist-info/METADATA,sha256=hOFJcpStbZwvIJpiah4e0SrprrIjSwMkF7hLcVkYMmU,1715
14
+ modern_di-0.2.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
15
+ modern_di-0.2.1.dist-info/licenses/LICENSE,sha256=aA5_eJwDKqnGvL7PbkPb9x5f-VGIqZ9cvWWkeGqeD90,1070
16
+ modern_di-0.2.1.dist-info/RECORD,,