Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use specific IsInstance asserts
s/assert isinstance/self.assertIsInstance/
s/assert not isinstance/self.assertNotIsInstance/
  • Loading branch information
zware committed Apr 18, 2016
commit 8f35d4bcbcdc094580f9d209fe5f649d8fe0e3c0
104 changes: 52 additions & 52 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_basic_plain(self):
# T is a subclass of itself.
self.assertIsSubclass(T, T)
# T is an instance of TypeVar
assert isinstance(T, TypeVar)
self.assertIsInstance(T, TypeVar)

def test_typevar_instance_type_error(self):
T = TypeVar('T')
Expand Down Expand Up @@ -466,20 +466,20 @@ def test_cannot_instantiate(self):
def test_callable_instance_works(self):
def f():
pass
assert isinstance(f, Callable)
assert not isinstance(None, Callable)
self.assertIsInstance(f, Callable)
self.assertNotIsInstance(None, Callable)

def test_callable_instance_type_error(self):
def f():
pass
with self.assertRaises(TypeError):
assert isinstance(f, Callable[[], None])
self.assertIsInstance(f, Callable[[], None])
with self.assertRaises(TypeError):
assert isinstance(f, Callable[[], Any])
self.assertIsInstance(f, Callable[[], Any])
with self.assertRaises(TypeError):
assert not isinstance(None, Callable[[], None])
self.assertNotIsInstance(None, Callable[[], None])
with self.assertRaises(TypeError):
assert not isinstance(None, Callable[[], Any])
self.assertNotIsInstance(None, Callable[[], Any])

def test_repr(self):
ct0 = Callable[[], bool]
Expand Down Expand Up @@ -1100,26 +1100,26 @@ def __anext__(self) -> T_a:
class CollectionsAbcTests(TestCase):

def test_hashable(self):
assert isinstance(42, typing.Hashable)
assert not isinstance([], typing.Hashable)
self.assertIsInstance(42, typing.Hashable)
self.assertNotIsInstance([], typing.Hashable)

def test_iterable(self):
assert isinstance([], typing.Iterable)
self.assertIsInstance([], typing.Iterable)
# Due to ABC caching, the second time takes a separate code
# path and could fail. So call this a few times.
assert isinstance([], typing.Iterable)
assert isinstance([], typing.Iterable)
assert isinstance([], typing.Iterable[int])
assert not isinstance(42, typing.Iterable)
self.assertIsInstance([], typing.Iterable)
self.assertIsInstance([], typing.Iterable)
self.assertIsInstance([], typing.Iterable[int])
self.assertNotIsInstance(42, typing.Iterable)
# Just in case, also test issubclass() a few times.
self.assertIsSubclass(list, typing.Iterable)
self.assertIsSubclass(list, typing.Iterable)

def test_iterator(self):
it = iter([])
assert isinstance(it, typing.Iterator)
assert isinstance(it, typing.Iterator[int])
assert not isinstance(42, typing.Iterator)
self.assertIsInstance(it, typing.Iterator)
self.assertIsInstance(it, typing.Iterator[int])
self.assertNotIsInstance(42, typing.Iterator)

