annotationlib: Remove some unnecessary dict copies (#132495)
This commit is contained in:
parent
c35c7353eb
commit
a8ca26d250
@ -716,11 +716,11 @@ def get_annotations(
|
|||||||
# For STRING, we try to call __annotate__
|
# For STRING, we try to call __annotate__
|
||||||
ann = _get_and_call_annotate(obj, format)
|
ann = _get_and_call_annotate(obj, format)
|
||||||
if ann is not None:
|
if ann is not None:
|
||||||
return ann
|
return dict(ann)
|
||||||
# But if we didn't get it, we use __annotations__ instead.
|
# But if we didn't get it, we use __annotations__ instead.
|
||||||
ann = _get_dunder_annotations(obj)
|
ann = _get_dunder_annotations(obj)
|
||||||
if ann is not None:
|
if ann is not None:
|
||||||
ann = annotations_to_string(ann)
|
return annotations_to_string(ann)
|
||||||
case Format.VALUE_WITH_FAKE_GLOBALS:
|
case Format.VALUE_WITH_FAKE_GLOBALS:
|
||||||
raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only")
|
raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only")
|
||||||
case _:
|
case _:
|
||||||
@ -813,7 +813,10 @@ def type_repr(value):
|
|||||||
|
|
||||||
|
|
||||||
def annotations_to_string(annotations):
|
def annotations_to_string(annotations):
|
||||||
"""Convert an annotation dict containing values to approximately the STRING format."""
|
"""Convert an annotation dict containing values to approximately the STRING format.
|
||||||
|
|
||||||
|
Always returns a fresh a dictionary.
|
||||||
|
"""
|
||||||
return {
|
return {
|
||||||
n: t if isinstance(t, str) else type_repr(t)
|
n: t if isinstance(t, str) else type_repr(t)
|
||||||
for n, t in annotations.items()
|
for n, t in annotations.items()
|
||||||
@ -821,27 +824,28 @@ def annotations_to_string(annotations):
|
|||||||
|
|
||||||
|
|
||||||
def _get_and_call_annotate(obj, format):
|
def _get_and_call_annotate(obj, format):
|
||||||
|
"""Get the __annotate__ function and call it.
|
||||||
|
|
||||||
|
May not return a fresh dictionary.
|
||||||
|
"""
|
||||||
annotate = get_annotate_function(obj)
|
annotate = get_annotate_function(obj)
|
||||||
if annotate is not None:
|
if annotate is not None:
|
||||||
ann = call_annotate_function(annotate, format, owner=obj)
|
ann = call_annotate_function(annotate, format, owner=obj)
|
||||||
if not isinstance(ann, dict):
|
if not isinstance(ann, dict):
|
||||||
raise ValueError(f"{obj!r}.__annotate__ returned a non-dict")
|
raise ValueError(f"{obj!r}.__annotate__ returned a non-dict")
|
||||||
return dict(ann)
|
return ann
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _get_dunder_annotations(obj):
|
def _get_dunder_annotations(obj):
|
||||||
if isinstance(obj, type):
|
"""Return the annotations for an object, checking that it is a dictionary.
|
||||||
try:
|
|
||||||
ann = obj.__annotations__
|
Does not return a fresh dictionary.
|
||||||
except AttributeError:
|
"""
|
||||||
# For static types, the descriptor raises AttributeError.
|
ann = getattr(obj, "__annotations__", None)
|
||||||
return None
|
if ann is None:
|
||||||
else:
|
return None
|
||||||
ann = getattr(obj, "__annotations__", None)
|
|
||||||
if ann is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if not isinstance(ann, dict):
|
if not isinstance(ann, dict):
|
||||||
raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None")
|
raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None")
|
||||||
return dict(ann)
|
return ann
|
||||||
|
Loading…
x
Reference in New Issue
Block a user