@@ -649,8 +649,12 @@ def get_annotations(
649649):
650650    """Compute the annotations dict for an object. 
651651
652-     obj may be a callable, class, or module. 
653-     Passing in an object of any other type raises TypeError. 
652+     obj may be a callable, class, module, or any other object with an 
653+     __annotations__ or __annotate__ attribute. For the VALUE and 
654+     FORWARDREF formats, __annotations__ is tried first, and __annotate__ 
655+     is used as a fallback. The STRING format uses __annotate__ if it exists, 
656+     but falls back to accessing __annotations__ and using annotations_to_string() 
657+     to stringify the annotations. 
654658
655659    Returns a dict.  get_annotations() returns a new dict every time 
656660    it's called; calling it twice on the same object will return two 
@@ -696,14 +700,21 @@ def get_annotations(
696700
697701    match  format :
698702        case  Format .VALUE :
699-             # For VALUE, we only  look at __annotations__ 
703+             # For VALUE, we first  look at __annotations__ 
700704            ann  =  _get_dunder_annotations (obj )
705+ 
706+             # If it's not there, try __annotate__ instead 
707+             if  ann  is  None :
708+                 ann  =  _get_and_call_annotate (obj , format )
701709        case  Format .FORWARDREF :
702710            # For FORWARDREF, we use __annotations__ if it exists 
703711            try :
704-                 return   dict ( _get_dunder_annotations (obj ) )
712+                 ann   =   _get_dunder_annotations (obj )
705713            except  NameError :
706714                pass 
715+             else :
716+                 if  ann  is  not   None :
717+                     return  dict (ann )
707718
708719            # But if __annotations__ threw a NameError, we try calling __annotate__ 
709720            ann  =  _get_and_call_annotate (obj , format )
@@ -713,14 +724,19 @@ def get_annotations(
713724            # If that didn't work either, we have a very weird object: evaluating 
714725            # __annotations__ threw NameError and there is no __annotate__. In that case, 
715726            # we fall back to trying __annotations__ again. 
716-             return  dict (_get_dunder_annotations (obj ))
727+             ann  =  _get_dunder_annotations (obj )
728+             if  ann  is  None :
729+                 return  {}
730+             return  dict (ann )
717731        case  Format .STRING :
718732            # For STRING, we try to call __annotate__ 
719733            ann  =  _get_and_call_annotate (obj , format )
720734            if  ann  is  not   None :
721735                return  ann 
722736            # But if we didn't get it, we use __annotations__ instead. 
723737            ann  =  _get_dunder_annotations (obj )
738+             if  ann  is  None :
739+                 return  {}
724740            return  annotations_to_string (ann )
725741        case  Format .VALUE_WITH_FAKE_GLOBALS :
726742            raise  ValueError ("The VALUE_WITH_FAKE_GLOBALS format is for internal use only" )
@@ -836,11 +852,11 @@ def _get_dunder_annotations(obj):
836852            ann  =  _BASE_GET_ANNOTATIONS (obj )
837853        except  AttributeError :
838854            # For static types, the descriptor raises AttributeError. 
839-             return  {} 
855+             return  None 
840856    else :
841857        ann  =  getattr (obj , "__annotations__" , None )
842858        if  ann  is  None :
843-             return  {} 
859+             return  None 
844860
845861    if  not  isinstance (ann , dict ):
846862        raise  ValueError (f"{ obj !r}  .__annotations__ is neither a dict nor None" )
0 commit comments