Skip to content

Commit 7d73596

Browse files
committed
Started python#136
1 parent 56e6d65 commit 7d73596

2 files changed

Lines changed: 53 additions & 161 deletions

File tree

‎src/test_typing.py‎

Lines changed: 13 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,11 @@ def test_any_instance_type_error(self):
6262
with self.assertRaises(TypeError):
6363
isinstance(42, Any)
6464

65-
def test_any_subclass(self):
66-
self.assertTrue(issubclass(Employee, Any))
67-
self.assertTrue(issubclass(int, Any))
68-
self.assertTrue(issubclass(type(None), Any))
69-
self.assertTrue(issubclass(object, Any))
70-
71-
def test_others_any(self):
72-
self.assertFalse(issubclass(Any, Employee))
73-
self.assertFalse(issubclass(Any, int))
74-
self.assertFalse(issubclass(Any, type(None)))
75-
# However, Any is a subclass of object (this can't be helped).
76-
self.assertTrue(issubclass(Any, object))
65+
def test_any_subclass_type_error(self):
66+
with self.assertRaises(TypeError):
67+
self.assertTrue(issubclass(Employee, Any))
68+
with self.assertRaises(TypeError):
69+
self.assertFalse(issubclass(Any, Employee))
7770

7871
def test_repr(self):
7972
self.assertEqual(repr(Any), 'typing.Any')
@@ -97,23 +90,7 @@ def test_cannot_subscript(self):
9790
with self.assertRaises(TypeError):
9891
Any[int]
9992

100-
def test_any_is_subclass(self):
101-
# Any should be considered a subclass of everything.
102-
self.assertIsSubclass(Any, Any)
103-
self.assertIsSubclass(Any, typing.List)
104-
self.assertIsSubclass(Any, typing.List[int])
105-
self.assertIsSubclass(Any, typing.List[T])
106-
self.assertIsSubclass(Any, typing.Mapping)
107-
self.assertIsSubclass(Any, typing.Mapping[str, int])
108-
self.assertIsSubclass(Any, typing.Mapping[KT, VT])
109-
self.assertIsSubclass(Any, Generic)
110-
self.assertIsSubclass(Any, Generic[T])
111-
self.assertIsSubclass(Any, Generic[KT, VT])
112-
self.assertIsSubclass(Any, AnyStr)
113-
self.assertIsSubclass(Any, Union)
114-
self.assertIsSubclass(Any, Union[int, str])
115-
self.assertIsSubclass(Any, typing.Match)
116-
self.assertIsSubclass(Any, typing.Match[str])
93+
def test_any_works_with_alias(self):
11794
# These expressions must simply not fail.
11895
typing.Match[Any]
11996
typing.Pattern[Any]
@@ -124,13 +101,8 @@ class TypeVarTests(BaseTestCase):
124101

125102
def test_basic_plain(self):
126103
T = TypeVar('T')
127-
# Every class is a subclass of T.
128-
self.assertIsSubclass(int, T)
129-
self.assertIsSubclass(str, T)
130104
# T equals itself.
131105
self.assertEqual(T, T)
132-
# T is a subclass of itself.
133-
self.assertIsSubclass(T, T)
134106
# T is an instance of TypeVar
135107
self.assertIsInstance(T, TypeVar)
136108

@@ -139,16 +111,12 @@ def test_typevar_instance_type_error(self):
139111
with self.assertRaises(TypeError):
140112
isinstance(42, T)
141113

142-
def test_basic_constrained(self):
143-
A = TypeVar('A', str, bytes)
144-
# Only str and bytes are subclasses of A.
145-
self.assertIsSubclass(str, A)
146-
self.assertIsSubclass(bytes, A)
147-
self.assertNotIsSubclass(int, A)
148-
# A equals itself.
149-
self.assertEqual(A, A)
150-
# A is a subclass of itself.
151-
self.assertIsSubclass(A, A)
114+
def test_typevar_subclass_type_error(self):
115+
T = TypeVar('T')
116+
with self.assertRaises(TypeError):
117+
issubclass(int, T)
118+
with self.assertRaises(TypeError):
119+
issubclass(T, int)
152120

