@@ -716,11 +716,11 @@ def get_annotations(
716716 # For STRING, we try to call __annotate__
717717 ann = _get_and_call_annotate (obj , format )
718718 if ann is not None :
719- return ann
719+ return dict ( ann )
720720 # But if we didn't get it, we use __annotations__ instead.
721721 ann = _get_dunder_annotations (obj )
722722 if ann is not None :
723- ann = annotations_to_string (ann )
723+ return annotations_to_string (ann )
724724 case Format .VALUE_WITH_FAKE_GLOBALS :
725725 raise ValueError ("The VALUE_WITH_FAKE_GLOBALS format is for internal use only" )
726726 case _:
@@ -813,35 +813,39 @@ def type_repr(value):
813813
814814
815815def annotations_to_string (annotations ):
816- """Convert an annotation dict containing values to approximately the STRING format."""
816+ """Convert an annotation dict containing values to approximately the STRING format.
817+
818+ Always returns a fresh a dictionary.
819+ """
817820 return {
818821 n : t if isinstance (t , str ) else type_repr (t )
819822 for n , t in annotations .items ()
820823 }
821824
822825
823826def _get_and_call_annotate (obj , format ):
827+ """Get the __annotate__ function and call it.
828+
829+ May not return a fresh dictionary.
830+ """
824831 annotate = get_annotate_function (obj )
825832 if annotate is not None :
826833 ann = call_annotate_function (annotate , format , owner = obj )
827834 if not isinstance (ann , dict ):
828835 raise ValueError (f"{ obj !r} .__annotate__ returned a non-dict" )
829- return dict ( ann )
836+ return ann
830837 return None
831838
832839
833840def _get_dunder_annotations (obj ):
834- if isinstance (obj , type ):
835- try :
836- ann = obj .__annotations__
837- except AttributeError :
838- # For static types, the descriptor raises AttributeError.
839- return None
840- else :
841- ann = getattr (obj , "__annotations__" , None )
842- if ann is None :
843- return None
841+ """Return the annotations for an object, checking that it is a dictionary.
842+
843+ Does not return a fresh dictionary.
844+ """
845+ ann = getattr (obj , "__annotations__" , None )
846+ if ann is None :
847+ return None
844848
845849 if not isinstance (ann , dict ):
846850 raise ValueError (f"{ obj !r} .__annotations__ is neither a dict nor None" )
847- return dict ( ann )
851+ return ann
0 commit comments