@@ -1619,6 +1619,84 @@ def annotate(format, /):
16191619 # Some non-Format value
16201620 annotationlib .call_annotate_function (annotate , 7 )
16211621
1622+ def test_basic_non_function_annotate (self ):
1623+ class Annotate :
1624+ def __call__ (self , format , / , __Format = Format ,
1625+ __NotImplementedError = NotImplementedError ):
1626+ if format == __Format .VALUE :
1627+ return {'x' : str }
1628+ elif format == __Format .VALUE_WITH_FAKE_GLOBALS :
1629+ return {'x' : int }
1630+ elif format == __Format .STRING :
1631+ return {'x' : "float" }
1632+ else :
1633+ raise __NotImplementedError (format )
1634+
1635+ annotations = annotationlib .call_annotate_function (Annotate (), Format .VALUE )
1636+ self .assertEqual (annotations , {"x" : str })
1637+
1638+ annotations = annotationlib .call_annotate_function (Annotate (), Format .STRING )
1639+ self .assertEqual (annotations , {"x" : "float" })
1640+
1641+ with self .assertRaises (AttributeError ) as cm :
1642+ annotations = annotationlib .call_annotate_function (
1643+ Annotate (), Format .FORWARDREF
1644+ )
1645+
1646+ self .assertEqual (cm .exception .name , "__builtins__" )
1647+ self .assertIsInstance (cm .exception .obj , Annotate )
1648+
1649+ def test_full_non_function_annotate (self ):
1650+ def outer ():
1651+ local = str
1652+
1653+ class Annotate :
1654+ called_formats = []
1655+
1656+ def __call__ (self , format = None , * , _self = None ):
1657+ nonlocal local
1658+ if _self is not None :
1659+ self , format = _self , self
1660+
1661+ self .called_formats .append (format )
1662+ if format == 1 : # VALUE
1663+ return {"x" : MyClass , "y" : int , "z" : local }
1664+ if format == 2 : # VALUE_WITH_FAKE_GLOBALS
1665+ return {"w" : unknown , "x" : MyClass , "y" : int , "z" : local }
1666+ raise NotImplementedError
1667+
1668+ __globals__ = {"MyClass" : MyClass }
1669+ __builtins__ = {"int" : int }
1670+ __closure__ = (types .CellType (str ),)
1671+ __defaults__ = (None ,)
1672+
1673+ __kwdefaults__ = property (lambda self : dict (_self = self ))
1674+ __code__ = property (lambda self : self .__call__ .__code__ )
1675+
1676+ return Annotate ()
1677+
1678+ annotate = outer ()
1679+
1680+ self .assertEqual (
1681+ annotationlib .call_annotate_function (annotate , Format .VALUE ),
1682+ {"x" : MyClass , "y" : int , "z" : str }
1683+ )
1684+ self .assertEqual (annotate .called_formats [- 1 ], Format .VALUE )
1685+
1686+ self .assertEqual (
1687+ annotationlib .call_annotate_function (annotate , Format .STRING ),
1688+ {"w" : "unknown" , "x" : "MyClass" , "y" : "int" , "z" : "local" }
1689+ )
1690+ self .assertIn (Format .STRING , annotate .called_formats )
1691+ self .assertEqual (annotate .called_formats [- 1 ], Format .VALUE_WITH_FAKE_GLOBALS )
1692+
1693+ self .assertEqual (
1694+ annotationlib .call_annotate_function (annotate , Format .FORWARDREF ),
1695+ {"w" : support .EqualToForwardRef ("unknown" ), "x" : MyClass , "y" : int , "z" : str }
1696+ )
1697+ self .assertIn (Format .FORWARDREF , annotate .called_formats )
1698+ self .assertEqual (annotate .called_formats [- 1 ], Format .VALUE_WITH_FAKE_GLOBALS )
1699+
16221700 def test_error_from_value_raised (self ):
16231701 # Test that the error from format.VALUE is raised
16241702 # if all formats fail
0 commit comments