@@ -18,6 +18,11 @@ class Example:
18
18
19
19
class Forward : ...
20
20
21
+ def clear_typing_caches ():
22
+ for f in typing ._cleanups :
23
+ f ()
24
+
25
+
21
26
class TypesTests (unittest .TestCase ):
22
27
23
28
def test_truth_values (self ):
@@ -710,11 +715,34 @@ def test_or_type_operator_with_TypeVar(self):
710
715
self .assertIs ((TV | int )[int ], int )
711
716
712
717
def test_union_args (self ):
713
- self .assertEqual ((int | str ).__args__ , (int , str ))
714
- self .assertEqual (((int | str ) | list ).__args__ , (int , str , list ))
715
- self .assertEqual ((int | (str | list )).__args__ , (int , str , list ))
716
- self .assertEqual ((int | None ).__args__ , (int , type (None )))
717
- self .assertEqual ((int | type (None )).__args__ , (int , type (None )))
718
+ def check (arg , expected ):
719
+ clear_typing_caches ()
720
+ self .assertEqual (arg .__args__ , expected )
721
+
722
+ check (int | str , (int , str ))
723
+ check ((int | str ) | list , (int , str , list ))
724
+ check (int | (str | list ), (int , str , list ))
725
+ check ((int | str ) | int , (int , str ))
726
+ check (int | (str | int ), (int , str ))
727
+ check ((int | str ) | (str | int ), (int , str ))
728
+ check (typing .Union [int , str ] | list , (int , str , list ))
729
+ check (int | typing .Union [str , list ], (int , str , list ))
730
+ check ((int | str ) | (list | int ), (int , str , list ))
731
+ check ((int | str ) | typing .Union [list , int ], (int , str , list ))
732
+ check (typing .Union [int , str ] | (list | int ), (int , str , list ))
733
+ check ((str | int ) | (int | list ), (str , int , list ))
734
+ check ((str | int ) | typing .Union [int , list ], (str , int , list ))
735
+ check (typing .Union [str , int ] | (int | list ), (str , int , list ))
736
+ check (int | type (None ), (int , type (None )))
737
+ check (type (None ) | int , (type (None ), int ))
738
+
739
+ args = (int , list [int ], typing .List [int ],
740
+ typing .Tuple [int , int ], typing .Callable [[int ], int ],
741
+ typing .Hashable , typing .TypeVar ('T' ))
742
+ for x in args :
743
+ with self .subTest (x ):
744
+ check (x | None , (x , type (None )))
745
+ check (None | x , (type (None ), x ))
718
746
719
747
def test_union_parameter_chaining (self ):
720
748
T = typing .TypeVar ("T" )
0 commit comments