@@ -1559,6 +1559,44 @@ def __init__(self, format, /, __Format=Format,
15591559 # We manually set the __orig_class__ for this special-case, check this too.
15601560 self .assertEqual (annotations .__orig_class__ , Annotate [str , type ])
15611561
1562+ def test_callable_typing_generic_class_annotate_forwardref_fakeglobals (self ):
1563+ # Normally, generics are 'typing._GenericAlias' objects. These are implemented
1564+ # in Python with a __call__ method (in _typing.BaseGenericAlias), but this
1565+ # needs to be bypassed so we can inject fake globals into the origin class'
1566+ # __init__ method.
1567+ class Annotate [T ]:
1568+ def __init__ (self , format , / , __Format = Format ,
1569+ __NotImplementedError = NotImplementedError ):
1570+ if format == __Format .VALUE :
1571+ self .data = {'x' : str }
1572+ elif format == __Format .VALUE_WITH_FAKE_GLOBALS :
1573+ self .data = {"x" : int }
1574+ else :
1575+ raise __NotImplementedError (format )
1576+ def __getitem__ (self , item ):
1577+ return self .data [item ]
1578+ def __iter__ (self ):
1579+ return iter (self .data )
1580+ def __len__ (self ):
1581+ return len (self .data )
1582+ def __getattr__ (self , attr ):
1583+ val = getattr (collections .abc .Mapping , attr )
1584+ if isinstance (val , types .FunctionType ):
1585+ return types .MethodType (val , self )
1586+ return val
1587+ def __eq__ (self , other ):
1588+ return dict (self .items ()) == dict (other .items ())
1589+
1590+ annotations = annotationlib .call_annotate_function (
1591+ Annotate [int ],
1592+ Format .FORWARDREF ,
1593+ )
1594+
1595+ self .assertEqual (annotations , {"x" : int })
1596+
1597+ # We manually set the __orig_class__ for this special-case, check this too.
1598+ self .assertEqual (annotations .__orig_class__ , Annotate [int ])
1599+
15621600 def test_user_annotate_forwardref_value_fallback (self ):
15631601 # If Format.FORWARDREF and Format.VALUE_WITH_FAKE_GLOBALS are not supported
15641602 # use Format.VALUE
@@ -1808,39 +1846,6 @@ class Annotate:
18081846
18091847 self .assertEqual (annotations , {"x" : int })
18101848
1811- def test_callable_typing_generic_class_annotate_forwardref_value_fallback (self ):
1812- # Normally, generics are 'typing._GenericAlias' objects. These are implemented
1813- # in Python with a __call__ method (in _typing.BaseGenericAlias), so should work
1814- # as with any callable class instance.
1815- class Annotate [T ]:
1816- def __init__ (self , format , / , __Format = Format ,
1817- __NotImplementedError = NotImplementedError ):
1818- if format == __Format .VALUE_WITH_FAKE_GLOBALS :
1819- self .data = {"x" : int }
1820- else :
1821- raise __NotImplementedError (format )
1822- def __getitem__ (self , item ):
1823- return self .data [item ]
1824- def __iter__ (self ):
1825- return iter (self .data )
1826- def __len__ (self ):
1827- return len (self .data )
1828- def __getattr__ (self , attr ):
1829- val = getattr (collections .abc .Mapping , attr )
1830- if isinstance (val , types .FunctionType ):
1831- return types .MethodType (val , self )
1832- return val
1833- def __eq__ (self , other ):
1834- return dict (self .items ()) == dict (other .items ())
1835-
1836- annotations = annotationlib .call_annotate_function (
1837- Annotate [int ],
1838- Format .FORWARDREF ,
1839- )
1840-
1841- self .assertEqual (annotations , {"x" : int })
1842- self .assertEqual (annotations .__orig_class__ , Annotate [int ])
1843-
18441849 def test_callable_partial_annotate_forwardref_value_fallback (self ):
18451850 # functools.partial is implemented in C. Ensure that the annotate function
18461851 # is extracted and called correctly, particularly with Placeholder args.
0 commit comments