@@ -190,6 +190,10 @@ def test_bound_errors(self):
190
190
with self .assertRaises (TypeError ):
191
191
TypeVar ('X' , str , float , bound = Employee )
192
192
193
+ def test_no_bivariant (self ):
194
+ with self .assertRaises (ValueError ):
195
+ TypeVar ('T' , covariant = True , contravariant = True )
196
+
193
197
194
198
class UnionTests (BaseTestCase ):
195
199
@@ -401,6 +405,8 @@ def test_callable_wrong_forms(self):
401
405
Callable [[()], int ]
402
406
with self .assertRaises (TypeError ):
403
407
Callable [[int , 1 ], 2 ]
408
+ with self .assertRaises (TypeError ):
409
+ Callable [int ]
404
410
405
411
def test_callable_instance_works (self ):
406
412
def f ():
@@ -549,12 +555,23 @@ def test_basics(self):
549
555
550
556
def test_generic_errors (self ):
551
557
T = TypeVar ('T' )
558
+ S = TypeVar ('S' )
552
559
with self .assertRaises (TypeError ):
553
560
Generic [T ]()
561
+ with self .assertRaises (TypeError ):
562
+ Generic [T ][T ]
563
+ with self .assertRaises (TypeError ):
564
+ Generic [T ][S ]
554
565
with self .assertRaises (TypeError ):
555
566
isinstance ([], List [int ])
556
567
with self .assertRaises (TypeError ):
557
568
issubclass (list , List [int ])
569
+ with self .assertRaises (TypeError ):
570
+ class NewGeneric (Generic ): ...
571
+ with self .assertRaises (TypeError ):
572
+ class MyGeneric (Generic [T ], Generic [S ]): ...
573
+ with self .assertRaises (TypeError ):
574
+ class MyGeneric (List [T ], Generic [S ]): ...
558
575
559
576
def test_init (self ):
560
577
T = TypeVar ('T' )
@@ -1324,6 +1341,15 @@ def foo(a: 'whatevers') -> {}:
1324
1341
ith = get_type_hints (C ().foo )
1325
1342
self .assertEqual (ith , {})
1326
1343
1344
+ def test_no_type_check_no_bases (self ):
1345
+ class C :
1346
+ def meth (self , x : int ): ...
1347
+ @no_type_check
1348
+ class D (C ):
1349
+ c = C
1350
+ # verify that @no_type_check never affects bases
1351
+ self .assertEqual (get_type_hints (C .meth ), {'x' : int })
1352
+
1327
1353
def test_meta_no_type_check (self ):
1328
1354
1329
1355
@no_type_check_decorator
@@ -1526,6 +1552,8 @@ def test_previous_behavior(self):
1526
1552
def testf (x , y ): ...
1527
1553
testf .__annotations__ ['x' ] = 'int'
1528
1554
self .assertEqual (gth (testf ), {'x' : int })
1555
+ def testg (x : None ): ...
1556
+ self .assertEqual (gth (testg ), {'x' : type (None )})
1529
1557
1530
1558
def test_get_type_hints_for_object_with_annotations (self ):
1531
1559
class A : ...
@@ -1661,6 +1689,8 @@ def test_list(self):
1661
1689
1662
1690
def test_deque (self ):
1663
1691
self .assertIsSubclass (collections .deque , typing .Deque )
1692
+ class MyDeque (typing .Deque [int ]): ...
1693
+ self .assertIsInstance (MyDeque (), collections .deque )
1664
1694
1665
1695
def test_set (self ):
1666
1696
self .assertIsSubclass (set , typing .Set )
@@ -2047,6 +2077,14 @@ def test_basics(self):
2047
2077
collections .OrderedDict ([('name' , str ), ('id' , int )]))
2048
2078
self .assertIs (Emp ._field_types , Emp .__annotations__ )
2049
2079
2080
+ def test_namedtuple_pyversion (self ):
2081
+ if sys .version_info [:2 ] < (3 , 6 ):
2082
+ with self .assertRaises (TypeError ):
2083
+ NamedTuple ('Name' , one = int , other = str )
2084
+ with self .assertRaises (TypeError ):
2085
+ class NotYet (NamedTuple ):
2086
+ whatever = 0
2087
+
2050
2088
@skipUnless (PY36 , 'Python 3.6 required' )
2051
2089
def test_annotation_usage (self ):
2052
2090
tim = CoolEmployee ('Tim' , 9000 )
0 commit comments