-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathtestsubtypes.py
More file actions
430 lines (359 loc) · 16.8 KB
/
testsubtypes.py
File metadata and controls
430 lines (359 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from __future__ import annotations
from mypy.nodes import CONTRAVARIANT, COVARIANT, INVARIANT
from mypy.subtypes import is_proper_subtype, is_subtype, restrict_subtype_away
from mypy.test.helpers import Suite
from mypy.test.typefixture import InterfaceTypeFixture, TypeFixture
from mypy.types import Instance, TupleType, Type, UninhabitedType, UnpackType
class SubtypingSuite(Suite):
def setUp(self) -> None:
self.fx = TypeFixture(INVARIANT)
self.fx_contra = TypeFixture(CONTRAVARIANT)
self.fx_co = TypeFixture(COVARIANT)
def test_trivial_cases(self) -> None:
for simple in self.fx_co.a, self.fx_co.o, self.fx_co.b:
self.assert_subtype(simple, simple)
def test_instance_subtyping(self) -> None:
self.assert_strict_subtype(self.fx.a, self.fx.o)
self.assert_strict_subtype(self.fx.b, self.fx.o)
self.assert_strict_subtype(self.fx.b, self.fx.a)
self.assert_not_subtype(self.fx.a, self.fx.d)
self.assert_not_subtype(self.fx.b, self.fx.c)
def test_simple_generic_instance_subtyping_invariant(self) -> None:
self.assert_subtype(self.fx.ga, self.fx.ga)
self.assert_subtype(self.fx.hab, self.fx.hab)
self.assert_not_subtype(self.fx.ga, self.fx.g2a)
self.assert_not_subtype(self.fx.ga, self.fx.gb)
self.assert_not_subtype(self.fx.gb, self.fx.ga)
def test_simple_generic_instance_subtyping_covariant(self) -> None:
self.assert_subtype(self.fx_co.ga, self.fx_co.ga)
self.assert_subtype(self.fx_co.hab, self.fx_co.hab)
self.assert_not_subtype(self.fx_co.ga, self.fx_co.g2a)
self.assert_not_subtype(self.fx_co.ga, self.fx_co.gb)
self.assert_subtype(self.fx_co.gb, self.fx_co.ga)
def test_simple_generic_instance_subtyping_contravariant(self) -> None:
self.assert_subtype(self.fx_contra.ga, self.fx_contra.ga)
self.assert_subtype(self.fx_contra.hab, self.fx_contra.hab)
self.assert_not_subtype(self.fx_contra.ga, self.fx_contra.g2a)
self.assert_subtype(self.fx_contra.ga, self.fx_contra.gb)
self.assert_not_subtype(self.fx_contra.gb, self.fx_contra.ga)
def test_generic_subtyping_with_inheritance_invariant(self) -> None:
self.assert_subtype(self.fx.gsab, self.fx.gb)
self.assert_not_subtype(self.fx.gsab, self.fx.ga)
self.assert_not_subtype(self.fx.gsaa, self.fx.gb)
def test_generic_subtyping_with_inheritance_covariant(self) -> None:
self.assert_subtype(self.fx_co.gsab, self.fx_co.gb)
self.assert_subtype(self.fx_co.gsab, self.fx_co.ga)
self.assert_not_subtype(self.fx_co.gsaa, self.fx_co.gb)
def test_generic_subtyping_with_inheritance_contravariant(self) -> None:
self.assert_subtype(self.fx_contra.gsab, self.fx_contra.gb)
self.assert_not_subtype(self.fx_contra.gsab, self.fx_contra.ga)
self.assert_subtype(self.fx_contra.gsaa, self.fx_contra.gb)
def test_interface_subtyping(self) -> None:
self.assert_subtype(self.fx.e, self.fx.f)
self.assert_equivalent(self.fx.f, self.fx.f)
self.assert_not_subtype(self.fx.a, self.fx.f)
def test_generic_interface_subtyping(self) -> None:
# TODO make this work
fx2 = InterfaceTypeFixture()
self.assert_subtype(fx2.m1, fx2.gfa)
self.assert_not_subtype(fx2.m1, fx2.gfb)
self.assert_equivalent(fx2.gfa, fx2.gfa)
def test_basic_callable_subtyping(self) -> None:
self.assert_strict_subtype(
self.fx.callable(self.fx.o, self.fx.d), self.fx.callable(self.fx.a, self.fx.d)
)
self.assert_strict_subtype(
self.fx.callable(self.fx.d, self.fx.b), self.fx.callable(self.fx.d, self.fx.a)
)
self.assert_strict_subtype(
self.fx.callable(self.fx.a, UninhabitedType()), self.fx.callable(self.fx.a, self.fx.a)
)
self.assert_unrelated(
self.fx.callable(self.fx.a, self.fx.a, self.fx.a),
self.fx.callable(self.fx.a, self.fx.a),
)
def test_default_arg_callable_subtyping(self) -> None:
self.assert_strict_subtype(
self.fx.callable_default(1, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable(self.fx.a, self.fx.d, self.fx.a),
)
self.assert_strict_subtype(
self.fx.callable_default(1, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable(self.fx.a, self.fx.a),
)
self.assert_strict_subtype(
self.fx.callable_default(0, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable_default(1, self.fx.a, self.fx.d, self.fx.a),
)
self.assert_unrelated(
self.fx.callable_default(1, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable(self.fx.d, self.fx.d, self.fx.a),
)
self.assert_unrelated(
self.fx.callable_default(0, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable_default(1, self.fx.a, self.fx.a, self.fx.a),
)
self.assert_unrelated(
self.fx.callable_default(1, self.fx.a, self.fx.a),
self.fx.callable(self.fx.a, self.fx.a, self.fx.a),
)
def test_var_arg_callable_subtyping_1(self) -> None:
self.assert_strict_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.a),
self.fx.callable_var_arg(0, self.fx.b, self.fx.a),
)
def test_var_arg_callable_subtyping_2(self) -> None:
self.assert_strict_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.a),
self.fx.callable(self.fx.b, self.fx.a),
)
def test_var_arg_callable_subtyping_3(self) -> None:
self.assert_strict_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.a), self.fx.callable(self.fx.a)
)
def test_var_arg_callable_subtyping_4(self) -> None:
self.assert_strict_subtype(
self.fx.callable_var_arg(1, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable(self.fx.b, self.fx.a),
)
def test_var_arg_callable_subtyping_5(self) -> None:
self.assert_strict_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable(self.fx.b, self.fx.a),
)
def test_var_arg_callable_subtyping_6(self) -> None:
self.assert_strict_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.f, self.fx.d),
self.fx.callable_var_arg(0, self.fx.b, self.fx.e, self.fx.d),
)
def test_var_arg_callable_subtyping_7(self) -> None:
self.assert_not_subtype(
self.fx.callable_var_arg(0, self.fx.b, self.fx.d),
self.fx.callable(self.fx.a, self.fx.d),
)
def test_var_arg_callable_subtyping_8(self) -> None:
self.assert_not_subtype(
self.fx.callable_var_arg(0, self.fx.b, self.fx.d),
self.fx.callable_var_arg(0, self.fx.a, self.fx.a, self.fx.d),
)
self.assert_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.d),
self.fx.callable_var_arg(0, self.fx.b, self.fx.b, self.fx.d),
)
def test_var_arg_callable_subtyping_9(self) -> None:
self.assert_not_subtype(
self.fx.callable_var_arg(0, self.fx.b, self.fx.b, self.fx.d),
self.fx.callable_var_arg(0, self.fx.a, self.fx.d),
)
self.assert_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.a, self.fx.d),
self.fx.callable_var_arg(0, self.fx.b, self.fx.d),
)
def test_type_callable_subtyping(self) -> None:
self.assert_subtype(self.fx.callable_type(self.fx.d, self.fx.a), self.fx.type_type)
self.assert_strict_subtype(
self.fx.callable_type(self.fx.d, self.fx.b), self.fx.callable(self.fx.d, self.fx.a)
)
self.assert_strict_subtype(
self.fx.callable_type(self.fx.a, self.fx.b), self.fx.callable(self.fx.a, self.fx.b)
)
def test_type_var_tuple(self) -> None:
self.assert_subtype(Instance(self.fx.gvi, []), Instance(self.fx.gvi, []))
self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b]),
Instance(self.fx.gvi, [self.fx.b, self.fx.a]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b]), Instance(self.fx.gvi, [self.fx.a])
)
self.assert_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [UnpackType(self.fx.ss)]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [UnpackType(self.fx.us)]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss)]), Instance(self.fx.gvi, [])
)
self.assert_not_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss)]), Instance(self.fx.gvi, [self.fx.anyt])
)
def test_type_var_tuple_with_prefix_suffix(self) -> None:
self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
)
self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss)]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.b, UnpackType(self.fx.ss)]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss)]),
)
self.assert_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.b]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a, self.fx.b]),
)
self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss), self.fx.c]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss), self.fx.c]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss), self.fx.c]),
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss), self.fx.b, self.fx.c]),
)
def test_type_var_tuple_unpacked_variable_length_tuple(self) -> None:
self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.a]),
Instance(self.fx.gvi, [UnpackType(Instance(self.fx.std_tuplei, [self.fx.a]))]),
)
def test_fallback_not_subtype_of_tuple(self) -> None:
self.assert_not_subtype(self.fx.a, TupleType([self.fx.b], fallback=self.fx.a))
def test_literal(self) -> None:
str1 = self.fx.lit_str1
str2 = self.fx.lit_str2
str1_inst = self.fx.lit_str1_inst
str2_inst = self.fx.lit_str2_inst
str_type = self.fx.str_type
# other operand is the fallback type
# "x" ≲ str -> YES
# str ≲ "x" -> NO
# "x"? ≲ str -> YES
# str ≲ "x"? -> YES
self.assert_subtype(str1, str_type)
self.assert_not_subtype(str_type, str1)
self.assert_subtype(str1_inst, str_type)
self.assert_subtype(str_type, str1_inst)
# other operand is the same literal
# "x" ≲ "x" -> YES
# "x" ≲ "x"? -> YES
# "x"? ≲ "x" -> YES
# "x"? ≲ "x"? -> YES
self.assert_subtype(str1, str1)
self.assert_subtype(str1, str1_inst)
self.assert_subtype(str1_inst, str1)
self.assert_subtype(str1_inst, str1_inst)
# other operand is a different literal
# "x" ≲ "y" -> NO
# "x" ≲ "y"? -> YES
# "x"? ≲ "y" -> NO
# "x"? ≲ "y"? -> YES
self.assert_not_subtype(str1, str2)
self.assert_subtype(str1, str2_inst)
self.assert_not_subtype(str1_inst, str2)
self.assert_subtype(str1_inst, str2_inst)
# check proper subtyping
# other operand is the fallback type
# "x" <: str -> YES
# str <: "x" -> NO
# "x"? <: str -> YES
# str <: "x"? -> NO
self.assert_proper_subtype(str1, str_type)
self.assert_not_proper_subtype(str_type, str1)
self.assert_proper_subtype(str1_inst, str_type)
self.assert_not_proper_subtype(str_type, str1_inst)
# other operand is the same literal
# "x" <: "x" -> YES
# "x" <: "x"? -> YES
# "x"? <: "x" -> NO
# "x"? <: "x"? -> YES
self.assert_proper_subtype(str1, str1)
self.assert_proper_subtype(str1, str1_inst)
self.assert_not_proper_subtype(str1_inst, str1)
self.assert_proper_subtype(str1_inst, str1_inst)
# other operand is a different literal
# "x" <: "y" -> NO
# "x" <: "y"? -> NO
# "x"? <: "y" -> NO
# "x"? <: "y"? -> NO
self.assert_not_proper_subtype(str1, str2)
self.assert_not_proper_subtype(str1, str2_inst)
self.assert_not_proper_subtype(str1_inst, str2)
self.assert_not_proper_subtype(str1_inst, str2_inst)
# IDEA: Maybe add these test cases (they are tested pretty well in type
# checker tests already):
# * more interface subtyping test cases
# * more generic interface subtyping test cases
# * type variables
# * tuple types
# * None type
# * any type
# * generic function types
def assert_proper_subtype(self, s: Type, t: Type) -> None:
assert is_proper_subtype(s, t), f"{s} not proper subtype of {t}"
def assert_not_proper_subtype(self, s: Type, t: Type) -> None:
assert not is_proper_subtype(s, t), f"{s} not proper subtype of {t}"
def assert_subtype(self, s: Type, t: Type) -> None:
assert is_subtype(s, t), f"{s} not subtype of {t}"
def assert_not_subtype(self, s: Type, t: Type) -> None:
assert not is_subtype(s, t), f"{s} subtype of {t}"
def assert_strict_subtype(self, s: Type, t: Type) -> None:
self.assert_subtype(s, t)
self.assert_not_subtype(t, s)
def assert_equivalent(self, s: Type, t: Type) -> None:
self.assert_subtype(s, t)
self.assert_subtype(t, s)
def assert_unrelated(self, s: Type, t: Type) -> None:
self.assert_not_subtype(s, t)
self.assert_not_subtype(t, s)
class RestrictionSuite(Suite):
# Tests for type restrictions "A - B", i.e. ``T <: A and not T <: B``.
def setUp(self) -> None:
self.fx = TypeFixture()
def assert_restriction(self, s: Type, t: Type, expected: Type) -> None:
actual = restrict_subtype_away(s, t)
msg = f"restrict_subtype_away({s}, {t}) == {{}} ({{}} expected)"
self.assertEqual(actual, expected, msg=msg.format(actual, expected))
def test_literal(self) -> None:
str1 = self.fx.lit_str1
str2 = self.fx.lit_str2
str1_inst = self.fx.lit_str1_inst
str2_inst = self.fx.lit_str2_inst
str_type = self.fx.str_type
uninhabited = self.fx.uninhabited
# other operand is the fallback type
# "x" - str -> Never
# str - "x" -> str
# "x"? - str -> Never
# str - "x"? -> Never
self.assert_restriction(str1, str_type, uninhabited)
self.assert_restriction(str_type, str1, str_type)
self.assert_restriction(str1_inst, str_type, uninhabited)
self.assert_restriction(str_type, str1_inst, uninhabited)
# other operand is the same literal
# "x" - "x" -> Never
# "x" - "x"? -> Never
# "x"? - "x" -> Never
# "x"? - "x"? -> Never
self.assert_restriction(str1, str1, uninhabited)
self.assert_restriction(str1, str1_inst, uninhabited)
self.assert_restriction(str1_inst, str1, uninhabited)
self.assert_restriction(str1_inst, str1_inst, uninhabited)
# other operand is a different literal
# "x" - "y" -> "x"
# "x" - "y"? -> Never
# "x"? - "y" -> "x"?
# "x"? - "y"? -> Never
self.assert_restriction(str1, str2, str1)
self.assert_restriction(str1, str2_inst, uninhabited)
self.assert_restriction(str1_inst, str2, str1_inst)
self.assert_restriction(str1_inst, str2_inst, uninhabited)