153121
def test_constrained_error(self):
154122
with self.assertRaises(TypeError):
@@ -185,19 +153,6 @@ def test_no_redefinition(self):
185153
self.assertNotEqual(TypeVar('T'), TypeVar('T'))
186154
self.assertNotEqual(TypeVar('T', int, str), TypeVar('T', int, str))
187155

188-
def test_subclass_as_unions(self):
189-
# None of these are true -- each type var is its own world.
190-
self.assertFalse(issubclass(TypeVar('T', int, str),
191-
TypeVar('T', int, str)))
192-
self.assertFalse(issubclass(TypeVar('T', int, float),
193-
TypeVar('T', int, float, str)))
194-
self.assertFalse(issubclass(TypeVar('T', int, str),
195-
TypeVar('T', str, int)))
196-
A = TypeVar('A', int, str)
197-
B = TypeVar('B', int, str, float)
198-
self.assertFalse(issubclass(A, B))
199-
self.assertFalse(issubclass(B, A))
200-
201156
def test_cannot_subclass_vars(self):
202157
with self.assertRaises(TypeError):
203158
class V(TypeVar('T')):
@@ -212,12 +167,6 @@ def test_cannot_instantiate_vars(self):
212167
with self.assertRaises(TypeError):
213168
TypeVar('A')()
214169

215-
def test_bound(self):
216-
X = TypeVar('X', bound=Employee)
217-
self.assertIsSubclass(Employee, X)
218-
self.assertIsSubclass(Manager, X)
219-
self.assertNotIsSubclass(int, X)
220-
221170
def test_bound_errors(self):
222171
with self.assertRaises(TypeError):
223172
TypeVar('X', bound=42)
@@ -357,29 +306,10 @@ def Elem(*args):
357306

358307
class TypeVarUnionTests(BaseTestCase):
359308

360-
def test_simpler(self):
361-
A = TypeVar('A', int, str, float)
362-
B = TypeVar('B', int, str)
363-
self.assertIsSubclass(A, A)
364-
self.assertIsSubclass(B, B)
365-
self.assertNotIsSubclass(B, A)
366-
self.assertIsSubclass(A, Union[int, str, float])
367-
self.assertNotIsSubclass(Union[int, str, float], A)
368-
self.assertNotIsSubclass(Union[int, str], B)
369-
self.assertIsSubclass(B, Union[int, str])
370-
self.assertNotIsSubclass(A, B)
371-
self.assertNotIsSubclass(Union[int, str, float], B)
372-
self.assertNotIsSubclass(A, Union[int, str])
373-
374309
def test_var_union_subclass(self):
375310
self.assertTrue(issubclass(T, Union[int, T]))
376311
self.assertTrue(issubclass(KT, Union[KT, VT]))
377312

378-
def test_var_union(self):
379-
TU = TypeVar('TU', Union[int, float], None)
380-
self.assertIsSubclass(int, TU)
381-
self.assertIsSubclass(float, TU)
382-
383313

384314
class TupleTests(BaseTestCase):
385315

@@ -1653,22 +1583,14 @@ def test_basics(self):
16531583
pat = re.compile('[a-z]+', re.I)
16541584
self.assertIsSubclass(pat.__class__, Pattern)
16551585
self.assertIsSubclass(type(pat), Pattern)
1656-
self.assertIsSubclass(type(pat), Pattern[str])
16571586

16581587
mat = pat.search('12345abcde.....')
16591588
self.assertIsSubclass(mat.__class__, Match)
1660-
self.assertIsSubclass(mat.__class__, Match[str])
1661-
self.assertIsSubclass(mat.__class__, Match[bytes]) # Sad but true.
16621589
self.assertIsSubclass(type(mat), Match)
1663-
self.assertIsSubclass(type(mat), Match[str])
16641590

1591+
# these should just work
16651592
p = Pattern[Union[str, bytes]]
1666-
self.assertIsSubclass(Pattern[str], Pattern)
1667-
self.assertIsSubclass(Pattern[str], p)
1668-
16691593
m = Match[Union[bytes, str]]
1670-
self.assertIsSubclass(Match[bytes], Match)
1671-
self.assertIsSubclass(Match[bytes], m)
16721594

16731595
def test_errors(self):
16741596
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)