Fix attribute injection with Annotated types

This commit is contained in:
Roman Mogylatov 2025-05-21 12:21:10 -04:00
parent 19600d9d08
commit 783b91bf19

View File

@ -415,7 +415,7 @@ def wire( # noqa: C901
providers_map = ProvidersMap(container)
for module in modules:
for member_name, member in inspect.getmembers(module):
for member_name, member in _get_members_and_annotated(module):
if _inspect_filter.is_excluded(member):
continue
@ -426,7 +426,7 @@ def wire( # noqa: C901
elif inspect.isclass(member):
cls = member
try:
cls_members = inspect.getmembers(cls)
cls_members = _get_members_and_annotated(cls)
except Exception: # noqa
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/441
continue
@ -1025,3 +1025,18 @@ def _get_sync_patched(fn: F, patched: PatchedCallable) -> F:
patched.closing,
)
return cast(F, _patched)
def _get_members_and_annotated(obj: Any) -> list[tuple[str, Any]]:
members = inspect.getmembers(obj)
try:
annotations = inspect.get_annotations(obj)
except Exception:
annotations = {}
for annotation_name, annotation in annotations.items():
if get_origin(annotation) is Annotated:
args = get_args(annotation)
if len(args) > 1:
member = args[1]
members.append((annotation_name, member))
return members