Commit 75b1bdca authored by Zachary Ware's avatar Zachary Ware

Replace assert statements with self.assertXxx() calls

Sync with upstream, see github.com/python/typing/pull/205
parent def8072c
...@@ -20,6 +20,23 @@ from typing import Pattern, Match ...@@ -20,6 +20,23 @@ from typing import Pattern, Match
import typing 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: class Employee:
pass pass
...@@ -36,7 +53,7 @@ class ManagingFounder(Manager, Founder): ...@@ -36,7 +53,7 @@ class ManagingFounder(Manager, Founder):
pass pass
class AnyTests(TestCase): class AnyTests(BaseTestCase):
def test_any_instance_type_error(self): def test_any_instance_type_error(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -79,40 +96,40 @@ class AnyTests(TestCase): ...@@ -79,40 +96,40 @@ class AnyTests(TestCase):
def test_any_is_subclass(self): def test_any_is_subclass(self):
# Any should be considered a subclass of everything. # Any should be considered a subclass of everything.
assert issubclass(Any, Any) self.assertIsSubclass(Any, Any)
assert issubclass(Any, typing.List) self.assertIsSubclass(Any, typing.List)
assert issubclass(Any, typing.List[int]) self.assertIsSubclass(Any, typing.List[int])
assert issubclass(Any, typing.List[T]) self.assertIsSubclass(Any, typing.List[T])
assert issubclass(Any, typing.Mapping) self.assertIsSubclass(Any, typing.Mapping)
assert issubclass(Any, typing.Mapping[str, int]) self.assertIsSubclass(Any, typing.Mapping[str, int])
assert issubclass(Any, typing.Mapping[KT, VT]) self.assertIsSubclass(Any, typing.Mapping[KT, VT])
assert issubclass(Any, Generic) self.assertIsSubclass(Any, Generic)
assert issubclass(Any, Generic[T]) self.assertIsSubclass(Any, Generic[T])
assert issubclass(Any, Generic[KT, VT]) self.assertIsSubclass(Any, Generic[KT, VT])
assert issubclass(Any, AnyStr) self.assertIsSubclass(Any, AnyStr)
assert issubclass(Any, Union) self.assertIsSubclass(Any, Union)
assert issubclass(Any, Union[int, str]) self.assertIsSubclass(Any, Union[int, str])
assert issubclass(Any, typing.Match) self.assertIsSubclass(Any, typing.Match)
assert issubclass(Any, typing.Match[str]) self.assertIsSubclass(Any, typing.Match[str])
# These expressions must simply not fail. # These expressions must simply not fail.
typing.Match[Any] typing.Match[Any]
typing.Pattern[Any] typing.Pattern[Any]
typing.IO[Any] typing.IO[Any]
class TypeVarTests(TestCase): class TypeVarTests(BaseTestCase):
def test_basic_plain(self): def test_basic_plain(self):
T = TypeVar('T') T = TypeVar('T')
# Every class is a subclass of T. # Every class is a subclass of T.
assert issubclass(int, T) self.assertIsSubclass(int, T)
assert issubclass(str, T) self.assertIsSubclass(str, T)
# T equals itself. # T equals itself.
assert T == T self.assertEqual(T, T)
# T is a subclass of itself. # T is a subclass of itself.
assert issubclass(T, T) self.assertIsSubclass(T, T)
# T is an instance of TypeVar # T is an instance of TypeVar
assert isinstance(T, TypeVar) self.assertIsInstance(T, TypeVar)
def test_typevar_instance_type_error(self): def test_typevar_instance_type_error(self):
T = TypeVar('T') T = TypeVar('T')
...@@ -122,13 +139,13 @@ class TypeVarTests(TestCase): ...@@ -122,13 +139,13 @@ class TypeVarTests(TestCase):
def test_basic_constrained(self): def test_basic_constrained(self):
A = TypeVar('A', str, bytes) A = TypeVar('A', str, bytes)
# Only str and bytes are subclasses of A. # Only str and bytes are subclasses of A.
assert issubclass(str, A) self.assertIsSubclass(str, A)
assert issubclass(bytes, A) self.assertIsSubclass(bytes, A)
assert not issubclass(int, A) self.assertNotIsSubclass(int, A)
# A equals itself. # A equals itself.
assert A == A self.assertEqual(A, A)
# A is a subclass of itself. # A is a subclass of itself.
assert issubclass(A, A) self.assertIsSubclass(A, A)
def test_constrained_error(self): def test_constrained_error(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -138,18 +155,18 @@ class TypeVarTests(TestCase): ...@@ -138,18 +155,18 @@ class TypeVarTests(TestCase):
def test_union_unique(self): def test_union_unique(self):
X = TypeVar('X') X = TypeVar('X')
Y = TypeVar('Y') Y = TypeVar('Y')
assert X != Y self.assertNotEqual(X, Y)
assert Union[X] == X self.assertEqual(Union[X], X)
assert Union[X] != Union[X, Y] self.assertNotEqual(Union[X], Union[X, Y])
assert Union[X, X] == X self.assertEqual(Union[X, X], X)
assert Union[X, int] != Union[X] self.assertNotEqual(Union[X, int], Union[X])
assert Union[X, int] != Union[int] self.assertNotEqual(Union[X, int], Union[int])
assert Union[X, int].__union_params__ == (X, int) self.assertEqual(Union[X, int].__union_params__, (X, int))
assert Union[X, int].__union_set_params__ == {X, int} self.assertEqual(Union[X, int].__union_set_params__, {X, int})
def test_union_constrained(self): def test_union_constrained(self):
A = TypeVar('A', str, bytes) A = TypeVar('A', str, bytes)
assert Union[A, str] != Union[A] self.assertNotEqual(Union[A, str], Union[A])
def test_repr(self): def test_repr(self):
self.assertEqual(repr(T), '~T') self.assertEqual(repr(T), '~T')
...@@ -194,9 +211,9 @@ class TypeVarTests(TestCase): ...@@ -194,9 +211,9 @@ class TypeVarTests(TestCase):
def test_bound(self): def test_bound(self):
X = TypeVar('X', bound=Employee) X = TypeVar('X', bound=Employee)
assert issubclass(Employee, X) self.assertIsSubclass(Employee, X)
assert issubclass(Manager, X) self.assertIsSubclass(Manager, X)
assert not issubclass(int, X) self.assertNotIsSubclass(int, X)
def test_bound_errors(self): def test_bound_errors(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -205,7 +222,7 @@ class TypeVarTests(TestCase): ...@@ -205,7 +222,7 @@ class TypeVarTests(TestCase):
TypeVar('X', str, float, bound=Employee) TypeVar('X', str, float, bound=Employee)
class UnionTests(TestCase): class UnionTests(BaseTestCase):
def test_basics(self): def test_basics(self):
u = Union[int, float] u = Union[int, float]
...@@ -308,8 +325,8 @@ class UnionTests(TestCase): ...@@ -308,8 +325,8 @@ class UnionTests(TestCase):
Union[()] Union[()]
def test_issubclass_union(self): def test_issubclass_union(self):
assert issubclass(Union[int, str], Union) self.assertIsSubclass(Union[int, str], Union)
assert not issubclass(int, Union) self.assertNotIsSubclass(int, Union)
def test_union_instance_type_error(self): def test_union_instance_type_error(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -321,21 +338,21 @@ class UnionTests(TestCase): ...@@ -321,21 +338,21 @@ class UnionTests(TestCase):
A A
class TypeVarUnionTests(TestCase): class TypeVarUnionTests(BaseTestCase):
def test_simpler(self): def test_simpler(self):
A = TypeVar('A', int, str, float) A = TypeVar('A', int, str, float)
B = TypeVar('B', int, str) B = TypeVar('B', int, str)
assert issubclass(A, A) self.assertIsSubclass(A, A)
assert issubclass(B, B) self.assertIsSubclass(B, B)
assert not issubclass(B, A) self.assertNotIsSubclass(B, A)
assert issubclass(A, Union[int, str, float]) self.assertIsSubclass(A, Union[int, str, float])
assert not issubclass(Union[int, str, float], A) self.assertNotIsSubclass(Union[int, str, float], A)
assert not issubclass(Union[int, str], B) self.assertNotIsSubclass(Union[int, str], B)
assert issubclass(B, Union[int, str]) self.assertIsSubclass(B, Union[int, str])
assert not issubclass(A, B) self.assertNotIsSubclass(A, B)
assert not issubclass(Union[int, str, float], B) self.assertNotIsSubclass(Union[int, str, float], B)
assert not issubclass(A, Union[int, str]) self.assertNotIsSubclass(A, Union[int, str])
def test_var_union_subclass(self): def test_var_union_subclass(self):
self.assertTrue(issubclass(T, Union[int, T])) self.assertTrue(issubclass(T, Union[int, T]))
...@@ -343,11 +360,11 @@ class TypeVarUnionTests(TestCase): ...@@ -343,11 +360,11 @@ class TypeVarUnionTests(TestCase):
def test_var_union(self): def test_var_union(self):
TU = TypeVar('TU', Union[int, float], None) TU = TypeVar('TU', Union[int, float], None)
assert issubclass(int, TU) self.assertIsSubclass(int, TU)
assert issubclass(float, TU) self.assertIsSubclass(float, TU)
class TupleTests(TestCase): class TupleTests(BaseTestCase):
def test_basics(self): def test_basics(self):
self.assertTrue(issubclass(Tuple[int, str], Tuple)) self.assertTrue(issubclass(Tuple[int, str], Tuple))
...@@ -360,10 +377,10 @@ class TupleTests(TestCase): ...@@ -360,10 +377,10 @@ class TupleTests(TestCase):
self.assertFalse(issubclass(Tuple, tuple)) # Can't have it both ways. self.assertFalse(issubclass(Tuple, tuple)) # Can't have it both ways.
def test_equality(self): def test_equality(self):
assert Tuple[int] == Tuple[int] self.assertEqual(Tuple[int], Tuple[int])
assert Tuple[int, ...] == Tuple[int, ...] self.assertEqual(Tuple[int, ...], Tuple[int, ...])
assert Tuple[int] != Tuple[int, int] self.assertNotEqual(Tuple[int], Tuple[int, int])
assert Tuple[int] != Tuple[int, ...] self.assertNotEqual(Tuple[int], Tuple[int, ...])
def test_tuple_subclass(self): def test_tuple_subclass(self):
class MyTuple(tuple): class MyTuple(tuple):
...@@ -384,10 +401,10 @@ class TupleTests(TestCase): ...@@ -384,10 +401,10 @@ class TupleTests(TestCase):
class C(B): class C(B):
pass pass
assert not issubclass(Tuple[B], Tuple[B, ...]) self.assertNotIsSubclass(Tuple[B], Tuple[B, ...])
assert issubclass(Tuple[C, ...], Tuple[B, ...]) self.assertIsSubclass(Tuple[C, ...], Tuple[B, ...])
assert not issubclass(Tuple[C, ...], Tuple[B]) self.assertNotIsSubclass(Tuple[C, ...], Tuple[B])
assert not issubclass(Tuple[C], Tuple[B, ...]) self.assertNotIsSubclass(Tuple[C], Tuple[B, ...])
def test_repr(self): def test_repr(self):
self.assertEqual(repr(Tuple), 'typing.Tuple') self.assertEqual(repr(Tuple), 'typing.Tuple')
...@@ -402,7 +419,7 @@ class TupleTests(TestCase): ...@@ -402,7 +419,7 @@ class TupleTests(TestCase):
issubclass(42, Tuple[int]) issubclass(42, Tuple[int])
class CallableTests(TestCase): class CallableTests(BaseTestCase):
def test_self_subclass(self): def test_self_subclass(self):
self.assertTrue(issubclass(Callable[[int], int], Callable)) self.assertTrue(issubclass(Callable[[int], int], Callable))
...@@ -447,20 +464,20 @@ class CallableTests(TestCase): ...@@ -447,20 +464,20 @@ class CallableTests(TestCase):
def test_callable_instance_works(self): def test_callable_instance_works(self):
def f(): def f():
pass pass
assert isinstance(f, Callable) self.assertIsInstance(f, Callable)
assert not isinstance(None, Callable) self.assertNotIsInstance(None, Callable)
def test_callable_instance_type_error(self): def test_callable_instance_type_error(self):
def f(): def f():
pass pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
assert isinstance(f, Callable[[], None]) self.assertIsInstance(f, Callable[[], None])
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
assert isinstance(f, Callable[[], Any]) self.assertIsInstance(f, Callable[[], Any])
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
assert not isinstance(None, Callable[[], None]) self.assertNotIsInstance(None, Callable[[], None])
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
assert not isinstance(None, Callable[[], Any]) self.assertNotIsInstance(None, Callable[[], Any])
def test_repr(self): def test_repr(self):
ct0 = Callable[[], bool] ct0 = Callable[[], bool]
...@@ -513,15 +530,15 @@ class MySimpleMapping(SimpleMapping[XK, XV]): ...@@ -513,15 +530,15 @@ class MySimpleMapping(SimpleMapping[XK, XV]):
return default return default
class ProtocolTests(TestCase): class ProtocolTests(BaseTestCase):
def test_supports_int(self): def test_supports_int(self):
assert issubclass(int, typing.SupportsInt) self.assertIsSubclass(int, typing.SupportsInt)
assert not issubclass(str, typing.SupportsInt) self.assertNotIsSubclass(str, typing.SupportsInt)
def test_supports_float(self): def test_supports_float(self):
assert issubclass(float, typing.SupportsFloat) self.assertIsSubclass(float, typing.SupportsFloat)
assert not issubclass(str, typing.SupportsFloat) self.assertNotIsSubclass(str, typing.SupportsFloat)
def test_supports_complex(self): def test_supports_complex(self):
...@@ -530,8 +547,8 @@ class ProtocolTests(TestCase): ...@@ -530,8 +547,8 @@ class ProtocolTests(TestCase):
def __complex__(self): def __complex__(self):
return 0j return 0j
assert issubclass(C, typing.SupportsComplex) self.assertIsSubclass(C, typing.SupportsComplex)
assert not issubclass(str, typing.SupportsComplex) self.assertNotIsSubclass(str, typing.SupportsComplex)
def test_supports_bytes(self): def test_supports_bytes(self):
...@@ -540,40 +557,40 @@ class ProtocolTests(TestCase): ...@@ -540,40 +557,40 @@ class ProtocolTests(TestCase):
def __bytes__(self): def __bytes__(self):
return b'' return b''
assert issubclass(B, typing.SupportsBytes) self.assertIsSubclass(B, typing.SupportsBytes)
assert not issubclass(str, typing.SupportsBytes) self.assertNotIsSubclass(str, typing.SupportsBytes)
def test_supports_abs(self): def test_supports_abs(self):
assert issubclass(float, typing.SupportsAbs) self.assertIsSubclass(float, typing.SupportsAbs)
assert issubclass(int, typing.SupportsAbs) self.assertIsSubclass(int, typing.SupportsAbs)
assert not issubclass(str, typing.SupportsAbs) self.assertNotIsSubclass(str, typing.SupportsAbs)
def test_supports_round(self): def test_supports_round(self):
issubclass(float, typing.SupportsRound) issubclass(float, typing.SupportsRound)
assert issubclass(float, typing.SupportsRound) self.assertIsSubclass(float, typing.SupportsRound)
assert issubclass(int, typing.SupportsRound) self.assertIsSubclass(int, typing.SupportsRound)
assert not issubclass(str, typing.SupportsRound) self.assertNotIsSubclass(str, typing.SupportsRound)
def test_reversible(self): def test_reversible(self):
assert issubclass(list, typing.Reversible) self.assertIsSubclass(list, typing.Reversible)
assert not issubclass(int, typing.Reversible) self.assertNotIsSubclass(int, typing.Reversible)
def test_protocol_instance_type_error(self): def test_protocol_instance_type_error(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
isinstance(0, typing.SupportsAbs) isinstance(0, typing.SupportsAbs)
class GenericTests(TestCase): class GenericTests(BaseTestCase):
def test_basics(self): def test_basics(self):
X = SimpleMapping[str, Any] X = SimpleMapping[str, Any]
assert X.__parameters__ == () self.assertEqual(X.__parameters__, ())
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
X[str] X[str]
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
X[str, str] X[str, str]
Y = SimpleMapping[XK, str] Y = SimpleMapping[XK, str]
assert Y.__parameters__ == (XK,) self.assertEqual(Y.__parameters__, (XK,))
Y[str] Y[str]
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
Y[str, str] Y[str, str]
...@@ -600,21 +617,21 @@ class GenericTests(TestCase): ...@@ -600,21 +617,21 @@ class GenericTests(TestCase):
pass pass
X = C[Tuple[S, T]] X = C[Tuple[S, T]]
assert X == C[Tuple[S, T]] self.assertEqual(X, C[Tuple[S, T]])
assert X != C[Tuple[T, S]] self.assertNotEqual(X, C[Tuple[T, S]])
Y = X[T, int] Y = X[T, int]
assert Y == X[T, int] self.assertEqual(Y, X[T, int])
assert Y != X[S, int] self.assertNotEqual(Y, X[S, int])
assert Y != X[T, str] self.assertNotEqual(Y, X[T, str])
Z = Y[str] Z = Y[str]
assert Z == Y[str] self.assertEqual(Z, Y[str])
assert Z != Y[int] self.assertNotEqual(Z, Y[int])
assert Z != Y[T] self.assertNotEqual(Z, Y[T])
assert str(Z).endswith( self.assertTrue(str(Z).endswith(
'.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]') '.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]'))
def test_dict(self): def test_dict(self):
T = TypeVar('T') T = TypeVar('T')
...@@ -666,28 +683,30 @@ class GenericTests(TestCase): ...@@ -666,28 +683,30 @@ class GenericTests(TestCase):
class C(Generic[T]): class C(Generic[T]):
pass pass
assert C.__module__ == __name__ self.assertEqual(C.__module__, __name__)
if not PY32: if not PY32:
assert C.__qualname__ == 'GenericTests.test_repr_2.<locals>.C' self.assertEqual(C.__qualname__,
assert repr(C).split('.')[-1] == 'C<~T>' 'GenericTests.test_repr_2.<locals>.C')
self.assertEqual(repr(C).split('.')[-1], 'C<~T>')
X = C[int] X = C[int]
assert X.__module__ == __name__ self.assertEqual(X.__module__, __name__)
if not PY32: if not PY32:
assert X.__qualname__ == 'C' self.assertEqual(X.__qualname__, 'C')
assert repr(X).split('.')[-1] == 'C<~T>[int]' self.assertEqual(repr(X).split('.')[-1], 'C<~T>[int]')
class Y(C[int]): class Y(C[int]):
pass pass
assert Y.__module__ == __name__ self.assertEqual(Y.__module__, __name__)
if not PY32: if not PY32:
assert Y.__qualname__ == 'GenericTests.test_repr_2.<locals>.Y' self.assertEqual(Y.__qualname__,
assert repr(Y).split('.')[-1] == 'Y' 'GenericTests.test_repr_2.<locals>.Y')
self.assertEqual(repr(Y).split('.')[-1], 'Y')
def test_eq_1(self): def test_eq_1(self):
assert Generic == Generic self.assertEqual(Generic, Generic)
assert Generic[T] == Generic[T] self.assertEqual(Generic[T], Generic[T])
assert Generic[KT] != Generic[VT] self.assertNotEqual(Generic[KT], Generic[VT])
def test_eq_2(self): def test_eq_2(self):
...@@ -697,10 +716,10 @@ class GenericTests(TestCase): ...@@ -697,10 +716,10 @@ class GenericTests(TestCase):
class B(Generic[T]): class B(Generic[T]):
pass pass
assert A == A self.assertEqual(A, A)
assert A != B self.assertNotEqual(A, B)
assert A[T] == A[T] self.assertEqual(A[T], A[T])
assert A[T] != B[T] self.assertNotEqual(A[T], B[T])
def test_multiple_inheritance(self): def test_multiple_inheritance(self):
...@@ -713,7 +732,7 @@ class GenericTests(TestCase): ...@@ -713,7 +732,7 @@ class GenericTests(TestCase):
class C(A[T, VT], Generic[VT, T, KT], B[KT, T]): class C(A[T, VT], Generic[VT, T, KT], B[KT, T]):
pass pass
assert C.__parameters__ == (VT, T, KT) self.assertEqual(C.__parameters__, (VT, T, KT))
def test_nested(self): def test_nested(self):
...@@ -743,7 +762,7 @@ class GenericTests(TestCase): ...@@ -743,7 +762,7 @@ class GenericTests(TestCase):
a.set([]) a.set([])
a.append(1) a.append(1)
a.append(42) a.append(42)
assert a.get() == [1, 42] self.assertEqual(a.get(), [1, 42])
def test_type_erasure(self): def test_type_erasure(self):
T = TypeVar('T') T = TypeVar('T')
...@@ -760,12 +779,12 @@ class GenericTests(TestCase): ...@@ -760,12 +779,12 @@ class GenericTests(TestCase):
a = Node(x) a = Node(x)
b = Node[T](x) b = Node[T](x)
c = Node[Any](x) c = Node[Any](x)
assert type(a) is Node self.assertIs(type(a), Node)
assert type(b) is Node self.assertIs(type(b), Node)
assert type(c) is Node self.assertIs(type(c), Node)
assert a.label == x self.assertEqual(a.label, x)
assert b.label == x self.assertEqual(b.label, x)
assert c.label == x self.assertEqual(c.label, x)
foo(42) foo(42)
...@@ -778,7 +797,7 @@ class GenericTests(TestCase): ...@@ -778,7 +797,7 @@ class GenericTests(TestCase):
class D(C): class D(C):
pass pass
assert D.__parameters__ == () self.assertEqual(D.__parameters__, ())
with self.assertRaises(Exception): with self.assertRaises(Exception):
D[int] D[int]
...@@ -788,61 +807,63 @@ class GenericTests(TestCase): ...@@ -788,61 +807,63 @@ class GenericTests(TestCase):
D[T] D[T]
class VarianceTests(TestCase): class VarianceTests(BaseTestCase):
def test_invariance(self): def test_invariance(self):
# Because of invariance, List[subclass of X] is not a subclass # Because of invariance, List[subclass of X] is not a subclass
# of List[X], and ditto for MutableSequence. # of List[X], and ditto for MutableSequence.
assert not issubclass(typing.List[Manager], typing.List[Employee]) self.assertNotIsSubclass(typing.List[Manager], typing.List[Employee])
assert not issubclass(typing.MutableSequence[Manager], self.assertNotIsSubclass(typing.MutableSequence[Manager],
typing.MutableSequence[Employee]) typing.MutableSequence[Employee])
# It's still reflexive. # It's still reflexive.
assert issubclass(typing.List[Employee], typing.List[Employee]) self.assertIsSubclass(typing.List[Employee], typing.List[Employee])
assert issubclass(typing.MutableSequence[Employee], self.assertIsSubclass(typing.MutableSequence[Employee],
typing.MutableSequence[Employee]) typing.MutableSequence[Employee])
def test_covariance_tuple(self): def test_covariance_tuple(self):
# Check covariace for Tuple (which are really special cases). # Check covariace for Tuple (which are really special cases).
assert issubclass(Tuple[Manager], Tuple[Employee]) self.assertIsSubclass(Tuple[Manager], Tuple[Employee])
assert not issubclass(Tuple[Employee], Tuple[Manager]) self.assertNotIsSubclass(Tuple[Employee], Tuple[Manager])
# And pairwise. # And pairwise.
assert issubclass(Tuple[Manager, Manager], Tuple[Employee, Employee]) self.assertIsSubclass(Tuple[Manager, Manager],
assert not issubclass(Tuple[Employee, Employee], Tuple[Employee, Employee])
self.assertNotIsSubclass(Tuple[Employee, Employee],
Tuple[Manager, Employee]) Tuple[Manager, Employee])
# And using ellipsis. # And using ellipsis.
assert issubclass(Tuple[Manager, ...], Tuple[Employee, ...]) self.assertIsSubclass(Tuple[Manager, ...], Tuple[Employee, ...])
assert not issubclass(Tuple[Employee, ...], Tuple[Manager, ...]) self.assertNotIsSubclass(Tuple[Employee, ...], Tuple[Manager, ...])
def test_covariance_sequence(self): def test_covariance_sequence(self):
# Check covariance for Sequence (which is just a generic class # Check covariance for Sequence (which is just a generic class
# for this purpose, but using a covariant type variable). # for this purpose, but using a covariant type variable).
assert issubclass(typing.Sequence[Manager], typing.Sequence[Employee]) self.assertIsSubclass(typing.Sequence[Manager],
assert not issubclass(typing.Sequence[Employee], typing.Sequence[Employee])
self.assertNotIsSubclass(typing.Sequence[Employee],
typing.Sequence[Manager]) typing.Sequence[Manager])
def test_covariance_mapping(self): def test_covariance_mapping(self):
# Ditto for Mapping (covariant in the value, invariant in the key). # 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]) typing.Mapping[Employee, Employee])
assert not issubclass(typing.Mapping[Manager, Employee], self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Employee, Employee]) typing.Mapping[Employee, Employee])
assert not issubclass(typing.Mapping[Employee, Manager], self.assertNotIsSubclass(typing.Mapping[Employee, Manager],
typing.Mapping[Manager, Manager]) typing.Mapping[Manager, Manager])
assert not issubclass(typing.Mapping[Manager, Employee], self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Manager, Manager]) typing.Mapping[Manager, Manager])
class CastTests(TestCase): class CastTests(BaseTestCase):
def test_basics(self): def test_basics(self):
assert cast(int, 42) == 42 self.assertEqual(cast(int, 42), 42)
assert cast(float, 42) == 42 self.assertEqual(cast(float, 42), 42)
assert type(cast(float, 42)) is int self.assertIs(type(cast(float, 42)), int)
assert cast(Any, 42) == 42 self.assertEqual(cast(Any, 42), 42)
assert cast(list, 42) == 42 self.assertEqual(cast(list, 42), 42)
assert cast(Union[str, float], 42) == 42 self.assertEqual(cast(Union[str, float], 42), 42)
assert cast(AnyStr, 42) == 42 self.assertEqual(cast(AnyStr, 42), 42)
assert cast(None, 42) == 42 self.assertEqual(cast(None, 42), 42)
def test_errors(self): def test_errors(self):
# Bogus calls are not expected to fail. # Bogus calls are not expected to fail.
...@@ -850,7 +871,7 @@ class CastTests(TestCase): ...@@ -850,7 +871,7 @@ class CastTests(TestCase):
cast('hello', 42) cast('hello', 42)
class ForwardRefTests(TestCase): class ForwardRefTests(BaseTestCase):
def test_basics(self): def test_basics(self):
...@@ -876,15 +897,17 @@ class ForwardRefTests(TestCase): ...@@ -876,15 +897,17 @@ class ForwardRefTests(TestCase):
t = Node[int] t = Node[int]
both_hints = get_type_hints(t.add_both, globals(), locals()) both_hints = get_type_hints(t.add_both, globals(), locals())
assert both_hints['left'] == both_hints['right'] == Optional[Node[T]] self.assertEqual(both_hints['left'], Optional[Node[T]])
assert both_hints['stuff'] == Optional[int] self.assertEqual(both_hints['right'], Optional[Node[T]])
assert 'blah' not in both_hints self.assertEqual(both_hints['left'], both_hints['right'])
self.assertEqual(both_hints['stuff'], Optional[int])
self.assertNotIn('blah', both_hints)
left_hints = get_type_hints(t.add_left, globals(), locals()) left_hints = get_type_hints(t.add_left, globals(), locals())
assert left_hints['node'] == Optional[Node[T]] self.assertEqual(left_hints['node'], Optional[Node[T]])
right_hints = get_type_hints(t.add_right, globals(), locals()) right_hints = get_type_hints(t.add_right, globals(), locals())
assert right_hints['node'] == Optional[Node[T]] self.assertEqual(right_hints['node'], Optional[Node[T]])
def test_forwardref_instance_type_error(self): def test_forwardref_instance_type_error(self):
fr = typing._ForwardRef('int') fr = typing._ForwardRef('int')
...@@ -1007,10 +1030,10 @@ class ForwardRefTests(TestCase): ...@@ -1007,10 +1030,10 @@ class ForwardRefTests(TestCase):
ns = {} ns = {}
exec(code, ns) exec(code, ns)
hints = get_type_hints(ns['C'].foo) hints = get_type_hints(ns['C'].foo)
assert hints == {'a': ns['C'], 'return': ns['D']} self.assertEqual(hints, {'a': ns['C'], 'return': ns['D']})
class OverloadTests(TestCase): class OverloadTests(BaseTestCase):
def test_overload_exists(self): def test_overload_exists(self):
from typing import overload from typing import overload
...@@ -1076,29 +1099,29 @@ if PY35: ...@@ -1076,29 +1099,29 @@ if PY35:
exec(PY35_TESTS) exec(PY35_TESTS)
class CollectionsAbcTests(TestCase): class CollectionsAbcTests(BaseTestCase):
def test_hashable(self): def test_hashable(self):
assert isinstance(42, typing.Hashable) self.assertIsInstance(42, typing.Hashable)
assert not isinstance([], typing.Hashable) self.assertNotIsInstance([], typing.Hashable)
def test_iterable(self): def test_iterable(self):
assert isinstance([], typing.Iterable) self.assertIsInstance([], typing.Iterable)
# Due to ABC caching, the second time takes a separate code # Due to ABC caching, the second time takes a separate code
# path and could fail. So call this a few times. # path and could fail. So call this a few times.
assert isinstance([], typing.Iterable) self.assertIsInstance([], typing.Iterable)
assert isinstance([], typing.Iterable) self.assertIsInstance([], typing.Iterable)
assert isinstance([], typing.Iterable[int]) self.assertIsInstance([], typing.Iterable[int])
assert not isinstance(42, typing.Iterable) self.assertNotIsInstance(42, typing.Iterable)
# Just in case, also test issubclass() a few times. # Just in case, also test issubclass() a few times.
assert issubclass(list, typing.Iterable) self.assertIsSubclass(list, typing.Iterable)
assert issubclass(list, typing.Iterable) self.assertIsSubclass(list, typing.Iterable)
def test_iterator(self): def test_iterator(self):
it = iter([]) it = iter([])
assert isinstance(it, typing.Iterator) self.assertIsInstance(it, typing.Iterator)
assert isinstance(it, typing.Iterator[int]) self.assertIsInstance(it, typing.Iterator[int])
assert not isinstance(42, typing.Iterator) self.assertNotIsInstance(42, typing.Iterator)
@skipUnless(PY35, 'Python 3.5 required') @skipUnless(PY35, 'Python 3.5 required')
def test_awaitable(self): def test_awaitable(self):
...@@ -1109,12 +1132,12 @@ class CollectionsAbcTests(TestCase): ...@@ -1109,12 +1132,12 @@ class CollectionsAbcTests(TestCase):
globals(), ns) globals(), ns)
foo = ns['foo'] foo = ns['foo']
g = foo() g = foo()
assert issubclass(type(g), typing.Awaitable[int]) self.assertIsSubclass(type(g), typing.Awaitable[int])
assert isinstance(g, typing.Awaitable) self.assertIsInstance(g, typing.Awaitable)
assert not isinstance(foo, typing.Awaitable) self.assertNotIsInstance(foo, typing.Awaitable)
assert issubclass(typing.Awaitable[Manager], self.assertIsSubclass(typing.Awaitable[Manager],
typing.Awaitable[Employee]) typing.Awaitable[Employee])
assert not issubclass(typing.Awaitable[Employee], self.assertNotIsSubclass(typing.Awaitable[Employee],
typing.Awaitable[Manager]) typing.Awaitable[Manager])
g.send(None) # Run foo() till completion, to avoid warning. g.send(None) # Run foo() till completion, to avoid warning.
...@@ -1122,70 +1145,70 @@ class CollectionsAbcTests(TestCase): ...@@ -1122,70 +1145,70 @@ class CollectionsAbcTests(TestCase):
def test_async_iterable(self): def test_async_iterable(self):
base_it = range(10) # type: Iterator[int] base_it = range(10) # type: Iterator[int]
it = AsyncIteratorWrapper(base_it) it = AsyncIteratorWrapper(base_it)
assert isinstance(it, typing.AsyncIterable) self.assertIsInstance(it, typing.AsyncIterable)
assert isinstance(it, typing.AsyncIterable) self.assertIsInstance(it, typing.AsyncIterable)
assert issubclass(typing.AsyncIterable[Manager], self.assertIsSubclass(typing.AsyncIterable[Manager],
typing.AsyncIterable[Employee]) typing.AsyncIterable[Employee])
assert not isinstance(42, typing.AsyncIterable) self.assertNotIsInstance(42, typing.AsyncIterable)
@skipUnless(PY35, 'Python 3.5 required') @skipUnless(PY35, 'Python 3.5 required')
def test_async_iterator(self): def test_async_iterator(self):
base_it = range(10) # type: Iterator[int] base_it = range(10) # type: Iterator[int]
it = AsyncIteratorWrapper(base_it) it = AsyncIteratorWrapper(base_it)
assert isinstance(it, typing.AsyncIterator) self.assertIsInstance(it, typing.AsyncIterator)
assert issubclass(typing.AsyncIterator[Manager], self.assertIsSubclass(typing.AsyncIterator[Manager],
typing.AsyncIterator[Employee]) typing.AsyncIterator[Employee])
assert not isinstance(42, typing.AsyncIterator) self.assertNotIsInstance(42, typing.AsyncIterator)
def test_sized(self): def test_sized(self):
assert isinstance([], typing.Sized) self.assertIsInstance([], typing.Sized)
assert not isinstance(42, typing.Sized) self.assertNotIsInstance(42, typing.Sized)
def test_container(self): def test_container(self):
assert isinstance([], typing.Container) self.assertIsInstance([], typing.Container)
assert not isinstance(42, typing.Container) self.assertNotIsInstance(42, typing.Container)
def test_abstractset(self): def test_abstractset(self):
assert isinstance(set(), typing.AbstractSet) self.assertIsInstance(set(), typing.AbstractSet)
assert not isinstance(42, typing.AbstractSet) self.assertNotIsInstance(42, typing.AbstractSet)
def test_mutableset(self): def test_mutableset(self):
assert isinstance(set(), typing.MutableSet) self.assertIsInstance(set(), typing.MutableSet)
assert not isinstance(frozenset(), typing.MutableSet) self.assertNotIsInstance(frozenset(), typing.MutableSet)
def test_mapping(self): def test_mapping(self):
assert isinstance({}, typing.Mapping) self.assertIsInstance({}, typing.Mapping)
assert not isinstance(42, typing.Mapping) self.assertNotIsInstance(42, typing.Mapping)
def test_mutablemapping(self): def test_mutablemapping(self):
assert isinstance({}, typing.MutableMapping) self.assertIsInstance({}, typing.MutableMapping)
assert not isinstance(42, typing.MutableMapping) self.assertNotIsInstance(42, typing.MutableMapping)
def test_sequence(self): def test_sequence(self):
assert isinstance([], typing.Sequence) self.assertIsInstance([], typing.Sequence)
assert not isinstance(42, typing.Sequence) self.assertNotIsInstance(42, typing.Sequence)
def test_mutablesequence(self): def test_mutablesequence(self):
assert isinstance([], typing.MutableSequence) self.assertIsInstance([], typing.MutableSequence)
assert not isinstance((), typing.MutableSequence) self.assertNotIsInstance((), typing.MutableSequence)
def test_bytestring(self): def test_bytestring(self):
assert isinstance(b'', typing.ByteString) self.assertIsInstance(b'', typing.ByteString)
assert isinstance(bytearray(b''), typing.ByteString) self.assertIsInstance(bytearray(b''), typing.ByteString)
def test_list(self): def test_list(self):
assert issubclass(list, typing.List) self.assertIsSubclass(list, typing.List)
def test_set(self): def test_set(self):
assert issubclass(set, typing.Set) self.assertIsSubclass(set, typing.Set)
assert not issubclass(frozenset, typing.Set) self.assertNotIsSubclass(frozenset, typing.Set)
def test_frozenset(self): def test_frozenset(self):
assert issubclass(frozenset, typing.FrozenSet) self.assertIsSubclass(frozenset, typing.FrozenSet)
assert not issubclass(set, typing.FrozenSet) self.assertNotIsSubclass(set, typing.FrozenSet)
def test_dict(self): def test_dict(self):
assert issubclass(dict, typing.Dict) self.assertIsSubclass(dict, typing.Dict)
def test_no_list_instantiation(self): def test_no_list_instantiation(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -1201,7 +1224,7 @@ class CollectionsAbcTests(TestCase): ...@@ -1201,7 +1224,7 @@ class CollectionsAbcTests(TestCase):
pass pass
a = MyList() a = MyList()
assert isinstance(a, MyList) self.assertIsInstance(a, MyList)
def test_no_dict_instantiation(self): def test_no_dict_instantiation(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -1217,7 +1240,7 @@ class CollectionsAbcTests(TestCase): ...@@ -1217,7 +1240,7 @@ class CollectionsAbcTests(TestCase):
pass pass
d = MyDict() d = MyDict()
assert isinstance(d, MyDict) self.assertIsInstance(d, MyDict)
def test_no_defaultdict_instantiation(self): def test_no_defaultdict_instantiation(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -1233,7 +1256,7 @@ class CollectionsAbcTests(TestCase): ...@@ -1233,7 +1256,7 @@ class CollectionsAbcTests(TestCase):
pass pass
dd = MyDefDict() dd = MyDefDict()
assert isinstance(dd, MyDefDict) self.assertIsInstance(dd, MyDefDict)
def test_no_set_instantiation(self): def test_no_set_instantiation(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -1249,7 +1272,7 @@ class CollectionsAbcTests(TestCase): ...@@ -1249,7 +1272,7 @@ class CollectionsAbcTests(TestCase):
pass pass
d = MySet() d = MySet()
assert isinstance(d, MySet) self.assertIsInstance(d, MySet)
def test_no_frozenset_instantiation(self): def test_no_frozenset_instantiation(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -1265,7 +1288,7 @@ class CollectionsAbcTests(TestCase): ...@@ -1265,7 +1288,7 @@ class CollectionsAbcTests(TestCase):
pass pass
d = MyFrozenSet() d = MyFrozenSet()
assert isinstance(d, MyFrozenSet) self.assertIsInstance(d, MyFrozenSet)
def test_no_tuple_instantiation(self): def test_no_tuple_instantiation(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -1279,10 +1302,10 @@ class CollectionsAbcTests(TestCase): ...@@ -1279,10 +1302,10 @@ class CollectionsAbcTests(TestCase):
def foo(): def foo():
yield 42 yield 42
g = foo() g = foo()
assert issubclass(type(g), typing.Generator) self.assertIsSubclass(type(g), typing.Generator)
assert issubclass(typing.Generator[Manager, Employee, Manager], self.assertIsSubclass(typing.Generator[Manager, Employee, Manager],
typing.Generator[Employee, Manager, Employee]) 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]) typing.Generator[Employee, Employee, Employee])
def test_no_generator_instantiation(self): def test_no_generator_instantiation(self):
...@@ -1305,18 +1328,18 @@ class CollectionsAbcTests(TestCase): ...@@ -1305,18 +1328,18 @@ class CollectionsAbcTests(TestCase):
def __len__(self): def __len__(self):
return 0 return 0
assert len(MMC()) == 0 self.assertEqual(len(MMC()), 0)
class MMB(typing.MutableMapping[KT, VT]): class MMB(typing.MutableMapping[KT, VT]):
def __len__(self): def __len__(self):
return 0 return 0
assert len(MMB()) == 0 self.assertEqual(len(MMB()), 0)
assert len(MMB[str, str]()) == 0 self.assertEqual(len(MMB[str, str]()), 0)
assert len(MMB[KT, VT]()) == 0 self.assertEqual(len(MMB[KT, VT]()), 0)
class OtherABCTests(TestCase): class OtherABCTests(BaseTestCase):
@skipUnless(hasattr(typing, 'ContextManager'), @skipUnless(hasattr(typing, 'ContextManager'),
'requires typing.ContextManager') 'requires typing.ContextManager')
...@@ -1326,27 +1349,27 @@ class OtherABCTests(TestCase): ...@@ -1326,27 +1349,27 @@ class OtherABCTests(TestCase):
yield 42 yield 42
cm = manager() cm = manager()
assert isinstance(cm, typing.ContextManager) self.assertIsInstance(cm, typing.ContextManager)
assert isinstance(cm, typing.ContextManager[int]) self.assertIsInstance(cm, typing.ContextManager[int])
assert not isinstance(42, typing.ContextManager) self.assertNotIsInstance(42, typing.ContextManager)
class NamedTupleTests(TestCase): class NamedTupleTests(BaseTestCase):
def test_basics(self): def test_basics(self):
Emp = NamedTuple('Emp', [('name', str), ('id', int)]) Emp = NamedTuple('Emp', [('name', str), ('id', int)])
assert issubclass(Emp, tuple) self.assertIsSubclass(Emp, tuple)
joe = Emp('Joe', 42) joe = Emp('Joe', 42)
jim = Emp(name='Jim', id=1) jim = Emp(name='Jim', id=1)
assert isinstance(joe, Emp) self.assertIsInstance(joe, Emp)
assert isinstance(joe, tuple) self.assertIsInstance(joe, tuple)
assert joe.name == 'Joe' self.assertEqual(joe.name, 'Joe')
assert joe.id == 42 self.assertEqual(joe.id, 42)
assert jim.name == 'Jim' self.assertEqual(jim.name, 'Jim')
assert jim.id == 1 self.assertEqual(jim.id, 1)
assert Emp.__name__ == 'Emp' self.assertEqual(Emp.__name__, 'Emp')
assert Emp._fields == ('name', 'id') self.assertEqual(Emp._fields, ('name', 'id'))
assert Emp._field_types == dict(name=str, id=int) self.assertEqual(Emp._field_types, dict(name=str, id=int))
def test_pickle(self): def test_pickle(self):
global Emp # pickle wants to reference the class by name global Emp # pickle wants to reference the class by name
...@@ -1358,7 +1381,7 @@ class NamedTupleTests(TestCase): ...@@ -1358,7 +1381,7 @@ class NamedTupleTests(TestCase):
self.assertEqual(jane2, jane) self.assertEqual(jane2, jane)
class IOTests(TestCase): class IOTests(BaseTestCase):
def test_io(self): def test_io(self):
...@@ -1366,7 +1389,7 @@ class IOTests(TestCase): ...@@ -1366,7 +1389,7 @@ class IOTests(TestCase):
return a.readline() return a.readline()
a = stuff.__annotations__['a'] a = stuff.__annotations__['a']
assert a.__parameters__ == (AnyStr,) self.assertEqual(a.__parameters__, (AnyStr,))
def test_textio(self): def test_textio(self):
...@@ -1374,7 +1397,7 @@ class IOTests(TestCase): ...@@ -1374,7 +1397,7 @@ class IOTests(TestCase):
return a.readline() return a.readline()
a = stuff.__annotations__['a'] a = stuff.__annotations__['a']
assert a.__parameters__ == () self.assertEqual(a.__parameters__, ())
def test_binaryio(self): def test_binaryio(self):
...@@ -1382,40 +1405,40 @@ class IOTests(TestCase): ...@@ -1382,40 +1405,40 @@ class IOTests(TestCase):
return a.readline() return a.readline()
a = stuff.__annotations__['a'] a = stuff.__annotations__['a']
assert a.__parameters__ == () self.assertEqual(a.__parameters__, ())
def test_io_submodule(self): def test_io_submodule(self):
from typing.io import IO, TextIO, BinaryIO, __all__, __name__ from typing.io import IO, TextIO, BinaryIO, __all__, __name__
assert IO is typing.IO self.assertIs(IO, typing.IO)
assert TextIO is typing.TextIO self.assertIs(TextIO, typing.TextIO)
assert BinaryIO is typing.BinaryIO self.assertIs(BinaryIO, typing.BinaryIO)
assert set(__all__) == set(['IO', 'TextIO', 'BinaryIO']) self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO']))
assert __name__ == 'typing.io' self.assertEqual(__name__, 'typing.io')
class RETests(TestCase): class RETests(BaseTestCase):
# Much of this is really testing _TypeAlias. # Much of this is really testing _TypeAlias.
def test_basics(self): def test_basics(self):
pat = re.compile('[a-z]+', re.I) pat = re.compile('[a-z]+', re.I)
assert issubclass(pat.__class__, Pattern) self.assertIsSubclass(pat.__class__, Pattern)
assert issubclass(type(pat), Pattern) self.assertIsSubclass(type(pat), Pattern)
assert issubclass(type(pat), Pattern[str]) self.assertIsSubclass(type(pat), Pattern[str])
mat = pat.search('12345abcde.....') mat = pat.search('12345abcde.....')
assert issubclass(mat.__class__, Match) self.assertIsSubclass(mat.__class__, Match)
assert issubclass(mat.__class__, Match[str]) self.assertIsSubclass(mat.__class__, Match[str])
assert issubclass(mat.__class__, Match[bytes]) # Sad but true. self.assertIsSubclass(mat.__class__, Match[bytes]) # Sad but true.
assert issubclass(type(mat), Match) self.assertIsSubclass(type(mat), Match)
assert issubclass(type(mat), Match[str]) self.assertIsSubclass(type(mat), Match[str])
p = Pattern[Union[str, bytes]] p = Pattern[Union[str, bytes]]
assert issubclass(Pattern[str], Pattern) self.assertIsSubclass(Pattern[str], Pattern)
assert issubclass(Pattern[str], p) self.assertIsSubclass(Pattern[str], p)
m = Match[Union[bytes, str]] m = Match[Union[bytes, str]]
assert issubclass(Match[bytes], Match) self.assertIsSubclass(Match[bytes], Match)
assert issubclass(Match[bytes], m) self.assertIsSubclass(Match[bytes], m)
def test_errors(self): def test_errors(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
...@@ -1436,19 +1459,19 @@ class RETests(TestCase): ...@@ -1436,19 +1459,19 @@ class RETests(TestCase):
isinstance(42, Pattern[str]) isinstance(42, Pattern[str])
def test_repr(self): def test_repr(self):
assert repr(Pattern) == 'Pattern[~AnyStr]' self.assertEqual(repr(Pattern), 'Pattern[~AnyStr]')
assert repr(Pattern[str]) == 'Pattern[str]' self.assertEqual(repr(Pattern[str]), 'Pattern[str]')
assert repr(Pattern[bytes]) == 'Pattern[bytes]' self.assertEqual(repr(Pattern[bytes]), 'Pattern[bytes]')
assert repr(Match) == 'Match[~AnyStr]' self.assertEqual(repr(Match), 'Match[~AnyStr]')
assert repr(Match[str]) == 'Match[str]' self.assertEqual(repr(Match[str]), 'Match[str]')
assert repr(Match[bytes]) == 'Match[bytes]' self.assertEqual(repr(Match[bytes]), 'Match[bytes]')
def test_re_submodule(self): def test_re_submodule(self):
from typing.re import Match, Pattern, __all__, __name__ from typing.re import Match, Pattern, __all__, __name__
assert Match is typing.Match self.assertIs(Match, typing.Match)
assert Pattern is typing.Pattern self.assertIs(Pattern, typing.Pattern)
assert set(__all__) == set(['Match', 'Pattern']) self.assertEqual(set(__all__), set(['Match', 'Pattern']))
assert __name__ == 'typing.re' self.assertEqual(__name__, 'typing.re')
def test_cannot_subclass(self): def test_cannot_subclass(self):
with self.assertRaises(TypeError) as ex: with self.assertRaises(TypeError) as ex:
...@@ -1456,29 +1479,30 @@ class RETests(TestCase): ...@@ -1456,29 +1479,30 @@ class RETests(TestCase):
class A(typing.Match): class A(typing.Match):
pass 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__.""" """Tests for __all__."""
def test_all(self): def test_all(self):
from typing import __all__ as a from typing import __all__ as a
# Just spot-check the first and last of every category. # Just spot-check the first and last of every category.
assert 'AbstractSet' in a self.assertIn('AbstractSet', a)
assert 'ValuesView' in a self.assertIn('ValuesView', a)
assert 'cast' in a self.assertIn('cast', a)
assert 'overload' in a self.assertIn('overload', a)
if hasattr(contextlib, 'AbstractContextManager'): if hasattr(contextlib, 'AbstractContextManager'):
assert 'ContextManager' in a self.assertIn('ContextManager', a)
# Check that io and re are not exported. # Check that io and re are not exported.
assert 'io' not in a self.assertNotIn('io', a)
assert 're' not in a self.assertNotIn('re', a)
# Spot-check that stdlib modules aren't exported. # Spot-check that stdlib modules aren't exported.
assert 'os' not in a self.assertNotIn('os', a)
assert 'sys' not in a self.assertNotIn('sys', a)
# Check that Text is defined. # Check that Text is defined.
assert 'Text' in a self.assertIn('Text', a)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment