From 164a45cd8268fc77a7a01106ba107aae050da6c6 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Wed, 21 May 2025 13:58:35 -0400 Subject: [PATCH] Fix: robust Annotated detection for wiring across Python versions --- src/dependency_injector/wiring.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index 45ffd2ba..b8bd968c 100644 --- a/src/dependency_injector/wiring.py +++ b/src/dependency_injector/wiring.py @@ -578,8 +578,13 @@ def _unpatch_attribute(patched: PatchedAttribute) -> None: def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]: - if get_origin(parameter.annotation) is Annotated: - marker = get_args(parameter.annotation)[1] + is_annotated = ( + isinstance(annotation, type(Annotated)) + and get_origin(annotation) is not None + and get_origin(annotation) is get_origin(Annotated) + ) + if is_annotated: + marker = get_args(annotation)[1] else: marker = parameter.default @@ -1034,7 +1039,12 @@ def _get_members_and_annotated(obj: Any) -> Iterable[Tuple[str, Any]]: except Exception: annotations = {} for annotation_name, annotation in annotations.items(): - if get_origin(annotation) is Annotated: + is_annotated = ( + isinstance(annotation, type(Annotated)) + and get_origin(annotation) is not None + and get_origin(annotation) is get_origin(Annotated) + ) + if is_annotated: args = get_args(annotation) if len(args) > 1: member = args[1]