@@ -4858,20 +4858,30 @@ def f(x: X): ...
48584858 {'x' : list [list [ForwardRef ('X' )]]}
48594859 )
48604860
4861- def test_pep695_generic_with_future_annotations (self ):
4861+ def test_pep695_generic_class_with_future_annotations (self ):
4862+ original_globals = dict (ann_module695 .__dict__ )
4863+
48624864 hints_for_A = get_type_hints (ann_module695 .A )
48634865 A_type_params = ann_module695 .A .__type_params__
48644866 self .assertIs (hints_for_A ["x" ], A_type_params [0 ])
48654867 self .assertEqual (hints_for_A ["y" ].__args__ [0 ], Unpack [A_type_params [1 ]])
48664868 self .assertIs (hints_for_A ["z" ].__args__ [0 ], A_type_params [2 ])
48674869
4870+ # should not have changed as a result of the get_type_hints() calls!
4871+ self .assertEqual (ann_module695 .__dict__ , original_globals )
4872+
4873+ def test_pep695_generic_class_with_future_annotations_and_local_shadowing (self ):
48684874 hints_for_B = get_type_hints (ann_module695 .B )
4869- self .assertEqual (hints_for_B .keys (), {"x" , "y" , "z" })
4875+ self .assertEqual (hints_for_B , {"x" : int , "y" : str , "z" : bytes })
4876+
4877+ def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars (self ):
4878+ hints_for_C = get_type_hints (ann_module695 .C )
48704879 self .assertEqual (
4871- set (hints_for_B .values ()) ^ set ( ann_module695 . B . __type_params__ ),
4872- set ()
4880+ set (hints_for_C .values ()),
4881+ set (ann_module695 . C . __type_params__ )
48734882 )
48744883
4884+ def test_pep_695_generic_function_with_future_annotations (self ):
48754885 hints_for_generic_function = get_type_hints (ann_module695 .generic_function )
48764886 func_t_params = ann_module695 .generic_function .__type_params__
48774887 self .assertEqual (
@@ -4882,6 +4892,54 @@ def test_pep695_generic_with_future_annotations(self):
48824892 self .assertIs (hints_for_generic_function ["z" ].__origin__ , func_t_params [2 ])
48834893 self .assertIs (hints_for_generic_function ["zz" ].__origin__ , func_t_params [2 ])
48844894
4895+ def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars (self ):
4896+ self .assertEqual (
4897+ set (get_type_hints (ann_module695 .generic_function_2 ).values ()),
4898+ set (ann_module695 .generic_function_2 .__type_params__ )
4899+ )
4900+
4901+ def test_pep_695_generic_method_with_future_annotations (self ):
4902+ hints_for_generic_method = get_type_hints (ann_module695 .D .generic_method )
4903+ params = {
4904+ param .__name__ : param
4905+ for param in ann_module695 .D .generic_method .__type_params__
4906+ }
4907+ self .assertEqual (
4908+ hints_for_generic_method ,
4909+ {"x" : params ["Foo" ], "y" : params ["Bar" ], "return" : types .NoneType }
4910+ )
4911+
4912+ def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars (self ):
4913+ self .assertEqual (
4914+ set (get_type_hints (ann_module695 .D .generic_method_2 ).values ()),
4915+ set (ann_module695 .D .generic_method_2 .__type_params__ )
4916+ )
4917+
4918+ def test_pep_695_generics_with_future_annotations_nested_in_function (self ):
4919+ results = ann_module695 .nested ()
4920+
4921+ self .assertEqual (
4922+ set (results .hints_for_E .values ()),
4923+ set (results .E .__type_params__ )
4924+ )
4925+ self .assertEqual (
4926+ set (results .hints_for_E_meth .values ()),
4927+ set (results .E .generic_method .__type_params__ )
4928+ )
4929+ self .assertNotEqual (
4930+ set (results .hints_for_E_meth .values ()),
4931+ set (results .E .__type_params__ )
4932+ )
4933+ self .assertEqual (
4934+ set (results .hints_for_E_meth .values ()).intersection (results .E .__type_params__ ),
4935+ set ()
4936+ )
4937+
4938+ self .assertEqual (
4939+ set (results .hints_for_generic_func .values ()),
4940+ set (results .generic_func .__type_params__ )
4941+ )
4942+
48854943 def test_extended_generic_rules_subclassing (self ):
48864944 class T1 (Tuple [T , KT ]): ...
48874945 class T2 (Tuple [T , ...]): ...
0 commit comments