@@ -4531,20 +4531,30 @@ def f(x: X): ...
45314531 {'x' : list [list [ForwardRef ('X' )]]}
45324532 )
45334533
4534- def test_pep695_generic_with_future_annotations (self ):
4534+ def test_pep695_generic_class_with_future_annotations (self ):
4535+ original_globals = dict (ann_module695 .__dict__ )
4536+
45354537 hints_for_A = get_type_hints (ann_module695 .A )
45364538 A_type_params = ann_module695 .A .__type_params__
45374539 self .assertIs (hints_for_A ["x" ], A_type_params [0 ])
45384540 self .assertEqual (hints_for_A ["y" ].__args__ [0 ], Unpack [A_type_params [1 ]])
45394541 self .assertIs (hints_for_A ["z" ].__args__ [0 ], A_type_params [2 ])
45404542
4543+ # should not have changed as a result of the get_type_hints() calls!
4544+ self .assertEqual (ann_module695 .__dict__ , original_globals )
4545+
4546+ def test_pep695_generic_class_with_future_annotations_and_local_shadowing (self ):
45414547 hints_for_B = get_type_hints (ann_module695 .B )
4542- self .assertEqual (hints_for_B .keys (), {"x" , "y" , "z" })
4548+ self .assertEqual (hints_for_B , {"x" : int , "y" : str , "z" : bytes })
4549+
4550+ def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars (self ):
4551+ hints_for_C = get_type_hints (ann_module695 .C )
45434552 self .assertEqual (
4544- set (hints_for_B .values ()) ^ set ( ann_module695 . B . __type_params__ ),
4545- set ()
4553+ set (hints_for_C .values ()),
4554+ set (ann_module695 . C . __type_params__ )
45464555 )
45474556
4557+ def test_pep_695_generic_function_with_future_annotations (self ):
45484558 hints_for_generic_function = get_type_hints (ann_module695 .generic_function )
45494559 func_t_params = ann_module695 .generic_function .__type_params__
45504560 self .assertEqual (
@@ -4555,6 +4565,54 @@ def test_pep695_generic_with_future_annotations(self):
45554565 self .assertIs (hints_for_generic_function ["z" ].__origin__ , func_t_params [2 ])
45564566 self .assertIs (hints_for_generic_function ["zz" ].__origin__ , func_t_params [2 ])
45574567
4568+ def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars (self ):
4569+ self .assertEqual (
4570+ set (get_type_hints (ann_module695 .generic_function_2 ).values ()),
4571+ set (ann_module695 .generic_function_2 .__type_params__ )
4572+ )
4573+
4574+ def test_pep_695_generic_method_with_future_annotations (self ):
4575+ hints_for_generic_method = get_type_hints (ann_module695 .D .generic_method )
4576+ params = {
4577+ param .__name__ : param
4578+ for param in ann_module695 .D .generic_method .__type_params__
4579+ }
4580+ self .assertEqual (
4581+ hints_for_generic_method ,
4582+ {"x" : params ["Foo" ], "y" : params ["Bar" ], "return" : types .NoneType }
4583+ )
4584+
4585+ def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars (self ):
4586+ self .assertEqual (
4587+ set (get_type_hints (ann_module695 .D .generic_method_2 ).values ()),
4588+ set (ann_module695 .D .generic_method_2 .__type_params__ )
4589+ )
4590+
4591+ def test_pep_695_generics_with_future_annotations_nested_in_function (self ):
4592+ results = ann_module695 .nested ()
4593+
4594+ self .assertEqual (
4595+ set (results .hints_for_E .values ()),
4596+ set (results .E .__type_params__ )
4597+ )
4598+ self .assertEqual (
4599+ set (results .hints_for_E_meth .values ()),
4600+ set (results .E .generic_method .__type_params__ )
4601+ )
4602+ self .assertNotEqual (
4603+ set (results .hints_for_E_meth .values ()),
4604+ set (results .E .__type_params__ )
4605+ )
4606+ self .assertEqual (
4607+ set (results .hints_for_E_meth .values ()).intersection (results .E .__type_params__ ),
4608+ set ()
4609+ )
4610+
4611+ self .assertEqual (
4612+ set (results .hints_for_generic_func .values ()),
4613+ set (results .generic_func .__type_params__ )
4614+ )
4615+
45584616 def test_extended_generic_rules_subclassing (self ):
45594617 class T1 (Tuple [T , KT ]): ...
45604618 class T2 (Tuple [T , ...]): ...
0 commit comments