@@ -612,6 +612,8 @@ def test_or_types_operator(self):
612612 self .assertEqual (str | int , typing .Union [int , str ])
613613 self .assertEqual (int | None , typing .Union [int , None ])
614614 self .assertEqual (None | int , typing .Union [int , None ])
615+ self .assertEqual (int | type (None ), int | None )
616+ self .assertEqual (type (None ) | int , None | int )
615617 self .assertEqual (int | str | list , typing .Union [int , str , list ])
616618 self .assertEqual (int | (str | list ), typing .Union [int , str , list ])
617619 self .assertEqual (str | (int | list ), typing .Union [int , str , list ])
@@ -699,6 +701,13 @@ def test_or_type_operator_with_TypeVar(self):
699701 assert TV | str == typing .Union [TV , str ]
700702 assert str | TV == typing .Union [str , TV ]
701703
704+ def test_union_args (self ):
705+ self .assertEqual ((int | str ).__args__ , (int , str ))
706+ self .assertEqual (((int | str ) | list ).__args__ , (int , str , list ))
707+ self .assertEqual ((int | (str | list )).__args__ , (int , str , list ))
708+ self .assertEqual ((int | None ).__args__ , (int , type (None )))
709+ self .assertEqual ((int | type (None )).__args__ , (int , type (None )))
710+
702711 def test_union_parameter_chaining (self ):
703712 T = typing .TypeVar ("T" )
704713 S = typing .TypeVar ("S" )
@@ -754,8 +763,13 @@ def test_or_type_operator_with_SpecialForm(self):
754763 assert typing .Union [int , bool ] | str == typing .Union [int , bool , str ]
755764
756765 def test_or_type_repr (self ):
766+ assert repr (int | str ) == "int | str"
767+ assert repr ((int | str ) | list ) == "int | str | list"
768+ assert repr (int | (str | list )) == "int | str | list"
757769 assert repr (int | None ) == "int | None"
770+ assert repr (int | type (None )) == "int | None"
758771 assert repr (int | typing .GenericAlias (list , int )) == "int | list[int]"
772+ assert repr (int | typing .TypeVar ('T' )) == "int | ~T"
759773
760774 def test_or_type_operator_with_genericalias (self ):
761775 a = list [int ]
@@ -792,13 +806,18 @@ def __eq__(self, other):
792806 issubclass (int , type_ )
793807
794808 def test_or_type_operator_with_bad_module (self ):
795- class TypeVar :
809+ class BadMeta (type ):
810+ __qualname__ = 'TypeVar'
796811 @property
797812 def __module__ (self ):
798813 1 / 0
814+ TypeVar = BadMeta ('TypeVar' , (), {})
815+ _SpecialForm = BadMeta ('_SpecialForm' , (), {})
799816 # Crashes in Issue44483
800817 with self .assertRaises (ZeroDivisionError ):
801818 str | TypeVar ()
819+ with self .assertRaises (ZeroDivisionError ):
820+ str | _SpecialForm ()
802821
803822 @cpython_only
804823 def test_or_type_operator_reference_cycle (self ):
0 commit comments