Skip to content

Commit

Permalink
Add some tests based on coverage report: Part 1 (#373)
Browse files Browse the repository at this point in the history
* Add some tests based on coverage report

* Fix lint
  • Loading branch information
ilevkivskyi authored Feb 1, 2017
1 parent 75380ac commit d149ce7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ def test_bound_errors(self):
with self.assertRaises(TypeError):
TypeVar('X', str, float, bound=Employee)

def test_no_bivariant(self):
with self.assertRaises(ValueError):
TypeVar('T', covariant=True, contravariant=True)


class UnionTests(BaseTestCase):

Expand Down Expand Up @@ -394,6 +398,8 @@ def test_callable_wrong_forms(self):
Callable[[()], int]
with self.assertRaises(TypeError):
Callable[[int, 1], 2]
with self.assertRaises(TypeError):
Callable[int]

def test_callable_instance_works(self):
def f():
Expand Down Expand Up @@ -518,12 +524,23 @@ def test_basics(self):

def test_generic_errors(self):
T = TypeVar('T')
S = TypeVar('S')
with self.assertRaises(TypeError):
Generic[T]()
with self.assertRaises(TypeError):
Generic[T][T]
with self.assertRaises(TypeError):
Generic[T][S]
with self.assertRaises(TypeError):
isinstance([], List[int])
with self.assertRaises(TypeError):
issubclass(list, List[int])
with self.assertRaises(TypeError):
class NewGeneric(Generic): pass
with self.assertRaises(TypeError):
class MyGeneric(Generic[T], Generic[S]): pass
with self.assertRaises(TypeError):
class MyGeneric(List[T], Generic[S]): pass

def test_init(self):
T = TypeVar('T')
Expand Down Expand Up @@ -1263,6 +1280,8 @@ def test_list(self):

def test_deque(self):
self.assertIsSubclass(collections.deque, typing.Deque)
class MyDeque(typing.Deque[int]): pass
self.assertIsInstance(MyDeque(), collections.deque)

def test_set(self):
self.assertIsSubclass(set, typing.Set)
Expand Down
38 changes: 38 additions & 0 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ def test_bound_errors(self):
with self.assertRaises(TypeError):
TypeVar('X', str, float, bound=Employee)

def test_no_bivariant(self):
with self.assertRaises(ValueError):
TypeVar('T', covariant=True, contravariant=True)


class UnionTests(BaseTestCase):

Expand Down Expand Up @@ -401,6 +405,8 @@ def test_callable_wrong_forms(self):
Callable[[()], int]
with self.assertRaises(TypeError):
Callable[[int, 1], 2]
with self.assertRaises(TypeError):
Callable[int]

def test_callable_instance_works(self):
def f():
Expand Down Expand Up @@ -549,12 +555,23 @@ def test_basics(self):

def test_generic_errors(self):
T = TypeVar('T')
S = TypeVar('S')
with self.assertRaises(TypeError):
Generic[T]()
with self.assertRaises(TypeError):
Generic[T][T]
with self.assertRaises(TypeError):
Generic[T][S]
with self.assertRaises(TypeError):
isinstance([], List[int])
with self.assertRaises(TypeError):
issubclass(list, List[int])
with self.assertRaises(TypeError):
class NewGeneric(Generic): ...
with self.assertRaises(TypeError):
class MyGeneric(Generic[T], Generic[S]): ...
with self.assertRaises(TypeError):
class MyGeneric(List[T], Generic[S]): ...

def test_init(self):
T = TypeVar('T')
Expand Down Expand Up @@ -1324,6 +1341,15 @@ def foo(a: 'whatevers') -> {}:
ith = get_type_hints(C().foo)
self.assertEqual(ith, {})

def test_no_type_check_no_bases(self):
class C:
def meth(self, x: int): ...
@no_type_check
class D(C):
c = C
# verify that @no_type_check never affects bases
self.assertEqual(get_type_hints(C.meth), {'x': int})

def test_meta_no_type_check(self):

@no_type_check_decorator
Expand Down Expand Up @@ -1526,6 +1552,8 @@ def test_previous_behavior(self):
def testf(x, y): ...
testf.__annotations__['x'] = 'int'
self.assertEqual(gth(testf), {'x': int})
def testg(x: None): ...
self.assertEqual(gth(testg), {'x': type(None)})

def test_get_type_hints_for_object_with_annotations(self):
class A: ...
Expand Down Expand Up @@ -1661,6 +1689,8 @@ def test_list(self):

def test_deque(self):
self.assertIsSubclass(collections.deque, typing.Deque)
class MyDeque(typing.Deque[int]): ...
self.assertIsInstance(MyDeque(), collections.deque)

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

def test_namedtuple_pyversion(self):
if sys.version_info[:2] < (3, 6):
with self.assertRaises(TypeError):
NamedTuple('Name', one=int, other=str)
with self.assertRaises(TypeError):
class NotYet(NamedTuple):
whatever = 0

@skipUnless(PY36, 'Python 3.6 required')
def test_annotation_usage(self):
tim = CoolEmployee('Tim', 9000)
Expand Down

0 comments on commit d149ce7

Please sign in to comment.