Skip to content

Commit d149ce7

Browse files
authored
Add some tests based on coverage report: Part 1 (#373)
* Add some tests based on coverage report * Fix lint
1 parent 75380ac commit d149ce7

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

python2/test_typing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ def test_bound_errors(self):
187187
with self.assertRaises(TypeError):
188188
TypeVar('X', str, float, bound=Employee)
189189

190+
def test_no_bivariant(self):
191+
with self.assertRaises(ValueError):
192+
TypeVar('T', covariant=True, contravariant=True)
193+
190194

191195
class UnionTests(BaseTestCase):
192196

@@ -394,6 +398,8 @@ def test_callable_wrong_forms(self):
394398
Callable[[()], int]
395399
with self.assertRaises(TypeError):
396400
Callable[[int, 1], 2]
401+
with self.assertRaises(TypeError):
402+
Callable[int]
397403

398404
def test_callable_instance_works(self):
399405
def f():
@@ -518,12 +524,23 @@ def test_basics(self):
518524

519525
def test_generic_errors(self):
520526
T = TypeVar('T')
527+
S = TypeVar('S')
521528
with self.assertRaises(TypeError):
522529
Generic[T]()
530+
with self.assertRaises(TypeError):
531+
Generic[T][T]
532+
with self.assertRaises(TypeError):
533+
Generic[T][S]
523534
with self.assertRaises(TypeError):
524535
isinstance([], List[int])
525536
with self.assertRaises(TypeError):
526537
issubclass(list, List[int])
538+
with self.assertRaises(TypeError):
539+
class NewGeneric(Generic): pass
540+
with self.assertRaises(TypeError):
541+
class MyGeneric(Generic[T], Generic[S]): pass
542+
with self.assertRaises(TypeError):
543+
class MyGeneric(List[T], Generic[S]): pass
527544

528545
def test_init(self):
529546
T = TypeVar('T')
@@ -1263,6 +1280,8 @@ def test_list(self):
12631280

12641281
def test_deque(self):
12651282
self.assertIsSubclass(collections.deque, typing.Deque)
1283+
class MyDeque(typing.Deque[int]): pass
1284+
self.assertIsInstance(MyDeque(), collections.deque)
12661285

12671286
def test_set(self):
12681287
self.assertIsSubclass(set, typing.Set)

src/test_typing.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ def test_bound_errors(self):
190190
with self.assertRaises(TypeError):
191191
TypeVar('X', str, float, bound=Employee)
192192

193+
def test_no_bivariant(self):
194+
with self.assertRaises(ValueError):
195+
TypeVar('T', covariant=True, contravariant=True)
196+
193197

194198
class UnionTests(BaseTestCase):
195199

@@ -401,6 +405,8 @@ def test_callable_wrong_forms(self):
401405
Callable[[()], int]
402406
with self.assertRaises(TypeError):
403407
Callable[[int, 1], 2]
408+
with self.assertRaises(TypeError):
409+
Callable[int]
404410

405411
def test_callable_instance_works(self):
406412
def f():
@@ -549,12 +555,23 @@ def test_basics(self):
549555

550556
def test_generic_errors(self):
551557
T = TypeVar('T')
558+
S = TypeVar('S')
552559
with self.assertRaises(TypeError):
553560
Generic[T]()
561+
with self.assertRaises(TypeError):
562+
Generic[T][T]
563+
with self.assertRaises(TypeError):
564+
Generic[T][S]
554565
with self.assertRaises(TypeError):
555566
isinstance([], List[int])
556567
with self.assertRaises(TypeError):
557568
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]): ...
558575

559576
def test_init(self):
560577
T = TypeVar('T')
@@ -1324,6 +1341,15 @@ def foo(a: 'whatevers') -> {}:
13241341
ith = get_type_hints(C().foo)
13251342
self.assertEqual(ith, {})
13261343

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+
13271353
def test_meta_no_type_check(self):
13281354

13291355
@no_type_check_decorator
@@ -1526,6 +1552,8 @@ def test_previous_behavior(self):
15261552
def testf(x, y): ...
15271553
testf.__annotations__['x'] = 'int'
15281554
self.assertEqual(gth(testf), {'x': int})
1555+
def testg(x: None): ...
1556+
self.assertEqual(gth(testg), {'x': type(None)})
15291557

15301558
def test_get_type_hints_for_object_with_annotations(self):
15311559
class A: ...
@@ -1661,6 +1689,8 @@ def test_list(self):
16611689

16621690
def test_deque(self):
16631691
self.assertIsSubclass(collections.deque, typing.Deque)
1692+
class MyDeque(typing.Deque[int]): ...
1693+
self.assertIsInstance(MyDeque(), collections.deque)
16641694

16651695
def test_set(self):
16661696
self.assertIsSubclass(set, typing.Set)
@@ -2047,6 +2077,14 @@ def test_basics(self):
20472077
collections.OrderedDict([('name', str), ('id', int)]))
20482078
self.assertIs(Emp._field_types, Emp.__annotations__)
20492079

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+
20502088
@skipUnless(PY36, 'Python 3.6 required')
20512089
def test_annotation_usage(self):
20522090
tim = CoolEmployee('Tim', 9000)

0 commit comments

Comments
 (0)