@@ -1877,6 +1877,32 @@ def test_name_lookup_without_eval(self):
18771877
18781878 self .assertEqual (exc .exception .name , "doesntexist" )
18791879
1880+ def test_evaluate_undefined_generic (self ):
1881+ # Test the codepath where have to eval() with undefined variables.
1882+ class C :
1883+ x : alias [int , undef ]
1884+
1885+ generic = get_annotations (C , format = Format .FORWARDREF )["x" ].evaluate (
1886+ format = Format .FORWARDREF ,
1887+ globals = {"alias" : dict }
1888+ )
1889+ self .assertNotIsInstance (generic , ForwardRef )
1890+ self .assertIs (generic .__origin__ , dict )
1891+ self .assertEqual (len (generic .__args__ ), 2 )
1892+ self .assertIs (generic .__args__ [0 ], int )
1893+ self .assertIsInstance (generic .__args__ [1 ], ForwardRef )
1894+
1895+ generic = get_annotations (C , format = Format .FORWARDREF )["x" ].evaluate (
1896+ format = Format .FORWARDREF ,
1897+ globals = {"alias" : Union },
1898+ locals = {"alias" : dict }
1899+ )
1900+ self .assertNotIsInstance (generic , ForwardRef )
1901+ self .assertIs (generic .__origin__ , dict )
1902+ self .assertEqual (len (generic .__args__ ), 2 )
1903+ self .assertIs (generic .__args__ [0 ], int )
1904+ self .assertIsInstance (generic .__args__ [1 ], ForwardRef )
1905+
18801906 def test_fwdref_invalid_syntax (self ):
18811907 fr = ForwardRef ("if" )
18821908 with self .assertRaises (SyntaxError ):
@@ -1885,6 +1911,15 @@ def test_fwdref_invalid_syntax(self):
18851911 with self .assertRaises (SyntaxError ):
18861912 fr .evaluate ()
18871913
1914+ def test_re_evaluate_generics (self ):
1915+ global alias
1916+ class C :
1917+ x : alias [int ]
1918+
1919+ evaluated = get_annotations (C , format = Format .FORWARDREF )["x" ].evaluate (format = Format .FORWARDREF )
1920+ alias = list
1921+ self .assertEqual (evaluated .evaluate (), list [int ])
1922+
18881923
18891924class TestAnnotationLib (unittest .TestCase ):
18901925 def test__all__ (self ):
0 commit comments