modern-di 0.3.0__tar.gz → 0.4.1__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.
Files changed (53) hide show
  1. modern_di-0.4.1/PKG-INFO +39 -0
  2. modern_di-0.4.1/README.md +20 -0
  3. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/pyproject.toml +0 -3
  4. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/graph.py +3 -3
  5. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/providers/__init__.py +7 -2
  6. modern_di-0.3.0/modern_di/providers/base.py → modern_di-0.4.1/modern_di/providers/abstract.py +13 -10
  7. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/providers/context_adapter.py +2 -6
  8. modern_di-0.4.1/modern_di/providers/dict.py +23 -0
  9. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/providers/factory.py +3 -3
  10. modern_di-0.4.1/modern_di/providers/list.py +23 -0
  11. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/providers/resource.py +3 -3
  12. modern_di-0.4.1/modern_di/providers/selector.py +39 -0
  13. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/providers/singleton.py +3 -3
  14. {modern_di-0.3.0 → modern_di-0.4.1}/pyproject.toml +6 -0
  15. modern_di-0.4.1/tests/providers/__init__.py +0 -0
  16. {modern_di-0.3.0/tests/resolvers → modern_di-0.4.1/tests/providers}/test_context_adapter.py +0 -10
  17. modern_di-0.4.1/tests/providers/test_dict.py +24 -0
  18. modern_di-0.4.1/tests/providers/test_list.py +24 -0
  19. modern_di-0.4.1/tests/providers/test_selector.py +45 -0
  20. modern_di-0.3.0/PKG-INFO +0 -37
  21. modern_di-0.3.0/README.md +0 -18
  22. {modern_di-0.3.0 → modern_di-0.4.1}/.github/workflows/core-ci.yml +0 -0
  23. {modern_di-0.3.0 → modern_di-0.4.1}/.github/workflows/fastapi-ci.yml +0 -0
  24. {modern_di-0.3.0 → modern_di-0.4.1}/.github/workflows/publish.yml +0 -0
  25. {modern_di-0.3.0 → modern_di-0.4.1}/.gitignore +0 -0
  26. {modern_di-0.3.0 → modern_di-0.4.1}/.readthedocs.yaml +0 -0
  27. {modern_di-0.3.0 → modern_di-0.4.1}/Justfile +0 -0
  28. {modern_di-0.3.0 → modern_di-0.4.1}/LICENSE +0 -0
  29. {modern_di-0.3.0 → modern_di-0.4.1}/docs/conf.py +0 -0
  30. {modern_di-0.3.0 → modern_di-0.4.1}/docs/dev/contributing.md +0 -0
  31. {modern_di-0.3.0 → modern_di-0.4.1}/docs/dev/development-notes.md +0 -0
  32. {modern_di-0.3.0 → modern_di-0.4.1}/docs/index.md +0 -0
  33. {modern_di-0.3.0 → modern_di-0.4.1}/docs/requirements.txt +0 -0
  34. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/Justfile +0 -0
  35. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/README.md +0 -0
  36. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/modern_di_fastapi/__init__.py +0 -0
  37. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/modern_di_fastapi/depends.py +0 -0
  38. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/modern_di_fastapi/middleware.py +0 -0
  39. {modern_di-0.3.0/modern_di → modern_di-0.4.1/integrations/fastapi/modern_di_fastapi}/py.typed +0 -0
  40. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/tests/__init__.py +0 -0
  41. {modern_di-0.3.0 → modern_di-0.4.1}/integrations/fastapi/tests/test_fastapi_di.py +0 -0
  42. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/__init__.py +0 -0
  43. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/container.py +0 -0
  44. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/provider_state.py +0 -0
  45. /modern_di-0.3.0/tests/__init__.py → /modern_di-0.4.1/modern_di/py.typed +0 -0
  46. {modern_di-0.3.0 → modern_di-0.4.1}/modern_di/scope.py +0 -0
  47. {modern_di-0.3.0/tests/resolvers → modern_di-0.4.1/tests}/__init__.py +0 -0
  48. {modern_di-0.3.0 → modern_di-0.4.1}/tests/creators.py +0 -0
  49. {modern_di-0.3.0/tests/resolvers → modern_di-0.4.1/tests/providers}/test_factory.py +0 -0
  50. {modern_di-0.3.0/tests/resolvers → modern_di-0.4.1/tests/providers}/test_resource.py +0 -0
  51. {modern_di-0.3.0/tests/resolvers → modern_di-0.4.1/tests/providers}/test_singleton.py +0 -0
  52. {modern_di-0.3.0 → modern_di-0.4.1}/tests/test_container.py +0 -0
  53. {modern_di-0.3.0 → modern_di-0.4.1}/tests/test_graph.py +0 -0
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.3
2
+ Name: modern-di
3
+ Version: 0.4.1
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 | [![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) |
26
+ | 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) |
27
+ | 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) |
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 | [![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) |
7
+ | 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) |
8
+ | 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) |
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)
@@ -41,9 +41,6 @@ dev = [
41
41
  "asgi-lifespan",
42
42
  ]
