diff --git a/python2/test_typing.py b/python2/test_typing.py index 8fef143e..0994662f 100644 --- a/python2/test_typing.py +++ b/python2/test_typing.py @@ -19,6 +19,23 @@ import typing +class BaseTestCase(TestCase): + + def assertIsSubclass(self, cls, class_or_tuple, msg=None): + if not issubclass(cls, class_or_tuple): + message = '%r is not a subclass of %r' % (cls, class_or_tuple) + if msg is not None: + message += ' : %s' % msg + raise self.failureException(message) + + def assertNotIsSubclass(self, cls, class_or_tuple, msg=None): + if issubclass(cls, class_or_tuple): + message = '%r is a subclass of %r' % (cls, class_or_tuple) + if msg is not None: + message += ' : %s' % msg + raise self.failureException(message) + + class Employee(object): pass @@ -35,7 +52,7 @@ class ManagingFounder(Manager, Founder): pass -class AnyTests(TestCase): +class AnyTests(BaseTestCase): def test_any_instance_type_error(self): with self.assertRaises(TypeError): @@ -78,40 +95,40 @@ def test_cannot_subscript(self): def test_any_is_subclass(self): # Any should be considered a subclass of everything. - assert issubclass(Any, Any) - assert issubclass(Any, typing.List) - assert issubclass(Any, typing.List[int]) - assert issubclass(Any, typing.List[T]) - assert issubclass(Any, typing.Mapping) - assert issubclass(Any, typing.Mapping[str, int]) - assert issubclass(Any, typing.Mapping[KT, VT]) - assert issubclass(Any, Generic) - assert issubclass(Any, Generic[T]) - assert issubclass(Any, Generic[KT, VT]) - assert issubclass(Any, AnyStr) - assert issubclass(Any, Union) - assert issubclass(Any, Union[int, str]) - assert issubclass(Any, typing.Match) - assert issubclass(Any, typing.Match[str]) + self.assertIsSubclass(Any, Any) + self.assertIsSubclass(Any, typing.List) + self.assertIsSubclass(Any, typing.List[int]) + self.assertIsSubclass(Any, typing.List[T]) + self.assertIsSubclass(Any, typing.Mapping) + self.assertIsSubclass(Any, typing.Mapping[str, int]) + self.assertIsSubclass(Any, typing.Mapping[KT, VT]) + self.assertIsSubclass(Any, Generic) + self.assertIsSubclass(Any, Generic[T]) + self.assertIsSubclass(Any, Generic[KT, VT]) + self.assertIsSubclass(Any, AnyStr) + self.assertIsSubclass(Any, Union) + self.assertIsSubclass(Any, Union[int, str]) + self.assertIsSubclass(Any, typing.Match) + self.assertIsSubclass(Any, typing.Match[str]) # These expressions must simply not fail. typing.Match[Any] typing.Pattern[Any] typing.IO[Any] -class TypeVarTests(TestCase): +class TypeVarTests(BaseTestCase): def test_basic_plain(self): T = TypeVar('T') # Every class is a subclass of T. - assert issubclass(int, T) - assert issubclass(str, T) + self.assertIsSubclass(int, T) + self.assertIsSubclass(str, T) # T equals itself. - assert T == T + self.assertEqual(T, T) # T is a subclass of itself. - assert issubclass(T, T) + 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') @@ -121,13 +138,13 @@ def test_typevar_instance_type_error(self): def test_basic_constrained(self): A = TypeVar('A', str, bytes) # Only str and bytes are subclasses of A. - assert issubclass(str, A) - assert issubclass(bytes, A) - assert not issubclass(int, A) + self.assertIsSubclass(str, A) + self.assertIsSubclass(bytes, A) + self.assertNotIsSubclass(int, A) # A equals itself. - assert A == A + self.assertEqual(A, A) # A is a subclass of itself. - assert issubclass(A, A) + self.assertIsSubclass(A, A) def test_constrained_error(self): with self.assertRaises(TypeError): @@ -137,18 +154,18 @@ def test_constrained_error(self): def test_union_unique(self): X = TypeVar('X') Y = TypeVar('Y') - assert X != Y - assert Union[X] == X - assert Union[X] != Union[X, Y] - assert Union[X, X] == X - assert Union[X, int] != Union[X] - assert Union[X, int] != Union[int] - assert Union[X, int].__union_params__ == (X, int) - assert Union[X, int].__union_set_params__ == {X, int} + self.assertNotEqual(X, Y) + self.assertEqual(Union[X], X) + self.assertNotEqual(Union[X], Union[X, Y]) + self.assertEqual(Union[X, X], X) + self.assertNotEqual(Union[X, int], Union[X]) + self.assertNotEqual(Union[X, int], Union[int]) + self.assertEqual(Union[X, int].__union_params__, (X, int)) + self.assertEqual(Union[X, int].__union_set_params__, {X, int}) def test_union_constrained(self): A = TypeVar('A', str, bytes) - assert Union[A, str] != Union[A] + self.assertNotEqual(Union[A, str], Union[A]) def test_repr(self): self.assertEqual(repr(T), '~T') @@ -193,9 +210,9 @@ def test_cannot_instantiate_vars(self): def test_bound(self): X = TypeVar('X', bound=Employee) - assert issubclass(Employee, X) - assert issubclass(Manager, X) - assert not issubclass(int, X) + self.assertIsSubclass(Employee, X) + self.assertIsSubclass(Manager, X) + self.assertNotIsSubclass(int, X) def test_bound_errors(self): with self.assertRaises(TypeError): @@ -204,7 +221,7 @@ def test_bound_errors(self): TypeVar('X', str, float, bound=Employee) -class UnionTests(TestCase): +class UnionTests(BaseTestCase): def test_basics(self): u = Union[int, float] @@ -307,8 +324,8 @@ def test_empty(self): Union[()] def test_issubclass_union(self): - assert issubclass(Union[int, str], Union) - assert not issubclass(int, Union) + self.assertIsSubclass(Union[int, str], Union) + self.assertNotIsSubclass(int, Union) def test_union_instance_type_error(self): with self.assertRaises(TypeError): @@ -320,21 +337,21 @@ def test_union_str_pattern(self): A -class TypeVarUnionTests(TestCase): +class TypeVarUnionTests(BaseTestCase): def test_simpler(self): A = TypeVar('A', int, str, float) B = TypeVar('B', int, str) - assert issubclass(A, A) - assert issubclass(B, B) - assert not issubclass(B, A) - assert issubclass(A, Union[int, str, float]) - assert not issubclass(Union[int, str, float], A) - assert not issubclass(Union[int, str], B) - assert issubclass(B, Union[int, str]) - assert not issubclass(A, B) - assert not issubclass(Union[int, str, float], B) - assert not issubclass(A, Union[int, str]) + self.assertIsSubclass(A, A) + self.assertIsSubclass(B, B) + self.assertNotIsSubclass(B, A) + self.assertIsSubclass(A, Union[int, str, float]) + self.assertNotIsSubclass(Union[int, str, float], A) + self.assertNotIsSubclass(Union[int, str], B) + self.assertIsSubclass(B, Union[int, str]) + self.assertNotIsSubclass(A, B) + self.assertNotIsSubclass(Union[int, str, float], B) + self.assertNotIsSubclass(A, Union[int, str]) def test_var_union_subclass(self): self.assertTrue(issubclass(T, Union[int, T])) @@ -342,11 +359,11 @@ def test_var_union_subclass(self): def test_var_union(self): TU = TypeVar('TU', Union[int, float], None) - assert issubclass(int, TU) - assert issubclass(float, TU) + self.assertIsSubclass(int, TU) + self.assertIsSubclass(float, TU) -class TupleTests(TestCase): +class TupleTests(BaseTestCase): def test_basics(self): self.assertTrue(issubclass(Tuple[int, str], Tuple)) @@ -359,10 +376,10 @@ def test_basics(self): self.assertFalse(issubclass(Tuple, tuple)) # Can't have it both ways. def test_equality(self): - assert Tuple[int] == Tuple[int] - assert Tuple[int, ...] == Tuple[int, ...] - assert Tuple[int] != Tuple[int, int] - assert Tuple[int] != Tuple[int, ...] + self.assertEqual(Tuple[int], Tuple[int]) + self.assertEqual(Tuple[int, ...], Tuple[int, ...]) + self.assertNotEqual(Tuple[int], Tuple[int, int]) + self.assertNotEqual(Tuple[int], Tuple[int, ...]) def test_tuple_subclass(self): class MyTuple(tuple): @@ -383,10 +400,10 @@ class B(object): class C(B): pass - assert not issubclass(Tuple[B], Tuple[B, ...]) - assert issubclass(Tuple[C, ...], Tuple[B, ...]) - assert not issubclass(Tuple[C, ...], Tuple[B]) - assert not issubclass(Tuple[C], Tuple[B, ...]) + self.assertNotIsSubclass(Tuple[B], Tuple[B, ...]) + self.assertIsSubclass(Tuple[C, ...], Tuple[B, ...]) + self.assertNotIsSubclass(Tuple[C, ...], Tuple[B]) + self.assertNotIsSubclass(Tuple[C], Tuple[B, ...]) def test_repr(self): self.assertEqual(repr(Tuple), 'typing.Tuple') @@ -401,7 +418,7 @@ def test_errors(self): issubclass(42, Tuple[int]) -class CallableTests(TestCase): +class CallableTests(BaseTestCase): def test_self_subclass(self): self.assertTrue(issubclass(Callable[[int], int], Callable)) @@ -446,20 +463,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] @@ -504,15 +521,15 @@ def get(self, key, default=None): return default -class ProtocolTests(TestCase): +class ProtocolTests(BaseTestCase): def test_supports_int(self): - assert issubclass(int, typing.SupportsInt) - assert not issubclass(str, typing.SupportsInt) + self.assertIsSubclass(int, typing.SupportsInt) + self.assertNotIsSubclass(str, typing.SupportsInt) def test_supports_float(self): - assert issubclass(float, typing.SupportsFloat) - assert not issubclass(str, typing.SupportsFloat) + self.assertIsSubclass(float, typing.SupportsFloat) + self.assertNotIsSubclass(str, typing.SupportsFloat) def test_supports_complex(self): @@ -521,34 +538,34 @@ class C(object): def __complex__(self): return 0j - assert issubclass(C, typing.SupportsComplex) - assert not issubclass(str, typing.SupportsComplex) + self.assertIsSubclass(C, typing.SupportsComplex) + self.assertNotIsSubclass(str, typing.SupportsComplex) def test_supports_abs(self): - assert issubclass(float, typing.SupportsAbs) - assert issubclass(int, typing.SupportsAbs) - assert not issubclass(str, typing.SupportsAbs) + self.assertIsSubclass(float, typing.SupportsAbs) + self.assertIsSubclass(int, typing.SupportsAbs) + self.assertNotIsSubclass(str, typing.SupportsAbs) def test_reversible(self): - assert issubclass(list, typing.Reversible) - assert not issubclass(int, typing.Reversible) + self.assertIsSubclass(list, typing.Reversible) + self.assertNotIsSubclass(int, typing.Reversible) def test_protocol_instance_type_error(self): with self.assertRaises(TypeError): isinstance(0, typing.SupportsAbs) -class GenericTests(TestCase): +class GenericTests(BaseTestCase): def test_basics(self): X = SimpleMapping[str, Any] - assert X.__parameters__ == () + self.assertEqual(X.__parameters__, ()) with self.assertRaises(TypeError): X[unicode] with self.assertRaises(TypeError): X[unicode, unicode] Y = SimpleMapping[XK, unicode] - assert Y.__parameters__ == (XK,) + self.assertEqual(Y.__parameters__, (XK,)) Y[unicode] with self.assertRaises(TypeError): Y[unicode, unicode] @@ -575,21 +592,21 @@ class C(Generic[T]): pass X = C[Tuple[S, T]] - assert X == C[Tuple[S, T]] - assert X != C[Tuple[T, S]] + self.assertEqual(X, C[Tuple[S, T]]) + self.assertNotEqual(X, C[Tuple[T, S]]) Y = X[T, int] - assert Y == X[T, int] - assert Y != X[S, int] - assert Y != X[T, str] + self.assertEqual(Y, X[T, int]) + self.assertNotEqual(Y, X[S, int]) + self.assertNotEqual(Y, X[T, str]) Z = Y[str] - assert Z == Y[str] - assert Z != Y[int] - assert Z != Y[T] + self.assertEqual(Z, Y[str]) + self.assertNotEqual(Z, Y[int]) + self.assertNotEqual(Z, Y[T]) - assert str(Z).endswith( - '.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]') + self.assertTrue(str(Z).endswith( + '.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]')) def test_dict(self): T = TypeVar('T') @@ -641,28 +658,30 @@ def test_repr_2(self): class C(Generic[T]): pass - assert C.__module__ == __name__ + self.assertEqual(C.__module__, __name__) if not PY32: - assert C.__qualname__ == 'GenericTests.test_repr_2..C' - assert repr(C).split('.')[-1] == 'C<~T>' + self.assertEqual(C.__qualname__, + 'GenericTests.test_repr_2..C') + self.assertEqual(repr(C).split('.')[-1], 'C<~T>') X = C[int] - assert X.__module__ == __name__ + self.assertEqual(X.__module__, __name__) if not PY32: - assert X.__qualname__ == 'C' - assert repr(X).split('.')[-1] == 'C<~T>[int]' + self.assertEqual(X.__qualname__, 'C') + self.assertEqual(repr(X).split('.')[-1], 'C<~T>[int]') class Y(C[int]): pass - assert Y.__module__ == __name__ + self.assertEqual(Y.__module__, __name__) if not PY32: - assert Y.__qualname__ == 'GenericTests.test_repr_2..Y' - assert repr(Y).split('.')[-1] == 'Y' + self.assertEqual(Y.__qualname__, + 'GenericTests.test_repr_2..Y') + self.assertEqual(repr(Y).split('.')[-1], 'Y') def test_eq_1(self): - assert Generic == Generic - assert Generic[T] == Generic[T] - assert Generic[KT] != Generic[VT] + self.assertEqual(Generic, Generic) + self.assertEqual(Generic[T], Generic[T]) + self.assertNotEqual(Generic[KT], Generic[VT]) def test_eq_2(self): @@ -672,10 +691,10 @@ class A(Generic[T]): class B(Generic[T]): pass - assert A == A - assert A != B - assert A[T] == A[T] - assert A[T] != B[T] + self.assertEqual(A, A) + self.assertNotEqual(A, B) + self.assertEqual(A[T], A[T]) + self.assertNotEqual(A[T], B[T]) def test_multiple_inheritance(self): @@ -688,7 +707,7 @@ class B(Generic[KT, T]): class C(A[T, VT], Generic[VT, T, KT], B[KT, T]): pass - assert C.__parameters__ == (VT, T, KT) + self.assertEqual(C.__parameters__, (VT, T, KT)) def test_nested(self): @@ -718,7 +737,7 @@ def append(self, x): a.set([]) a.append(1) a.append(42) - assert a.get() == [1, 42] + self.assertEqual(a.get(), [1, 42]) def test_type_erasure(self): T = TypeVar('T') @@ -735,12 +754,12 @@ def foo(x): a = Node(x) b = Node[T](x) c = Node[Any](x) - assert type(a) is Node - assert type(b) is Node - assert type(c) is Node - assert a.label == x - assert b.label == x - assert c.label == x + self.assertIs(type(a), Node) + self.assertIs(type(b), Node) + self.assertIs(type(c), Node) + self.assertEqual(a.label, x) + self.assertEqual(b.label, x) + self.assertEqual(c.label, x) foo(42) @@ -753,7 +772,7 @@ class C(Generic[T]): class D(C): pass - assert D.__parameters__ == () + self.assertEqual(D.__parameters__, ()) with self.assertRaises(Exception): D[int] @@ -763,61 +782,63 @@ class D(C): D[T] -class VarianceTests(TestCase): +class VarianceTests(BaseTestCase): def test_invariance(self): # Because of invariance, List[subclass of X] is not a subclass # of List[X], and ditto for MutableSequence. - assert not issubclass(typing.List[Manager], typing.List[Employee]) - assert not issubclass(typing.MutableSequence[Manager], + self.assertNotIsSubclass(typing.List[Manager], typing.List[Employee]) + self.assertNotIsSubclass(typing.MutableSequence[Manager], typing.MutableSequence[Employee]) # It's still reflexive. - assert issubclass(typing.List[Employee], typing.List[Employee]) - assert issubclass(typing.MutableSequence[Employee], + self.assertIsSubclass(typing.List[Employee], typing.List[Employee]) + self.assertIsSubclass(typing.MutableSequence[Employee], typing.MutableSequence[Employee]) def test_covariance_tuple(self): # Check covariace for Tuple (which are really special cases). - assert issubclass(Tuple[Manager], Tuple[Employee]) - assert not issubclass(Tuple[Employee], Tuple[Manager]) + self.assertIsSubclass(Tuple[Manager], Tuple[Employee]) + self.assertNotIsSubclass(Tuple[Employee], Tuple[Manager]) # And pairwise. - assert issubclass(Tuple[Manager, Manager], Tuple[Employee, Employee]) - assert not issubclass(Tuple[Employee, Employee], + self.assertIsSubclass(Tuple[Manager, Manager], + Tuple[Employee, Employee]) + self.assertNotIsSubclass(Tuple[Employee, Employee], Tuple[Manager, Employee]) # And using ellipsis. - assert issubclass(Tuple[Manager, ...], Tuple[Employee, ...]) - assert not issubclass(Tuple[Employee, ...], Tuple[Manager, ...]) + self.assertIsSubclass(Tuple[Manager, ...], Tuple[Employee, ...]) + self.assertNotIsSubclass(Tuple[Employee, ...], Tuple[Manager, ...]) def test_covariance_sequence(self): # Check covariance for Sequence (which is just a generic class # for this purpose, but using a covariant type variable). - assert issubclass(typing.Sequence[Manager], typing.Sequence[Employee]) - assert not issubclass(typing.Sequence[Employee], + self.assertIsSubclass(typing.Sequence[Manager], + typing.Sequence[Employee]) + self.assertNotIsSubclass(typing.Sequence[Employee], typing.Sequence[Manager]) def test_covariance_mapping(self): # Ditto for Mapping (covariant in the value, invariant in the key). - assert issubclass(typing.Mapping[Employee, Manager], + self.assertIsSubclass(typing.Mapping[Employee, Manager], typing.Mapping[Employee, Employee]) - assert not issubclass(typing.Mapping[Manager, Employee], + self.assertNotIsSubclass(typing.Mapping[Manager, Employee], typing.Mapping[Employee, Employee]) - assert not issubclass(typing.Mapping[Employee, Manager], + self.assertNotIsSubclass(typing.Mapping[Employee, Manager], typing.Mapping[Manager, Manager]) - assert not issubclass(typing.Mapping[Manager, Employee], + self.assertNotIsSubclass(typing.Mapping[Manager, Employee], typing.Mapping[Manager, Manager]) -class CastTests(TestCase): +class CastTests(BaseTestCase): def test_basics(self): - assert cast(int, 42) == 42 - assert cast(float, 42) == 42 - assert type(cast(float, 42)) is int - assert cast(Any, 42) == 42 - assert cast(list, 42) == 42 - assert cast(Union[str, float], 42) == 42 - assert cast(AnyStr, 42) == 42 - assert cast(None, 42) == 42 + self.assertEqual(cast(int, 42), 42) + self.assertEqual(cast(float, 42), 42) + self.assertIs(type(cast(float, 42)), int) + self.assertEqual(cast(Any, 42), 42) + self.assertEqual(cast(list, 42), 42) + self.assertEqual(cast(Union[str, float], 42), 42) + self.assertEqual(cast(AnyStr, 42), 42) + self.assertEqual(cast(None, 42), 42) def test_errors(self): # Bogus calls are not expected to fail. @@ -825,7 +846,7 @@ def test_errors(self): cast('hello', 42) -class ForwardRefTests(TestCase): +class ForwardRefTests(BaseTestCase): def test_forwardref_instance_type_error(self): fr = typing._ForwardRef('int') @@ -838,7 +859,7 @@ def test_syntax_error(self): Generic['/T'] -class OverloadTests(TestCase): +class OverloadTests(BaseTestCase): def test_overload_exists(self): from typing import overload @@ -867,79 +888,79 @@ def blah(): blah() -class CollectionsAbcTests(TestCase): +class CollectionsAbcTests(BaseTestCase): 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. - assert issubclass(list, typing.Iterable) - assert issubclass(list, typing.Iterable) + 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) 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): - assert issubclass(list, typing.List) + self.assertIsSubclass(list, typing.List) def test_set(self): - assert issubclass(set, typing.Set) - assert not issubclass(frozenset, typing.Set) + self.assertIsSubclass(set, typing.Set) + self.assertNotIsSubclass(frozenset, typing.Set) def test_frozenset(self): - assert issubclass(frozenset, typing.FrozenSet) - assert not issubclass(set, typing.FrozenSet) + self.assertIsSubclass(frozenset, typing.FrozenSet) + self.assertNotIsSubclass(set, typing.FrozenSet) def test_dict(self): - assert issubclass(dict, typing.Dict) + self.assertIsSubclass(dict, typing.Dict) def test_no_list_instantiation(self): with self.assertRaises(TypeError): @@ -955,7 +976,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): @@ -971,7 +992,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): @@ -987,7 +1008,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): @@ -1003,7 +1024,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): @@ -1019,7 +1040,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): @@ -1033,10 +1054,10 @@ def test_generator(self): def foo(): yield 42 g = foo() - assert issubclass(type(g), typing.Generator) - assert issubclass(typing.Generator[Manager, Employee, Manager], + self.assertIsSubclass(type(g), typing.Generator) + self.assertIsSubclass(typing.Generator[Manager, Employee, Manager], typing.Generator[Employee, Manager, Employee]) - assert not issubclass(typing.Generator[Manager, Manager, Manager], + self.assertNotIsSubclass(typing.Generator[Manager, Manager, Manager], typing.Generator[Employee, Employee, Employee]) def test_no_generator_instantiation(self): @@ -1059,33 +1080,33 @@ class MMC(MMA): def __len__(self): return 0 - assert len(MMC()) == 0 + self.assertEqual(len(MMC()), 0) class MMB(typing.MutableMapping[KT, VT]): def __len__(self): return 0 - assert len(MMB()) == 0 - assert len(MMB[str, str]()) == 0 - assert len(MMB[KT, VT]()) == 0 + self.assertEqual(len(MMB()), 0) + self.assertEqual(len(MMB[str, str]()), 0) + self.assertEqual(len(MMB[KT, VT]()), 0) -class NamedTupleTests(TestCase): +class NamedTupleTests(BaseTestCase): def test_basics(self): Emp = NamedTuple('Emp', [('name', str), ('id', int)]) - assert issubclass(Emp, tuple) + self.assertIsSubclass(Emp, tuple) joe = Emp('Joe', 42) jim = Emp(name='Jim', id=1) - assert isinstance(joe, Emp) - assert isinstance(joe, tuple) - assert joe.name == 'Joe' - assert joe.id == 42 - assert jim.name == 'Jim' - assert jim.id == 1 - assert Emp.__name__ == 'Emp' - assert Emp._fields == ('name', 'id') - assert Emp._field_types == dict(name=str, id=int) + self.assertIsInstance(joe, Emp) + self.assertIsInstance(joe, tuple) + self.assertEqual(joe.name, 'Joe') + self.assertEqual(joe.id, 42) + self.assertEqual(jim.name, 'Jim') + self.assertEqual(jim.id, 1) + self.assertEqual(Emp.__name__, 'Emp') + self.assertEqual(Emp._fields, ('name', 'id')) + self.assertEqual(Emp._field_types, dict(name=str, id=int)) def test_pickle(self): global Emp # pickle wants to reference the class by name @@ -1097,40 +1118,40 @@ def test_pickle(self): self.assertEqual(jane2, jane) -class IOTests(TestCase): +class IOTests(BaseTestCase): def test_io_submodule(self): from typing.io import IO, TextIO, BinaryIO, __all__, __name__ - assert IO is typing.IO - assert TextIO is typing.TextIO - assert BinaryIO is typing.BinaryIO - assert set(__all__) == set(['IO', 'TextIO', 'BinaryIO']) - assert __name__ == 'typing.io' + self.assertIs(IO, typing.IO) + self.assertIs(TextIO, typing.TextIO) + self.assertIs(BinaryIO, typing.BinaryIO) + self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO'])) + self.assertEqual(__name__, 'typing.io') -class RETests(TestCase): +class RETests(BaseTestCase): # Much of this is really testing _TypeAlias. def test_basics(self): pat = re.compile('[a-z]+', re.I) - assert issubclass(pat.__class__, Pattern) - assert issubclass(type(pat), Pattern) - assert issubclass(type(pat), Pattern[str]) + self.assertIsSubclass(pat.__class__, Pattern) + self.assertIsSubclass(type(pat), Pattern) + self.assertIsSubclass(type(pat), Pattern[str]) mat = pat.search('12345abcde.....') - assert issubclass(mat.__class__, Match) - assert issubclass(mat.__class__, Match[str]) - assert issubclass(mat.__class__, Match[bytes]) # Sad but true. - assert issubclass(type(mat), Match) - assert issubclass(type(mat), Match[str]) + self.assertIsSubclass(mat.__class__, Match) + self.assertIsSubclass(mat.__class__, Match[str]) + self.assertIsSubclass(mat.__class__, Match[bytes]) # Sad but true. + self.assertIsSubclass(type(mat), Match) + self.assertIsSubclass(type(mat), Match[str]) p = Pattern[Union[str, bytes]] - assert issubclass(Pattern[str], Pattern) - assert issubclass(Pattern[str], p) + self.assertIsSubclass(Pattern[str], Pattern) + self.assertIsSubclass(Pattern[str], p) m = Match[Union[bytes, str]] - assert issubclass(Match[bytes], Match) - assert issubclass(Match[bytes], m) + self.assertIsSubclass(Match[bytes], Match) + self.assertIsSubclass(Match[bytes], m) def test_errors(self): with self.assertRaises(TypeError): @@ -1151,19 +1172,19 @@ def test_errors(self): isinstance(42, Pattern[str]) def test_repr(self): - assert repr(Pattern) == 'Pattern[~AnyStr]' - assert repr(Pattern[unicode]) == 'Pattern[unicode]' - assert repr(Pattern[str]) == 'Pattern[str]' - assert repr(Match) == 'Match[~AnyStr]' - assert repr(Match[unicode]) == 'Match[unicode]' - assert repr(Match[str]) == 'Match[str]' + self.assertEqual(repr(Pattern), 'Pattern[~AnyStr]') + self.assertEqual(repr(Pattern[unicode]), 'Pattern[unicode]') + self.assertEqual(repr(Pattern[str]), 'Pattern[str]') + self.assertEqual(repr(Match), 'Match[~AnyStr]') + self.assertEqual(repr(Match[unicode]), 'Match[unicode]') + self.assertEqual(repr(Match[str]), 'Match[str]') def test_re_submodule(self): from typing.re import Match, Pattern, __all__, __name__ - assert Match is typing.Match - assert Pattern is typing.Pattern - assert set(__all__) == set(['Match', 'Pattern']) - assert __name__ == 'typing.re' + self.assertIs(Match, typing.Match) + self.assertIs(Pattern, typing.Pattern) + self.assertEqual(set(__all__), set(['Match', 'Pattern'])) + self.assertEqual(__name__, 'typing.re') def test_cannot_subclass(self): with self.assertRaises(TypeError) as ex: @@ -1171,27 +1192,28 @@ def test_cannot_subclass(self): class A(typing.Match): pass - assert str(ex.exception) == "A type alias cannot be subclassed" + self.assertEqual(str(ex.exception), + "A type alias cannot be subclassed") -class AllTests(TestCase): +class AllTests(BaseTestCase): """Tests for __all__.""" def test_all(self): from typing import __all__ as a # Just spot-check the first and last of every category. - assert 'AbstractSet' in a - assert 'ValuesView' in a - assert 'cast' in a - assert 'overload' in a + self.assertIn('AbstractSet', a) + self.assertIn('ValuesView', a) + self.assertIn('cast', a) + self.assertIn('overload', a) # Check that io and re are not exported. - assert 'io' not in a - assert 're' not in a + self.assertNotIn('io', a) + self.assertNotIn('re', a) # Spot-check that stdlib modules aren't exported. - assert 'os' not in a - assert 'sys' not in a + self.assertNotIn('os', a) + self.assertNotIn('sys', a) # Check that Text is defined. - assert 'Text' in a + self.assertIn('Text', a) def test_get_type_hints_dummy(self): @@ -1199,7 +1221,7 @@ def foo(x): # type: (int) -> int return x + 1 - assert typing.get_type_hints(foo) is None + self.assertIsNone(typing.get_type_hints(foo)) if __name__ == '__main__':