Skip to content

Commit 783b91b

Browse files
committed
Fix attribute injection with Annotated types
1 parent 19600d9 commit 783b91b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/dependency_injector/wiring.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def wire( # noqa: C901
415415
providers_map = ProvidersMap(container)
416416

417417
for module in modules:
418-
for member_name, member in inspect.getmembers(module):
418+
for member_name, member in _get_members_and_annotated(module):
419419
if _inspect_filter.is_excluded(member):
420420
continue
421421

@@ -426,7 +426,7 @@ def wire( # noqa: C901
426426
elif inspect.isclass(member):
427427
cls = member
428428
try:
429-
cls_members = inspect.getmembers(cls)
429+
cls_members = _get_members_and_annotated(cls)
430430
except Exception: # noqa
431431
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/441
432432
continue
@@ -1025,3 +1025,18 @@ def _patched(*args, **kwargs):
10251025
patched.closing,
10261026
)
10271027
return cast(F, _patched)
1028+
1029+
1030+
def _get_members_and_annotated(obj: Any) -> list[tuple[str, Any]]:
1031+
members = inspect.getmembers(obj)
1032+
try:
1033+
annotations = inspect.get_annotations(obj)
1034+
except Exception:
1035+
annotations = {}
1036+
for annotation_name, annotation in annotations.items():
1037+
if get_origin(annotation) is Annotated:
1038+
args = get_args(annotation)
1039+
if len(args) > 1:
1040+
member = args[1]
1041+
members.append((annotation_name, member))
1042+
return members

0 commit comments

Comments
 (0)