@@ -4531,20 +4531,30 @@ def f(x: X): ...
4531
4531
{'x' : list [list [ForwardRef ('X' )]]}
4532
4532
)
4533
4533
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
+
4535
4537
hints_for_A = get_type_hints (ann_module695 .A )
4536
4538
A_type_params = ann_module695 .A .__type_params__
4537
4539
self .assertIs (hints_for_A ["x" ], A_type_params [0 ])
4538
4540
self .assertEqual (hints_for_A ["y" ].__args__ [0 ], Unpack [A_type_params [1 ]])
4539
4541
self .assertIs (hints_for_A ["z" ].__args__ [0 ], A_type_params [2 ])
4540
4542
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 ):
4541
4547
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 )
4543
4552
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__ )
4546
4555
)
4547
4556
4557
+ def test_pep_695_generic_function_with_future_annotations (self ):
4548
4558
hints_for_generic_function = get_type_hints (ann_module695 .generic_function )
4549
4559
func_t_params = ann_module695 .generic_function .__type_params__
4550
4560
self .assertEqual (
@@ -4555,6 +4565,54 @@ def test_pep695_generic_with_future_annotations(self):
4555
4565
self .assertIs (hints_for_generic_function ["z" ].__origin__ , func_t_params [2 ])
4556
4566
self .assertIs (hints_for_generic_function ["zz" ].__origin__ , func_t_params [2 ])
4557
4567
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
+
4558
4616
def test_extended_generic_rules_subclassing (self ):
4559
4617
class T1 (Tuple [T , KT ]): ...
4560
4618
class T2 (Tuple [T , ...]): ...
0 commit comments