|
3 | 3 |
|
4 | 4 | from typing import Any, Callable, Dict, List, Optional, TypeVar, Type |
5 | 5 |
|
| 6 | +from dependency_injection.tags.all_tagged import AllTagged |
| 7 | +from dependency_injection.tags.any_tagged import AnyTagged |
| 8 | +from dependency_injection.tags.tagged import Tagged |
6 | 9 | from dependency_injection.registration import Registration |
7 | 10 | from dependency_injection.scope import DEFAULT_SCOPE_NAME, Scope |
8 | 11 | from dependency_injection.utils.singleton_meta import SingletonMeta |
@@ -225,12 +228,47 @@ def _inject_dependencies( |
225 | 228 | # **kwargs parameter |
226 | 229 | pass |
227 | 230 | else: |
228 | | - # Check if constructor_args has an argument with the same name |
| 231 | + # Priority 1: Check if constructor_args has argument with same name |
229 | 232 | if constructor_args and param_name in constructor_args: |
230 | 233 | dependencies[param_name] = constructor_args[param_name] |
231 | 234 | else: |
232 | | - dependencies[param_name] = self.resolve( |
233 | | - param_info.annotation, scope_name=scope_name |
234 | | - ) |
| 235 | + # Priority 2: Handle List[Tagged], List[AnyTagged[...]], ... |
| 236 | + tagged_dependencies = [] |
| 237 | + if ( |
| 238 | + hasattr(param_info.annotation, "__origin__") |
| 239 | + and param_info.annotation.__origin__ is list |
| 240 | + ): |
| 241 | + inner_type = param_info.annotation.__args__[0] |
| 242 | + |
| 243 | + if isinstance(inner_type, Tagged): |
| 244 | + tagged_dependencies = self.resolve_all( |
| 245 | + tags={inner_type.tag} |
| 246 | + ) |
| 247 | + |
| 248 | + elif isinstance(inner_type, AnyTagged): |
| 249 | + tagged_dependencies = self.resolve_all( |
| 250 | + tags=inner_type.tags, match_all_tags=False |
| 251 | + ) |
| 252 | + |
| 253 | + elif isinstance(inner_type, AllTagged): |
| 254 | + tagged_dependencies = self.resolve_all( |
| 255 | + tags=inner_type.tags, match_all_tags=True |
| 256 | + ) |
| 257 | + |
| 258 | + dependencies[param_name] = tagged_dependencies |
| 259 | + |
| 260 | + else: |
| 261 | + # Priority 3: Regular type resolution |
| 262 | + try: |
| 263 | + dependencies[param_name] = self.resolve( |
| 264 | + param_info.annotation, scope_name=scope_name |
| 265 | + ) |
| 266 | + except KeyError: |
| 267 | + raise ValueError( |
| 268 | + f"Cannot resolve dependency for parameter " |
| 269 | + f"'{param_name}' of type " |
| 270 | + f"'{param_info.annotation}' in class " |
| 271 | + f"'{implementation.__name__}'." |
| 272 | + ) |
235 | 273 |
|
236 | 274 | return implementation(**dependencies) |
0 commit comments