@@ -18,6 +18,11 @@ class Example:
1818
1919class Forward : ...
2020
21+ def clear_typing_caches ():
22+ for f in typing ._cleanups :
23+ f ()
24+
25+
2126class TypesTests (unittest .TestCase ):
2227
2328 def test_truth_values (self ):
@@ -708,11 +713,34 @@ def test_or_type_operator_with_TypeVar(self):
708713 assert str | TV == typing .Union [str , TV ]
709714
710715 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 ))
716744
717745 def test_or_type_operator_with_forward (self ):
718746 T = typing .TypeVar ('T' )
0 commit comments