@@ -315,6 +315,8 @@ def test_repr(self):
315
315
self .assertEqual (repr (u ), 'typing.Union[typing.List[int], int]' )
316
316
u = Union [list [int ], dict [str , float ]]
317
317
self .assertEqual (repr (u ), 'typing.Union[list[int], dict[str, float]]' )
318
+ u = Union [int | float ]
319
+ self .assertEqual (repr (u ), 'typing.Union[int, float]' )
318
320
319
321
def test_cannot_subclass (self ):
320
322
with self .assertRaises (TypeError ):
@@ -1449,6 +1451,8 @@ def test_basics(self):
1449
1451
with self .assertRaises (TypeError ):
1450
1452
issubclass (SM1 , SimpleMapping )
1451
1453
self .assertIsInstance (SM1 (), SimpleMapping )
1454
+ T = TypeVar ("T" )
1455
+ self .assertEqual (List [list [T ] | float ].__parameters__ , (T ,))
1452
1456
1453
1457
def test_generic_errors (self ):
1454
1458
T = TypeVar ('T' )
@@ -1785,6 +1789,7 @@ def test_extended_generic_rules_repr(self):
1785
1789
def test_generic_forward_ref (self ):
1786
1790
def foobar (x : List [List ['CC' ]]): ...
1787
1791
def foobar2 (x : list [list [ForwardRef ('CC' )]]): ...
1792
+ def foobar3 (x : list [ForwardRef ('CC | int' )] | int ): ...
1788
1793
class CC : ...
1789
1794
self .assertEqual (
1790
1795
get_type_hints (foobar , globals (), locals ()),
@@ -1794,6 +1799,10 @@ class CC: ...
1794
1799
get_type_hints (foobar2 , globals (), locals ()),
1795
1800
{'x' : list [list [CC ]]}
1796
1801
)
1802
+ self .assertEqual (
1803
+ get_type_hints (foobar3 , globals (), locals ()),
1804
+ {'x' : list [CC | int ] | int }
1805
+ )
1797
1806
1798
1807
T = TypeVar ('T' )
1799
1808
AT = Tuple [T , ...]
@@ -2467,6 +2476,12 @@ def foo(a: Union['T']):
2467
2476
self .assertEqual (get_type_hints (foo , globals (), locals ()),
2468
2477
{'a' : Union [T ]})
2469
2478
2479
+ def foo (a : tuple [ForwardRef ('T' )] | int ):
2480
+ pass
2481
+
2482
+ self .assertEqual (get_type_hints (foo , globals (), locals ()),
2483
+ {'a' : tuple [T ] | int })
2484
+
2470
2485
def test_tuple_forward (self ):
2471
2486
2472
2487
def foo (a : Tuple ['T' ]):
@@ -2848,7 +2863,7 @@ def test_get_type_hints_from_various_objects(self):
2848
2863
gth (None )
2849
2864
2850
2865
def test_get_type_hints_modules (self ):
2851
- ann_module_type_hints = {1 : 2 , 'f' : Tuple [int , int ], 'x' : int , 'y' : str }
2866
+ ann_module_type_hints = {1 : 2 , 'f' : Tuple [int , int ], 'x' : int , 'y' : str , 'u' : int | float }
2852
2867
self .assertEqual (gth (ann_module ), ann_module_type_hints )
2853
2868
self .assertEqual (gth (ann_module2 ), {})
2854
2869
self .assertEqual (gth (ann_module3 ), {})
@@ -4390,6 +4405,9 @@ def test_no_paramspec_in__parameters__(self):
4390
4405
self .assertNotIn (P , list [P ].__parameters__ )
4391
4406
self .assertIn (T , tuple [T , P ].__parameters__ )
4392
4407
4408
+ self .assertNotIn (P , (list [P ] | int ).__parameters__ )
4409
+ self .assertIn (T , (tuple [T , P ] | int ).__parameters__ )
4410
+
4393
4411
def test_paramspec_in_nested_generics (self ):
4394
4412
# Although ParamSpec should not be found in __parameters__ of most
4395
4413
# generics, they probably should be found when nested in
@@ -4399,8 +4417,10 @@ def test_paramspec_in_nested_generics(self):
4399
4417
C1 = Callable [P , T ]
4400
4418
G1 = List [C1 ]
4401
4419
G2 = list [C1 ]
4420
+ G3 = list [C1 ] | int
4402
4421
self .assertEqual (G1 .__parameters__ , (P , T ))
4403
4422
self .assertEqual (G2 .__parameters__ , (P , T ))
4423
+ self .assertEqual (G3 .__parameters__ , (P , T ))
4404
4424
4405
4425
4406
4426
class ConcatenateTests (BaseTestCase ):
0 commit comments