@@ -32,11 +32,6 @@ def wrapper(a, b):
3232 return wrapper
3333
3434
35- def assert_is_fwdref (case , obj , value ):
36- case .assertIsInstance (obj , annotationlib .ForwardRef )
37- case .assertEqual (obj .__forward_arg__ , value )
38-
39-
4035class MyClass :
4136 def __repr__ (self ):
4237 return "my repr"
@@ -64,7 +59,8 @@ def inner(arg: x):
6459
6560 anno = annotationlib .get_annotations (inner , format = Format .FORWARDREF )
6661 fwdref = anno ["arg" ]
67- assert_is_fwdref (self , fwdref , "x" )
62+ self .assertIsInstance (fwdref , annotationlib .ForwardRef )
63+ self .assertEqual (fwdref .__forward_arg__ , "x" )
6864 with self .assertRaises (NameError ):
6965 fwdref .evaluate ()
7066
@@ -81,7 +77,8 @@ def f(x: int, y: doesntexist):
8177 anno = annotationlib .get_annotations (f , format = Format .FORWARDREF )
8278 self .assertIs (anno ["x" ], int )
8379 fwdref = anno ["y" ]
84- assert_is_fwdref (self , fwdref , "doesntexist" )
80+ self .assertIsInstance (fwdref , annotationlib .ForwardRef )
81+ self .assertEqual (fwdref .__forward_arg__ , "doesntexist" )
8582 with self .assertRaises (NameError ):
8683 fwdref .evaluate ()
8784 self .assertEqual (fwdref .evaluate (globals = {"doesntexist" : 1 }), 1 )
@@ -99,22 +96,28 @@ def f(
9996
10097 anno = annotationlib .get_annotations (f , format = Format .FORWARDREF )
10198 x_anno = anno ["x" ]
102- assert_is_fwdref (self , x_anno , "some.module" )
99+ self .assertIsInstance (x_anno , ForwardRef )
100+ self .assertEqual (x_anno , support .EqualToForwardRef ("some.module" , owner = f ))
103101
104102 y_anno = anno ["y" ]
105- assert_is_fwdref (self , y_anno , "some[module]" )
103+ self .assertIsInstance (y_anno , ForwardRef )
104+ self .assertEqual (y_anno , support .EqualToForwardRef ("some[module]" , owner = f ))
106105
107106 z_anno = anno ["z" ]
108- assert_is_fwdref (self , z_anno , "some(module)" )
107+ self .assertIsInstance (z_anno , ForwardRef )
108+ self .assertEqual (z_anno , support .EqualToForwardRef ("some(module)" , owner = f ))
109109
110110 alpha_anno = anno ["alpha" ]
111- assert_is_fwdref (self , alpha_anno , "some | obj" )
111+ self .assertIsInstance (alpha_anno , ForwardRef )
112+ self .assertEqual (alpha_anno , support .EqualToForwardRef ("some | obj" , owner = f ))
112113
113114 beta_anno = anno ["beta" ]
114- assert_is_fwdref (self , beta_anno , "+some" )
115+ self .assertIsInstance (beta_anno , ForwardRef )
116+ self .assertEqual (beta_anno , support .EqualToForwardRef ("+some" , owner = f ))
115117
116118 gamma_anno = anno ["gamma" ]
117- assert_is_fwdref (self , gamma_anno , "some < obj" )
119+ self .assertIsInstance (gamma_anno , ForwardRef )
120+ self .assertEqual (gamma_anno , support .EqualToForwardRef ("some < obj" , owner = f ))
118121
119122
120123class TestSourceFormat (unittest .TestCase ):
@@ -359,6 +362,14 @@ def test_fwdref_to_builtin(self):
359362 obj = object ()
360363 self .assertIs (ForwardRef ("int" ).evaluate (globals = {"int" : obj }), obj )
361364
365+ def test_fwdref_value_is_not_cached (self ):
366+ fr = ForwardRef ("hello" )
367+ with self .assertRaises (NameError ):
368+ fr .evaluate ()
369+ self .assertIs (fr .evaluate (globals = {"hello" : str }), str )
370+ with self .assertRaises (NameError ):
371+ fr .evaluate ()
372+
362373 def test_fwdref_with_owner (self ):
363374 self .assertEqual (
364375 ForwardRef ("Counter[int]" , owner = collections ).evaluate (),
@@ -447,10 +458,12 @@ def f2(a: undefined):
447458 )
448459 self .assertEqual (annotationlib .get_annotations (f1 , format = 1 ), {"a" : int })
449460
450- for fmt in (Format .FORWARDREF , 3 ):
451- annos = annotationlib .get_annotations (f2 , format = fmt )
452- self .assertEqual (list (annos ), ["a" ])
453- assert_is_fwdref (self , annos ["a" ], "undefined" )
461+ fwd = support .EqualToForwardRef ("undefined" , owner = f2 )
462+ self .assertEqual (
463+ annotationlib .get_annotations (f2 , format = Format .FORWARDREF ),
464+ {"a" : fwd },
465+ )
466+ self .assertEqual (annotationlib .get_annotations (f2 , format = 3 ), {"a" : fwd })
454467
455468 self .assertEqual (
456469 annotationlib .get_annotations (f1 , format = Format .STRING ),
@@ -1000,10 +1013,10 @@ def evaluate(format, exc=NotImplementedError):
10001013
10011014 with self .assertRaises (NameError ):
10021015 annotationlib .call_evaluate_function (evaluate , Format .VALUE )
1003-
1004- fwdref = annotationlib .call_evaluate_function (evaluate , Format .FORWARDREF )
1005- assert_is_fwdref ( self , fwdref , "undefined" )
1006-
1016+ self . assertEqual (
1017+ annotationlib .call_evaluate_function (evaluate , Format .FORWARDREF ),
1018+ support . EqualToForwardRef ( "undefined" ),
1019+ )
10071020 self .assertEqual (
10081021 annotationlib .call_evaluate_function (evaluate , Format .STRING ),
10091022 "undefined" ,
0 commit comments