@@ -254,6 +254,11 @@ def test_repr(self):
254
254
self .assertEqual (repr (u ), 'typing.Union[%s.Employee, int]' % __name__ )
255
255
u = Union [int , Employee ]
256
256
self .assertEqual (repr (u ), 'typing.Union[int, %s.Employee]' % __name__ )
257
+ T = TypeVar ('T' )
258
+ u = Union [T , int ][int ]
259
+ self .assertEqual (repr (u ), repr (int ))
260
+ u = Union [List [int ], int ]
261
+ self .assertEqual (repr (u ), 'typing.Union[typing.List[int], int]' )
257
262
258
263
def test_cannot_subclass (self ):
259
264
with self .assertRaises (TypeError ):
@@ -304,6 +309,15 @@ def test_union_instance_type_error(self):
304
309
with self .assertRaises (TypeError ):
305
310
isinstance (42 , Union [int , str ])
306
311
312
+ def test_no_eval_union (self ):
313
+ u = Union [int , str ]
314
+ def f (x : u ): ...
315
+ self .assertIs (get_type_hints (f )['x' ], u )
316
+
317
+ def test_function_repr_union (self ):
318
+ def fun () -> int : ...
319
+ self .assertEqual (repr (Union [fun , int ]), 'typing.Union[fun, int]' )
320
+
307
321
def test_union_str_pattern (self ):
308
322
# Shouldn't crash; see http://bugs.python.org/issue25390
309
323
A = Union [str , Pattern ]
@@ -874,6 +888,8 @@ def test_fail_with_bare_generic(self):
874
888
Tuple [Generic [T ]]
875
889
with self .assertRaises (TypeError ):
876
890
List [typing ._Protocol ]
891
+ with self .assertRaises (TypeError ):
892
+ isinstance (1 , Generic )
877
893
878
894
def test_type_erasure_special (self ):
879
895
T = TypeVar ('T' )
@@ -1242,6 +1258,19 @@ def test_forwardref_instance_type_error(self):
1242
1258
with self .assertRaises (TypeError ):
1243
1259
isinstance (42 , fr )
1244
1260
1261
+ def test_forwardref_subclass_type_error (self ):
1262
+ fr = typing ._ForwardRef ('int' )
1263
+ with self .assertRaises (TypeError ):
1264
+ issubclass (int , fr )
1265
+
1266
+ def test_forward_equality (self ):
1267
+ fr = typing ._ForwardRef ('int' )
1268
+ self .assertEqual (fr , typing ._ForwardRef ('int' ))
1269
+ self .assertNotEqual (List ['int' ], List [int ])
1270
+
1271
+ def test_forward_repr (self ):
1272
+ self .assertEqual (repr (List ['int' ]), "typing.List[_ForwardRef('int')]" )
1273
+
1245
1274
def test_union_forward (self ):
1246
1275
1247
1276
def foo (a : Union ['T' ]):
@@ -1440,11 +1469,16 @@ class A:
1440
1469
class B(A):
1441
1470
x: ClassVar[Optional['B']] = None
1442
1471
y: int
1472
+ b: int
1443
1473
class CSub(B):
1444
1474
z: ClassVar['CSub'] = B()
1445
1475
class G(Generic[T]):
1446
1476
lst: ClassVar[List[T]] = []
1447
1477
1478
+ class NoneAndForward:
1479
+ parent: 'NoneAndForward'
1480
+ meaning: None
1481
+
1448
1482
class CoolEmployee(NamedTuple):
1449
1483
name: str
1450
1484
cool: int
@@ -1465,6 +1499,7 @@ def double(self):
1465
1499
# fake names for the sake of static analysis
1466
1500
ann_module = ann_module2 = ann_module3 = None
1467
1501
A = B = CSub = G = CoolEmployee = CoolEmployeeWithDefault = XMeth = object
1502
+ NoneAndForward = object
1468
1503
1469
1504
gth = get_type_hints
1470
1505
@@ -1499,6 +1534,8 @@ def test_get_type_hints_classes(self):
1499
1534
{'y' : Optional [ann_module .C ]})
1500
1535
self .assertEqual (gth (ann_module .S ), {'x' : str , 'y' : str })
1501
1536
self .assertEqual (gth (ann_module .foo ), {'x' : int })
1537
+ self .assertEqual (gth (NoneAndForward , globals ()),
1538
+ {'parent' : NoneAndForward , 'meaning' : type (None )})
1502
1539
1503
1540
@skipUnless (PY36 , 'Python 3.6 required' )
1504
1541
def test_respect_no_type_check (self ):
@@ -1539,9 +1576,10 @@ def test_get_type_hints_ClassVar(self):
1539
1576
self .assertEqual (gth (ann_module2 .CV , ann_module2 .__dict__ ),
1540
1577
{'var' : typing .ClassVar [ann_module2 .CV ]})
1541
1578
self .assertEqual (gth (B , globals ()),
1542
- {'y' : int , 'x' : ClassVar [Optional [B ]]})
1579
+ {'y' : int , 'x' : ClassVar [Optional [B ]], 'b' : int })
1543
1580
self .assertEqual (gth (CSub , globals ()),
1544
- {'z' : ClassVar [CSub ], 'y' : int , 'x' : ClassVar [Optional [B ]]})
1581
+ {'z' : ClassVar [CSub ], 'y' : int , 'b' : int ,
1582
+ 'x' : ClassVar [Optional [B ]]})
1545
1583
self .assertEqual (gth (G ), {'lst' : ClassVar [List [T ]]})
1546
1584
1547
1585
@@ -2177,6 +2215,12 @@ def test_basics(self):
2177
2215
Pattern [Union [str , bytes ]]
2178
2216
Match [Union [bytes , str ]]
2179
2217
2218
+ def test_alias_equality (self ):
2219
+ self .assertEqual (Pattern [str ], Pattern [str ])
2220
+ self .assertNotEqual (Pattern [str ], Pattern [bytes ])
2221
+ self .assertNotEqual (Pattern [str ], Match [str ])
2222
+ self .assertNotEqual (Pattern [str ], str )
2223
+
2180
2224
def test_errors (self ):
2181
2225
with self .assertRaises (TypeError ):
2182
2226
# Doesn't fit AnyStr.
@@ -2191,6 +2235,9 @@ def test_errors(self):
2191
2235
with self .assertRaises (TypeError ):
2192
2236
# We don't support isinstance().
2193
2237
isinstance (42 , Pattern [str ])
2238
+ with self .assertRaises (TypeError ):
2239
+ # We don't support issubclass().
2240
+ issubclass (Pattern [bytes ], Pattern [str ])
2194
2241
2195
2242
def test_repr (self ):
2196
2243
self .assertEqual (repr (Pattern ), 'Pattern[~AnyStr]' )
0 commit comments