@skipUnless(PY35, 'Python 3.5 required')
def test_awaitable(self):
Expand All @@ -1131,8 +1131,8 @@ def test_awaitable(self):
foo = ns['foo']
g = foo()
self.assertIsSubclass(type(g), typing.Awaitable[int])
assert isinstance(g, typing.Awaitable)
assert not isinstance(foo, typing.Awaitable)
self.assertIsInstance(g, typing.Awaitable)
self.assertNotIsInstance(foo, typing.Awaitable)
self.assertIsSubclass(typing.Awaitable[Manager],
typing.Awaitable[Employee])
self.assertNotIsSubclass(typing.Awaitable[Employee],
Expand All @@ -1143,56 +1143,56 @@ def test_awaitable(self):
def test_async_iterable(self):
base_it = range(10) # type: Iterator[int]
it = AsyncIteratorWrapper(base_it)
assert isinstance(it, typing.AsyncIterable)
assert isinstance(it, typing.AsyncIterable)
self.assertIsInstance(it, typing.AsyncIterable)
self.assertIsInstance(it, typing.AsyncIterable)
self.assertIsSubclass(typing.AsyncIterable[Manager],
typing.AsyncIterable[Employee])
assert not isinstance(42, typing.AsyncIterable)
self.assertNotIsInstance(42, typing.AsyncIterable)

@skipUnless(PY35, 'Python 3.5 required')
def test_async_iterator(self):
base_it = range(10) # type: Iterator[int]
it = AsyncIteratorWrapper(base_it)
assert isinstance(it, typing.AsyncIterator)
self.assertIsInstance(it, typing.AsyncIterator)
self.assertIsSubclass(typing.AsyncIterator[Manager],
typing.AsyncIterator[Employee])
assert not isinstance(42, typing.AsyncIterator)
self.assertNotIsInstance(42, typing.AsyncIterator)

def test_sized(self):
assert isinstance([], typing.Sized)
assert not isinstance(42, typing.Sized)
self.assertIsInstance([], typing.Sized)
self.assertNotIsInstance(42, typing.Sized)

def test_container(self):
assert isinstance([], typing.Container)
assert not isinstance(42, typing.Container)
self.assertIsInstance([], typing.Container)
self.assertNotIsInstance(42, typing.Container)

def test_abstractset(self):
assert isinstance(set(), typing.AbstractSet)
assert not isinstance(42, typing.AbstractSet)
self.assertIsInstance(set(), typing.AbstractSet)
self.assertNotIsInstance(42, typing.AbstractSet)

def test_mutableset(self):
assert isinstance(set(), typing.MutableSet)
assert not isinstance(frozenset(), typing.MutableSet)
self.assertIsInstance(set(), typing.MutableSet)
self.assertNotIsInstance(frozenset(), typing.MutableSet)

def test_mapping(self):
assert isinstance({}, typing.Mapping)
assert not isinstance(42, typing.Mapping)
self.assertIsInstance({}, typing.Mapping)
self.assertNotIsInstance(42, typing.Mapping)

def test_mutablemapping(self):
assert isinstance({}, typing.MutableMapping)
assert not isinstance(42, typing.MutableMapping)
self.assertIsInstance({}, typing.MutableMapping)
self.assertNotIsInstance(42, typing.MutableMapping)

def test_sequence(self):
assert isinstance([], typing.Sequence)
assert not isinstance(42, typing.Sequence)
self.assertIsInstance([], typing.Sequence)
self.assertNotIsInstance(42, typing.Sequence)

def test_mutablesequence(self):
assert isinstance([], typing.MutableSequence)
assert not isinstance((), typing.MutableSequence)
self.assertIsInstance([], typing.MutableSequence)
self.assertNotIsInstance((), typing.MutableSequence)

def test_bytestring(self):
assert isinstance(b'', typing.ByteString)
assert isinstance(bytearray(b''), typing.ByteString)
self.assertIsInstance(b'', typing.ByteString)
self.assertIsInstance(bytearray(b''), typing.ByteString)

def test_list(self):
self.assertIsSubclass(list, typing.List)
Expand Down Expand Up @@ -1222,7 +1222,7 @@ class MyList(typing.List[int]):
pass

a = MyList()
assert isinstance(a, MyList)
self.assertIsInstance(a, MyList)

def test_no_dict_instantiation(self):
with self.assertRaises(TypeError):
Expand All @@ -1238,7 +1238,7 @@ class MyDict(typing.Dict[str, int]):
pass

d = MyDict()
assert isinstance(d, MyDict)
self.assertIsInstance(d, MyDict)

def test_no_defaultdict_instantiation(self):
with self.assertRaises(TypeError):
Expand All @@ -1254,7 +1254,7 @@ class MyDefDict(typing.DefaultDict[str, int]):
pass

dd = MyDefDict()
assert isinstance(dd, MyDefDict)
self.assertIsInstance(dd, MyDefDict)

def test_no_set_instantiation(self):
with self.assertRaises(TypeError):
Expand All @@ -1270,7 +1270,7 @@ class MySet(typing.Set[int]):
pass

d = MySet()
assert isinstance(d, MySet)
self.assertIsInstance(d, MySet)

def test_no_frozenset_instantiation(self):
with self.assertRaises(TypeError):
Expand All @@ -1286,7 +1286,7 @@ class MyFrozenSet(typing.FrozenSet[int]):
pass

d = MyFrozenSet()
assert isinstance(d, MyFrozenSet)
self.assertIsInstance(d, MyFrozenSet)

def test_no_tuple_instantiation(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -1347,9 +1347,9 @@ def manager():
yield 42

cm = manager()
assert isinstance(cm, typing.ContextManager)
assert isinstance(cm, typing.ContextManager[int])
assert not isinstance(42, typing.ContextManager)
self.assertIsInstance(cm, typing.ContextManager)
self.assertIsInstance(cm, typing.ContextManager[int])
self.assertNotIsInstance(42, typing.ContextManager)


class NamedTupleTests(TestCase):
Expand All @@ -1359,8 +1359,8 @@ def test_basics(self):
self.assertIsSubclass(Emp, tuple)
joe = Emp('Joe', 42)
jim = Emp(name='Jim', id=1)
assert isinstance(joe, Emp)
assert isinstance(joe, tuple)
self.assertIsInstance(joe, Emp)
self.assertIsInstance(joe, tuple)
assert joe.name == 'Joe'
assert joe.id == 42
assert jim.name == 'Jim'
Expand Down