anydi 0.71.0__tar.gz → 0.72.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.
- {anydi-0.71.0 → anydi-0.72.0}/PKG-INFO +1 -1
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_container.py +20 -3
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_decorators.py +13 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_graph.py +4 -5
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_resolver.py +23 -18
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/typer.py +3 -1
- {anydi-0.71.0 → anydi-0.72.0}/pyproject.toml +1 -1
- {anydi-0.71.0 → anydi-0.72.0}/README.md +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/__init__.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_async_lock.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_cli.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_context.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_injector.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_marker.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_module.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_provider.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_scanner.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/_types.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/__init__.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/django/__init__.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/fastapi.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/faststream.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/pydantic_settings.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/pytest_plugin.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/starlette/__init__.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/ext/starlette/middleware.py +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/py.typed +0 -0
- {anydi-0.71.0 → anydi-0.72.0}/anydi/testing.py +0 -0
|
@@ -58,6 +58,7 @@ class Container:
|
|
|
58
58
|
|
|
59
59
|
self._resources: dict[str, list[Any]] = defaultdict(list)
|
|
60
60
|
self._aliases: dict[Any, Any] = {} # alias_type → canonical_type
|
|
61
|
+
self._aliases_by_canonical: dict[Any, set[Any]] = defaultdict(set)
|
|
61
62
|
self._singleton_context = InstanceContext()
|
|
62
63
|
self._scoped_context: dict[str, ContextVar[InstanceContext]] = {}
|
|
63
64
|
|
|
@@ -405,12 +406,22 @@ class Container:
|
|
|
405
406
|
f"Alias `{type_repr(alias_type)}` is already registered "
|
|
406
407
|
f"for `{type_repr(self._aliases[alias_type])}`."
|
|
407
408
|
)
|
|
409
|
+
if canonical_type not in self._providers:
|
|
410
|
+
raise ValueError(
|
|
411
|
+
f"Cannot create alias: provider for `{type_repr(canonical_type)}` "
|
|
412
|
+
"is not registered. Register the provider first."
|
|
413
|
+
)
|
|
408
414
|
self._aliases[alias_type] = canonical_type
|
|
415
|
+
self._aliases_by_canonical[canonical_type].add(alias_type)
|
|
409
416
|
|
|
410
417
|
def _resolve_alias(self, dependency_type: Any) -> Any:
|
|
411
418
|
"""Resolve an alias to its canonical type."""
|
|
412
419
|
return self._aliases.get(dependency_type, dependency_type)
|
|
413
420
|
|
|
421
|
+
def get_aliases_for(self, canonical_type: Any, /) -> set[Any]:
|
|
422
|
+
"""Get all aliases that point to the given canonical type."""
|
|
423
|
+
return self._aliases_by_canonical.get(canonical_type, set())
|
|
424
|
+
|
|
414
425
|
def is_registered(self, dependency_type: Any, /) -> bool:
|
|
415
426
|
"""Check if a provider is registered for the specified dependency type."""
|
|
416
427
|
canonical = self._resolve_alias(dependency_type)
|
|
@@ -722,20 +733,23 @@ class Container:
|
|
|
722
733
|
# Check if it's a @provided class
|
|
723
734
|
if inspect.isclass(dependency_type) and is_provided(dependency_type):
|
|
724
735
|
provided_scope = dependency_type.__provided__["scope"]
|
|
736
|
+
from_context = dependency_type.__provided__.get(
|
|
737
|
+
"from_context", False
|
|
738
|
+
)
|
|
725
739
|
|
|
726
740
|
# Auto-register @provided class
|
|
727
741
|
dep_provider = self._register_provider(
|
|
728
742
|
dependency_type,
|
|
729
743
|
dependency_type,
|
|
730
744
|
provided_scope,
|
|
731
|
-
|
|
745
|
+
from_context,
|
|
732
746
|
False,
|
|
733
747
|
None,
|
|
734
748
|
)
|
|
735
749
|
# Register aliases if specified
|
|
736
750
|
aliases = to_list(dependency_type.__provided__.get("alias"))
|
|
737
751
|
for alias_type in aliases:
|
|
738
|
-
self.
|
|
752
|
+
self.alias(alias_type, dependency_type)
|
|
739
753
|
# Recursively ensure the @provided class is resolved
|
|
740
754
|
dep_provider = self._ensure_provider_resolved(
|
|
741
755
|
dep_provider, resolving
|
|
@@ -833,6 +847,9 @@ class Container:
|
|
|
833
847
|
del self._providers[provider.dependency_type]
|
|
834
848
|
if provider.is_resource:
|
|
835
849
|
self._resources[provider.scope].remove(provider.dependency_type)
|
|
850
|
+
# Remove aliases pointing to this provider
|
|
851
|
+
for alias in self._aliases_by_canonical.pop(provider.dependency_type, set()):
|
|
852
|
+
self._aliases.pop(alias, None)
|
|
836
853
|
|
|
837
854
|
# == Instance Resolution ==
|
|
838
855
|
|
|
@@ -1039,7 +1056,7 @@ class Container:
|
|
|
1039
1056
|
# Register aliases if specified
|
|
1040
1057
|
provided_meta = param_dependency_type.__provided__
|
|
1041
1058
|
for alias_type in to_list(provided_meta.get("alias")):
|
|
1042
|
-
self.
|
|
1059
|
+
self.alias(alias_type, param_dependency_type)
|
|
1043
1060
|
elif param.has_default:
|
|
1044
1061
|
# Has default, can be missing
|
|
1045
1062
|
resolved_params.append(param)
|
|
@@ -34,12 +34,22 @@ class ProvidedMetadata(TypedDict):
|
|
|
34
34
|
from_context: NotRequired[bool]
|
|
35
35
|
|
|
36
36
|
|
|
37
|
+
def _check_already_provided(cls: type) -> None:
|
|
38
|
+
"""Check if class already has __provided__ defined directly on it."""
|
|
39
|
+
if "__provided__" in cls.__dict__:
|
|
40
|
+
raise TypeError(
|
|
41
|
+
f"Class `{cls.__name__}` already has `__provided__` defined. "
|
|
42
|
+
"Remove the duplicate scope decorator or manual `__provided__` attribute."
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
37
46
|
def provided(
|
|
38
47
|
*, scope: Scope, alias: Any = NOT_SET, from_context: bool = False
|
|
39
48
|
) -> Callable[[ClassT], ClassT]:
|
|
40
49
|
"""Decorator for marking a class as provided by AnyDI with a specific scope."""
|
|
41
50
|
|
|
42
51
|
def decorator(cls: ClassT) -> ClassT:
|
|
52
|
+
_check_already_provided(cls)
|
|
43
53
|
metadata: ProvidedMetadata = {"scope": scope}
|
|
44
54
|
if alias is not NOT_SET:
|
|
45
55
|
metadata["alias"] = alias
|
|
@@ -67,6 +77,7 @@ def singleton(
|
|
|
67
77
|
"""Decorator for marking a class as a singleton dependency."""
|
|
68
78
|
|
|
69
79
|
def decorator(c: ClassT) -> ClassT:
|
|
80
|
+
_check_already_provided(c)
|
|
70
81
|
metadata: ProvidedMetadata = {"scope": "singleton"}
|
|
71
82
|
if alias is not NOT_SET:
|
|
72
83
|
metadata["alias"] = alias
|
|
@@ -95,6 +106,7 @@ def transient(
|
|
|
95
106
|
"""Decorator for marking a class as a transient dependency."""
|
|
96
107
|
|
|
97
108
|
def decorator(c: ClassT) -> ClassT:
|
|
109
|
+
_check_already_provided(c)
|
|
98
110
|
metadata: ProvidedMetadata = {"scope": "transient"}
|
|
99
111
|
if alias is not NOT_SET:
|
|
100
112
|
metadata["alias"] = alias
|
|
@@ -127,6 +139,7 @@ def request(
|
|
|
127
139
|
"""Decorator for marking a class as a request-scoped dependency."""
|
|
128
140
|
|
|
129
141
|
def decorator(c: ClassT) -> ClassT:
|
|
142
|
+
_check_already_provided(c)
|
|
130
143
|
metadata: ProvidedMetadata = {"scope": "request"}
|
|
131
144
|
if alias is not NOT_SET:
|
|
132
145
|
metadata["alias"] = alias
|
|
@@ -21,11 +21,10 @@ class Graph:
|
|
|
21
21
|
|
|
22
22
|
def _get_aliases_for(self, dependency_type: Any) -> list[str]:
|
|
23
23
|
"""Get list of alias names that point to a dependency type."""
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return aliases
|
|
24
|
+
return [
|
|
25
|
+
type_repr(alias).rsplit(".", 1)[-1]
|
|
26
|
+
for alias in self._container.get_aliases_for(dependency_type)
|
|
27
|
+
]
|
|
29
28
|
|
|
30
29
|
def draw(
|
|
31
30
|
self,
|
|
@@ -53,22 +53,20 @@ class Resolver:
|
|
|
53
53
|
def add_override(self, dependency_type: Any, instance: Any) -> None:
|
|
54
54
|
"""Add an override for a type, its canonical type, and all aliases."""
|
|
55
55
|
self._overrides[dependency_type] = instance
|
|
56
|
-
|
|
57
|
-
if
|
|
58
|
-
self._overrides[
|
|
59
|
-
for alias
|
|
60
|
-
|
|
61
|
-
self._overrides[alias] = instance
|
|
56
|
+
canonical_type = self._container.aliases.get(dependency_type)
|
|
57
|
+
if canonical_type is not None:
|
|
58
|
+
self._overrides[canonical_type] = instance
|
|
59
|
+
for alias in self._container.get_aliases_for(dependency_type):
|
|
60
|
+
self._overrides[alias] = instance
|
|
62
61
|
|
|
63
62
|
def remove_override(self, dependency_type: Any) -> None:
|
|
64
63
|
"""Remove an override for a type, its canonical type, and all aliases."""
|
|
65
64
|
self._overrides.pop(dependency_type, None)
|
|
66
|
-
|
|
67
|
-
if
|
|
68
|
-
self._overrides.pop(
|
|
69
|
-
for alias
|
|
70
|
-
|
|
71
|
-
self._overrides.pop(alias, None)
|
|
65
|
+
canonical_type = self._container.aliases.get(dependency_type)
|
|
66
|
+
if canonical_type is not None:
|
|
67
|
+
self._overrides.pop(canonical_type, None)
|
|
68
|
+
for alias in self._container.get_aliases_for(dependency_type):
|
|
69
|
+
self._overrides.pop(alias, None)
|
|
72
70
|
|
|
73
71
|
def clear_caches(self) -> None:
|
|
74
72
|
"""Clear all cached resolvers."""
|
|
@@ -103,7 +101,11 @@ class Resolver:
|
|
|
103
101
|
for param in provider.parameters:
|
|
104
102
|
if param.provider is not None:
|
|
105
103
|
# Look up the current provider to handle overrides
|
|
106
|
-
|
|
104
|
+
# Resolve alias to canonical type if needed
|
|
105
|
+
canonical_type = self._container.aliases.get(
|
|
106
|
+
param.dependency_type, param.dependency_type
|
|
107
|
+
)
|
|
108
|
+
current_provider = self._container.providers.get(canonical_type)
|
|
107
109
|
if current_provider is not None:
|
|
108
110
|
self.compile(current_provider, is_async=is_async)
|
|
109
111
|
else:
|
|
@@ -117,10 +119,9 @@ class Resolver:
|
|
|
117
119
|
# Store the compiled functions in the cache
|
|
118
120
|
cache[provider.dependency_type] = compiled
|
|
119
121
|
|
|
120
|
-
# Also store under
|
|
121
|
-
for alias
|
|
122
|
-
|
|
123
|
-
cache[alias] = compiled
|
|
122
|
+
# Also store under aliases that point to this canonical type
|
|
123
|
+
for alias in self._container.get_aliases_for(provider.dependency_type):
|
|
124
|
+
cache[alias] = compiled
|
|
124
125
|
|
|
125
126
|
return compiled
|
|
126
127
|
|
|
@@ -198,7 +199,11 @@ class Resolver:
|
|
|
198
199
|
|
|
199
200
|
if param.provider is not None:
|
|
200
201
|
# Look up the current provider from the container to handle overrides
|
|
201
|
-
|
|
202
|
+
# Resolve alias to canonical type if needed
|
|
203
|
+
canonical_type = self._container.aliases.get(
|
|
204
|
+
param.dependency_type, param.dependency_type
|
|
205
|
+
)
|
|
206
|
+
current_provider = self._container.providers.get(canonical_type)
|
|
202
207
|
if current_provider is not None:
|
|
203
208
|
compiled = cache.get(current_provider.dependency_type)
|
|
204
209
|
else:
|
|
@@ -97,8 +97,10 @@ def _process_callback(callback: Callable[..., Any], container: Container) -> Any
|
|
|
97
97
|
processed_parameter = container._injector.unwrap_parameter(parameter)
|
|
98
98
|
if should_inject:
|
|
99
99
|
injected_param_names.add(parameter.name)
|
|
100
|
+
# Resolve alias to canonical type if needed
|
|
101
|
+
canonical_type = container.aliases.get(dependency_type, dependency_type)
|
|
100
102
|
try:
|
|
101
|
-
scopes.add(container.providers[
|
|
103
|
+
scopes.add(container.providers[canonical_type].scope)
|
|
102
104
|
except KeyError:
|
|
103
105
|
if inspect.isclass(dependency_type) and is_provided(dependency_type):
|
|
104
106
|
scopes.add(dependency_type.__provided__["scope"])
|
|
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
|