@@ -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 ):
@@ -708,11 +713,34 @@ def test_or_type_operator_with_TypeVar(self):
708
713
assert str | TV == typing .Union [str , TV ]
709
714
710
715
def test_union_args (self ):
711
- self .assertEqual ((int | str ).__args__ , (int , str ))
712
- self .assertEqual (((int | str ) | list ).__args__ , (int , str , list ))
713
- self .assertEqual ((int | (str | list )).__args__ , (int , str , list ))
714
- self .assertEqual ((int | None ).__args__ , (int , type (None )))
715
- self .assertEqual ((int | type (None )).__args__ , (int , type (None )))
716
+ def check (arg , expected ):
717
+ clear_typing_caches ()
718
+ self .assertEqual (arg .__args__ , expected )
719
+
720
+ check (int | str , (int , str ))
721
+ check ((int | str ) | list , (int , str , list ))
722
+ check (int | (str | list ), (int , str , list ))
723
+ check ((int | str ) | int , (int , str ))
724
+ check (int | (str | int ), (int , str ))
725
+ check ((int | str ) | (str | int ), (int , str ))
726
+ check (typing .Union [int , str ] | list , (int , str , list ))
727
+ check (int | typing .Union [str , list ], (int , str , list ))
728
+ check ((int | str ) | (list | int ), (int , str , list ))
729
+ check ((int | str ) | typing .Union [list , int ], (int , str , list ))
730
+ check (typing .Union [int , str ] | (list | int ), (int , str , list ))
731
+ check ((str | int ) | (int | list ), (str , int , list ))
732
+ check ((str | int ) | typing .Union [int , list ], (str , int , list ))
733
+ check (typing .Union [str , int ] | (int | list ), (str , int , list ))
734
+ check (int | type (None ), (int , type (None )))
735
+ check (type (None ) | int , (type (None ), int ))
736
+
737
+ args = (int , list [int ], typing .List [int ],
738
+ typing .Tuple [int , int ], typing .Callable [[int ], int ],
739
+ typing .Hashable , typing .TypeVar ('T' ))
740
+ for x in args :
741
+ with self .subTest (x ):
742
+ check (x | None , (x , type (None )))
743
+ check (None | x , (type (None ), x ))
716
744
717
745
def test_or_type_operator_with_forward (self ):
718
746
T = typing .TypeVar ('T' )
0 commit comments