anydi 0.26.1__tar.gz → 0.26.2a0__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 (27) hide show
  1. {anydi-0.26.1 → anydi-0.26.2a0}/PKG-INFO +1 -1
  2. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/_container.py +11 -1
  3. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/pytest_plugin.py +23 -13
  4. {anydi-0.26.1 → anydi-0.26.2a0}/pyproject.toml +1 -1
  5. {anydi-0.26.1 → anydi-0.26.2a0}/LICENSE +0 -0
  6. {anydi-0.26.1 → anydi-0.26.2a0}/README.md +0 -0
  7. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/__init__.py +0 -0
  8. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/_context.py +0 -0
  9. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/_logger.py +0 -0
  10. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/_module.py +0 -0
  11. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/_scanner.py +0 -0
  12. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/_types.py +0 -0
  13. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/_utils.py +0 -0
  14. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/__init__.py +0 -0
  15. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/__init__.py +0 -0
  16. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/_container.py +0 -0
  17. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/_settings.py +0 -0
  18. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/_utils.py +0 -0
  19. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/apps.py +0 -0
  20. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/middleware.py +0 -0
  21. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/ninja/__init__.py +0 -0
  22. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/ninja/_operation.py +0 -0
  23. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/django/ninja/_signature.py +0 -0
  24. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/fastapi.py +0 -0
  25. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/starlette/__init__.py +0 -0
  26. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/ext/starlette/middleware.py +0 -0
  27. {anydi-0.26.1 → anydi-0.26.2a0}/anydi/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: anydi
3
- Version: 0.26.1
3
+ Version: 0.26.2a0
4
4
  Summary: Dependency Injection library
5
5
  Home-page: https://github.com/antonrh/anydi
6
6
  License: MIT
@@ -225,7 +225,7 @@ class Container:
225
225
  scoped_context.delete(interface)
226
226
 
227
227
  # Cleanup provider references
228
- self._providers.pop(interface, None)
228
+ self._delete_provider(interface)
229
229
 
230
230
  def _get_provider(self, interface: AnyInterface) -> Provider:
231
231
  """Get provider by interface.
@@ -288,6 +288,16 @@ class Container:
288
288
  if provider.is_resource:
289
289
  self._providers_cache[provider.scope].append(interface)
290
290
 
291
+ def _delete_provider(self, interface: AnyInterface) -> None:
292
+ """Delete a provider by interface.
293
+
294
+ Args:
295
+ interface: The interface for which to delete the provider.
296
+ """
297
+ provider = self._providers.pop(interface, None)
298
+ if provider is not None and provider.is_resource:
299
+ self._providers_cache[provider.scope].remove(interface)
300
+
291
301
  def _validate_provider_scope(self, provider: Provider) -> None:
292
302
  """Validate the scope of a provider.
293
303
 
@@ -6,6 +6,7 @@ from typing import Any, Callable, Iterator, cast
6
6
  import pytest
7
7
 
8
8
  from anydi import Container
9
+ from anydi._types import is_marker
9
10
  from anydi._utils import get_typed_parameters
10
11
 
11
12
 
@@ -23,6 +24,12 @@ def pytest_addoption(parser: pytest.Parser) -> None:
23
24
  type="bool",
24
25
  default=False,
25
26
  )
27
+ parser.addini(
28
+ "anydi_inject_auto",
29
+ help="Automatically inject dependencies",
30
+ type="bool",
31
+ default=True,
32
+ )
26
33
 
27
34
 
28
35
  CONTAINER_FIXTURE_NAME = "container"
@@ -63,13 +70,18 @@ def _anydi_injected_parameter_iterator(
63
70
  request: pytest.FixtureRequest,
64
71
  _anydi_unresolved: list[str],
65
72
  ) -> Callable[[], Iterator[tuple[str, Any]]]:
73
+ registered_fixtures = request.session._fixturemanager._arg2fixturedefs # noqa
74
+ inject_auto = cast(bool, request.config.getini("anydi_inject_auto"))
75
+
66
76
  def _iterator() -> Iterator[tuple[str, inspect.Parameter]]:
67
77
  for parameter in get_typed_parameters(request.function):
68
- if (
69
- ((interface := parameter.annotation) is parameter.empty)
70
- or interface in _anydi_unresolved
71
- or parameter.name in request.node.funcargs
72
- ):
78
+ interface = parameter.annotation
79
+ if interface is parameter.empty:
80
+ continue
81
+ if not inject_auto and is_marker(parameter.default):
82
+ yield parameter.name, interface
83
+ continue
84
+ if interface in _anydi_unresolved or parameter.name in registered_fixtures:
73
85
  continue
74
86
  yield parameter.name, interface
75
87
 
@@ -92,11 +104,10 @@ def _anydi_inject(
92
104
  container = cast(Container, request.getfixturevalue("anydi_setup_container"))
93
105
 
94
106
  for argname, interface in _anydi_injected_parameter_iterator():
95
- try:
96
- # Release the instance if it was already resolved
107
+ # Release the instance if it was already resolved
108
+ if container.is_resolved(interface):
97
109
  container.release(interface)
98
- except LookupError:
99
- pass
110
+
100
111
  try:
101
112
  # Resolve the instance
102
113
  instance = container.resolve(interface)
@@ -121,11 +132,10 @@ async def _anydi_ainject(
121
132
  container = cast(Container, request.getfixturevalue("anydi_setup_container"))
122
133
 
123
134
  for argname, interface in _anydi_injected_parameter_iterator():
124
- try:
125
- # Release the instance if it was already resolved
135
+ # Release the instance if it was already resolved
136
+ if container.is_resolved(interface):
126
137
  container.release(interface)
127
- except LookupError:
128
- pass
138
+
129
139
  try:
130
140
  # Resolve the instance
131
141
  instance = await container.aresolve(interface)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "anydi"
3
- version = "0.26.1"
3
+ version = "0.26.2a0"
4
4
  description = "Dependency Injection library"
5
5
  authors = ["Anton Ruhlov <antonruhlov@gmail.com>"]
6
6
  license = "MIT"
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