mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-17 11:32:21 +03:00
Refactor: extract annotation retrieval and improve typing for Python 3.9 compatibility
This commit is contained in:
parent
164a45cd82
commit
2ab78f34eb
|
@ -578,13 +578,12 @@ def _unpatch_attribute(patched: PatchedAttribute) -> None:
|
||||||
|
|
||||||
|
|
||||||
def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
|
def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
|
||||||
is_annotated = (
|
if get_origin(parameter.annotation) is Annotated:
|
||||||
isinstance(annotation, type(Annotated))
|
args = get_args(parameter.annotation)
|
||||||
and get_origin(annotation) is not None
|
if len(args) > 1:
|
||||||
and get_origin(annotation) is get_origin(Annotated)
|
marker = args[1]
|
||||||
)
|
else:
|
||||||
if is_annotated:
|
marker = None
|
||||||
marker = get_args(annotation)[1]
|
|
||||||
else:
|
else:
|
||||||
marker = parameter.default
|
marker = parameter.default
|
||||||
|
|
||||||
|
@ -1032,19 +1031,18 @@ def _get_sync_patched(fn: F, patched: PatchedCallable) -> F:
|
||||||
return cast(F, _patched)
|
return cast(F, _patched)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_annotations(obj: Any) -> Dict[str, Any]:
|
||||||
|
if sys.version_info >= (3, 10):
|
||||||
|
return inspect.get_annotations(obj)
|
||||||
|
else:
|
||||||
|
return getattr(obj, "__annotations__", {})
|
||||||
|
|
||||||
|
|
||||||
def _get_members_and_annotated(obj: Any) -> Iterable[Tuple[str, Any]]:
|
def _get_members_and_annotated(obj: Any) -> Iterable[Tuple[str, Any]]:
|
||||||
members = inspect.getmembers(obj)
|
members = inspect.getmembers(obj)
|
||||||
try:
|
annotations = _get_annotations(obj)
|
||||||
annotations = inspect.get_annotations(obj)
|
|
||||||
except Exception:
|
|
||||||
annotations = {}
|
|
||||||
for annotation_name, annotation in annotations.items():
|
for annotation_name, annotation in annotations.items():
|
||||||
is_annotated = (
|
if get_origin(annotation) 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)
|
args = get_args(annotation)
|
||||||
if len(args) > 1:
|
if len(args) > 1:
|
||||||
member = args[1]
|
member = args[1]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user