@@ -8944,7 +8944,120 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self):
89448944 set (results .generic_func .__type_params__ )
89458945 )
89468946
8947- class TestEvaluateForwardRefs (BaseTestCase ):
8947+
8948+ class EvaluateForwardRefTests (BaseTestCase ):
8949+ def test_evaluate_forward_ref (self ):
8950+ int_ref = typing_extensions .ForwardRef ('int' )
8951+ self .assertIs (typing_extensions .evaluate_forward_ref (int_ref ), int )
8952+ self .assertIs (
8953+ typing_extensions .evaluate_forward_ref (int_ref , type_params = ()),
8954+ int ,
8955+ )
8956+ self .assertIs (
8957+ typing_extensions .evaluate_forward_ref (int_ref , format = typing_extensions .Format .VALUE ),
8958+ int ,
8959+ )
8960+ self .assertIs (
8961+ typing_extensions .evaluate_forward_ref (
8962+ int_ref , format = typing_extensions .Format .FORWARDREF ,
8963+ ),
8964+ int ,
8965+ )
8966+ self .assertEqual (
8967+ typing_extensions .evaluate_forward_ref (
8968+ int_ref , format = typing_extensions .Format .STRING ,
8969+ ),
8970+ 'int' ,
8971+ )
8972+
8973+ def test_evaluate_forward_ref_undefined (self ):
8974+ missing = typing_extensions .ForwardRef ('missing' )
8975+ with self .assertRaises (NameError ):
8976+ typing_extensions .evaluate_forward_ref (missing )
8977+ self .assertIs (
8978+ typing_extensions .evaluate_forward_ref (
8979+ missing , format = typing_extensions .Format .FORWARDREF ,
8980+ ),
8981+ missing ,
8982+ )
8983+ self .assertEqual (
8984+ typing_extensions .evaluate_forward_ref (
8985+ missing , format = typing_extensions .Format .STRING ,
8986+ ),
8987+ "missing" ,
8988+ )
8989+
8990+ def test_evaluate_forward_ref_nested (self ):
8991+ ref = typing_extensions .ForwardRef ("int | list['str']" )
8992+ self .assertEqual (
8993+ typing_extensions .evaluate_forward_ref (ref ),
8994+ int | list [str ],
8995+ )
8996+ self .assertEqual (
8997+ typing_extensions .evaluate_forward_ref (ref , format = typing_extensions .Format .FORWARDREF ),
8998+ int | list [str ],
8999+ )
9000+ self .assertEqual (
9001+ typing_extensions .evaluate_forward_ref (ref , format = typing_extensions .Format .STRING ),
9002+ "int | list['str']" ,
9003+ )
9004+
9005+ why = typing_extensions .ForwardRef ('"\' str\' "' )
9006+ self .assertIs (typing_extensions .evaluate_forward_ref (why ), str )
9007+
9008+ def test_evaluate_forward_ref_none (self ):
9009+ none_ref = typing_extensions .ForwardRef ('None' )
9010+ self .assertIs (typing_extensions .evaluate_forward_ref (none_ref ), None )
9011+
9012+ def test_globals (self ):
9013+ A = "str"
9014+ ref = typing_extensions .ForwardRef ('list[A]' )
9015+ with self .assertRaises (NameError ):
9016+ typing_extensions .evaluate_forward_ref (ref )
9017+ self .assertEqual (
9018+ typing_extensions .evaluate_forward_ref (ref , globals = {'A' : A }),
9019+ list [str ],
9020+ )
9021+
9022+ def test_owner (self ):
9023+ ref = typing_extensions .ForwardRef ("A" )
9024+
9025+ with self .assertRaises (NameError ):
9026+ typing_extensions .evaluate_forward_ref (ref )
9027+
9028+ # We default to the globals of `owner`,
9029+ # so it no longer raises `NameError`
9030+ self .assertIs (
9031+ typing_extensions .evaluate_forward_ref (ref , owner = Loop ), A
9032+ )
9033+
9034+ @skipUnless (sys .version_info >= (3 , 14 ), "Not yet implemented in Python < 3.14" )
9035+ def test_inherited_owner (self ):
9036+ # owner passed to evaluate_forward_ref
9037+ ref = typing_extensions .ForwardRef ("list['A']" )
9038+ self .assertEqual (
9039+ typing_extensions .evaluate_forward_ref (ref , owner = Loop ),
9040+ list [A ],
9041+ )
9042+
9043+ # owner set on the ForwardRef
9044+ ref = typing_extensions .ForwardRef ("list['A']" , owner = Loop )
9045+ self .assertEqual (
9046+ typing_extensions .evaluate_forward_ref (ref ),
9047+ list [A ],
9048+ )
9049+
9050+ @skipUnless (sys .version_info >= (3 , 14 ), "Not yet implemented in Python < 3.14" )
9051+ def test_partial_evaluation (self ):
9052+ ref = typing_extensions .ForwardRef ("list[A]" )
9053+ with self .assertRaises (NameError ):
9054+ typing_extensions .evaluate_forward_ref (ref )
9055+
9056+ self .assertEqual (
9057+ typing_extensions .evaluate_forward_ref (ref , format = typing_extensions .Format .FORWARDREF ),
9058+ list [EqualToForwardRef ('A' )],
9059+ )
9060+
89489061 def test_global_constant (self ):
89499062 if sys .version_info [:3 ] > (3 , 10 , 0 ):
89509063 self .assertTrue (_FORWARD_REF_HAS_CLASS )
@@ -9107,30 +9220,17 @@ class Y(Generic[Tx]):
91079220 self .assertEqual (get_args (evaluated_ref3 ), (Z [str ],))
91089221
91099222 def test_invalid_special_forms (self ):
9110- # tests _lax_type_check to raise errors the same way as the typing module.
9111- # Regex capture "< class 'module.name'> and "module.name"
9112- with self .assertRaisesRegex (
9113- TypeError , r"Plain .*Protocol('>)? is not valid as type argument"
9114- ):
9115- evaluate_forward_ref (typing .ForwardRef ("Protocol" ), globals = vars (typing ))
9116- with self .assertRaisesRegex (
9117- TypeError , r"Plain .*Generic('>)? is not valid as type argument"
9118- ):
9119- evaluate_forward_ref (typing .ForwardRef ("Generic" ), globals = vars (typing ))
9120- with self .assertRaisesRegex (TypeError , r"Plain typing(_extensions)?\.Final is not valid as type argument" ):
9121- evaluate_forward_ref (typing .ForwardRef ("Final" ), globals = vars (typing ))
9122- with self .assertRaisesRegex (TypeError , r"Plain typing(_extensions)?\.ClassVar is not valid as type argument" ):
9123- evaluate_forward_ref (typing .ForwardRef ("ClassVar" ), globals = vars (typing ))
9223+ for name in ("Protocol" , "Final" , "ClassVar" , "Generic" ):
9224+ with self .subTest (name = name ):
9225+ self .assertIs (
9226+ evaluate_forward_ref (typing .ForwardRef (name ), globals = vars (typing )),
9227+ getattr (typing , name ),
9228+ )
91249229 if _FORWARD_REF_HAS_CLASS :
91259230 self .assertIs (evaluate_forward_ref (typing .ForwardRef ("Final" , is_class = True ), globals = vars (typing )), Final )
91269231 self .assertIs (evaluate_forward_ref (typing .ForwardRef ("ClassVar" , is_class = True ), globals = vars (typing )), ClassVar )
9127- with self .assertRaisesRegex (TypeError , r"Plain typing(_extensions)?\.Final is not valid as type argument" ):
9128- evaluate_forward_ref (typing .ForwardRef ("Final" , is_argument = False ), globals = vars (typing ))
9129- with self .assertRaisesRegex (TypeError , r"Plain typing(_extensions)?\.ClassVar is not valid as type argument" ):
9130- evaluate_forward_ref (typing .ForwardRef ("ClassVar" , is_argument = False ), globals = vars (typing ))
9131- else :
9132- self .assertIs (evaluate_forward_ref (typing .ForwardRef ("Final" , is_argument = False ), globals = vars (typing )), Final )
9133- self .assertIs (evaluate_forward_ref (typing .ForwardRef ("ClassVar" , is_argument = False ), globals = vars (typing )), ClassVar )
9232+ self .assertIs (evaluate_forward_ref (typing .ForwardRef ("Final" , is_argument = False ), globals = vars (typing )), Final )
9233+ self .assertIs (evaluate_forward_ref (typing .ForwardRef ("ClassVar" , is_argument = False ), globals = vars (typing )), ClassVar )
91349234
91359235
91369236class TestSentinels (BaseTestCase ):
0 commit comments