43
43
 
44
- [tool.uv.sources]
45
- modern-di = { path = "../../" }
46
-
47
44
  [build-system]
48
45
  requires = ["hatchling", "hatch-vcs"]
49
46
  build-backend = "hatchling.build"
@@ -1,7 +1,7 @@
1
1
  import typing
2
2
 
3
3
  from modern_di import Container
4
- from modern_di.providers import AbstractProvider, BaseCreatorProvider
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, BaseCreatorProvider) and provider.scope == container.scope:
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, BaseCreatorProvider) and provider.scope == container.scope:
38
+ if isinstance(provider, AbstractCreatorProvider) and provider.scope == container.scope:
39
39
  provider.sync_resolve(container)
@@ -1,15 +1,20 @@
1
- from modern_di.providers.base import AbstractProvider, BaseCreatorProvider
1
+ from modern_di.providers.abstract import AbstractProvider
2
2
  from modern_di.providers.context_adapter import ContextAdapter
3
+ from modern_di.providers.dict import Dict
3
4
  from modern_di.providers.factory import Factory
5
+ from modern_di.providers.list import List
4
6
  from modern_di.providers.resource import Resource
7
+ from modern_di.providers.selector import Selector
5
8
  from modern_di.providers.singleton import Singleton
6
9
 
7
10
 
8
11
  __all__ = [
9
12
  "AbstractProvider",
10
- "BaseCreatorProvider",
11
13
  "ContextAdapter",
12
14
  "Factory",
15
+ "Dict",
16
+ "List",
17
+ "Selector",
13
18
  "Singleton",
14
19
  "Resource",
15
20
  ]
@@ -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 BaseCreatorProvider(AbstractProvider[T_co], abc.ABC):
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
@@ -21,11 +21,7 @@ class ContextAdapter(AbstractProvider[T_co]):
21
21
  self._function = function
22
22
 
23
23
  async def async_resolve(self, container: Container) -> T_co:
24
- return self.sync_resolve(container)
24
+ return self._function(**container.find_container(self.scope).context)
25
25
 
26
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)
27
+ return self._function(**container.find_container(self.scope).context)
@@ -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 BaseCreatorProvider
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(BaseCreatorProvider[T_co]):
13
- __slots__ = [*BaseCreatorProvider.BASE_SLOTS, "_creator"]
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 BaseCreatorProvider
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(BaseCreatorProvider[T_co]):
15
- __slots__ = [*BaseCreatorProvider.BASE_SLOTS, "_creator", "_args", "_kwargs", "_is_async"]
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 BaseCreatorProvider
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(BaseCreatorProvider[T_co]):
13
- __slots__ = [*BaseCreatorProvider.BASE_SLOTS, "_creator"]
12
+ class Singleton(AbstractCreatorProvider[T_co]):
13
+ __slots__ = [*AbstractCreatorProvider.BASE_SLOTS, "_creator"]
14
14
 
15
15
  def __init__(
16
16
  self,
@@ -35,6 +35,12 @@ dev = [
35
35
  "typing-extensions",
36
36
  ]
37
37
 
38
+ [tool.uv.workspace]
39
+ members = ["integrations/*"]
40
+
41
+ [tool.uv.sources]
42
+ modern-di = { workspace = true }
43
+
38
44
  [build-system]
39
45
  requires = ["hatchling", "hatch-vcs"]
40
46
  build-backend = "hatchling.build"
File without changes
@@ -28,13 +28,3 @@ async def test_context_adapter_in_request_scope() -> None:
28
28
  instance1 = await request_context_adapter.async_resolve(request_container)
29
29
  instance2 = request_context_adapter.sync_resolve(request_container)
30
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
@@ -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
- [![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
23
- [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di.svg)](https://pypi.python.org/pypi/modern-di)
24
- [![downloads](https://img.shields.io/pypi/dm/modern-di.svg)](https://pypistats.org/packages/modern-di)
25
- [![GitHub stars](https://img.shields.io/github/stars/modern-python/modern-di)](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
- [![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
4
- [![Supported versions](https://img.shields.io/pypi/pyversions/modern-di.svg)](https://pypi.python.org/pypi/modern-di)
5
- [![downloads](https://img.shields.io/pypi/dm/modern-di.svg)](https://pypistats.org/packages/modern-di)
6
- [![GitHub stars](https://img.shields.io/github/stars/modern-python/modern-di)](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)
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