@@ -900,7 +900,9 @@ def _get_annotate_attr(annotate, attr, default):
900900 if call_func := getattr (annotate .__call__ , "__func__" , None ):
901901 return getattr (call_func , attr , default )
902902
903- if isinstance (annotate , type ):
903+ # Classes and generics are callable, usually the __init__ method sets attributes,
904+ # so let's access this method for fake globals and the like.
905+ if isinstance (annotate , type ) or isinstance (annotate , types .GenericAlias ):
904906 return getattr (annotate .__init__ , attr , default )
905907
906908 if (wrapped := getattr (annotate , "__wrapped__" , None )) is not None :
@@ -936,6 +938,17 @@ def _direct_call_annotate(func, annotate, format):
936938 func (inst , format )
937939 return inst
938940
941+ # Generic instantiation is slightly different.
942+ if isinstance (annotate , types .GenericAlias ):
943+ inst = annotate .__new__ (annotate .__origin__ )
944+ func (inst , format )
945+ # Try to set the original class on the instance, if possible.
946+ try :
947+ inst .__orig_class__ = annotate
948+ except Exception :
949+ pass
950+ return inst
951+
939952 if functools := sys .modules .get ("functools" , None ):
940953 # If annotate is a partial function, re-create it with the new function object.
941954 # We could call the function directly, but then we'd have to handle placeholders,
0 commit comments