diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index 1effd16f..6a4c5c7a 100644 --- a/src/dependency_injector/wiring.py +++ b/src/dependency_injector/wiring.py @@ -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