Skip to content

Commit 2afbbab

Browse files
committed
annotationlib: Remove some unnecessary dict copies
1 parent ccad61e commit 2afbbab

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

Lib/annotationlib.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,11 @@ def get_annotations(
720720
# For STRING, we try to call __annotate__
721721
ann = _get_and_call_annotate(obj, format)
722722
if ann is not None:
723-
return ann
723+
return dict(ann)
724724
# But if we didn't get it, we use __annotations__ instead.
725725
ann = _get_dunder_annotations(obj)
726726
if ann is not None:
727-
ann = annotations_to_string(ann)
727+
return annotations_to_string(ann)
728728
case Format.VALUE_WITH_FAKE_GLOBALS:
729729
raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only")
730730
case _:
@@ -832,22 +832,15 @@ def _get_and_call_annotate(obj, format):
832832
ann = call_annotate_function(annotate, format, owner=obj)
833833
if not isinstance(ann, dict):
834834
raise ValueError(f"{obj!r}.__annotate__ returned a non-dict")
835-
return dict(ann)
835+
return ann
836836
return None
837837

838838

839839
def _get_dunder_annotations(obj):
840-
if isinstance(obj, type):
841-
try:
842-
ann = obj.__annotations__
843-
except AttributeError:
844-
# For static types, the descriptor raises AttributeError.
845-
return None
846-
else:
847-
ann = getattr(obj, "__annotations__", None)
848-
if ann is None:
849-
return None
840+
ann = getattr(obj, "__annotations__", None)
841+
if ann is None:
842+
return None
850843

851844
if not isinstance(ann, dict):
852845
raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None")
853-
return dict(ann)
846+
return ann

0 commit comments

Comments
 (0)