@@ -77,11 +77,15 @@ def __init__(
7777 self .__forward_is_argument__ = is_argument
7878 self .__forward_is_class__ = is_class
7979 self .__forward_module__ = module
80+ self .__owner__ = owner
81+ # These are always set to None here but may be non-None if a ForwardRef
82+ # is created through __class__ assignment on a _Stringifier object.
8083 self .__globals__ = None
84+ self .__cell__ = None
85+ # These are initially None but serve as a cache and may be set to a non-None
86+ # value later.
8187 self .__code__ = None
8288 self .__ast_node__ = None
83- self .__cell__ = None
84- self .__owner__ = owner
8589
8690 def __init_subclass__ (cls , / , * args , ** kwds ):
8791 raise TypeError ("Cannot subclass ForwardRef" )
@@ -716,11 +720,11 @@ def get_annotations(
716720 # For STRING, we try to call __annotate__
717721 ann = _get_and_call_annotate (obj , format )
718722 if ann is not None :
719- return ann
723+ return dict ( ann )
720724 # But if we didn't get it, we use __annotations__ instead.
721725 ann = _get_dunder_annotations (obj )
722726 if ann is not None :
723- ann = annotations_to_string (ann )
727+ return annotations_to_string (ann )
724728 case Format .VALUE_WITH_FAKE_GLOBALS :
725729 raise ValueError ("The VALUE_WITH_FAKE_GLOBALS format is for internal use only" )
726730 case _:
@@ -813,35 +817,39 @@ def type_repr(value):
813817
814818
815819def annotations_to_string (annotations ):
816- """Convert an annotation dict containing values to approximately the STRING format."""
820+ """Convert an annotation dict containing values to approximately the STRING format.
821+
822+ Always returns a fresh a dictionary.
823+ """
817824 return {
818825 n : t if isinstance (t , str ) else type_repr (t )
819826 for n , t in annotations .items ()
820827 }
821828
822829
823830def _get_and_call_annotate (obj , format ):
831+ """Get the __annotate__ function and call it.
832+
833+ May not return a fresh dictionary.
834+ """
824835 annotate = get_annotate_function (obj )
825836 if annotate is not None :
826837 ann = call_annotate_function (annotate , format , owner = obj )
827838 if not isinstance (ann , dict ):
828839 raise ValueError (f"{ obj !r} .__annotate__ returned a non-dict" )
829- return dict ( ann )
840+ return ann
830841 return None
831842
832843
833844def _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
845+ """Return the annotations for an object, checking that it is a dictionary.
846+
847+ Does not return a fresh dictionary.
848+ """
849+ ann = getattr (obj , "__annotations__" , None )
850+ if ann is None :
851+ return None
844852
845853 if not isinstance (ann , dict ):
846854 raise ValueError (f"{ obj !r} .__annotations__ is neither a dict nor None" )
847- return dict ( ann )
855+ return ann
0 commit comments