forked from python/mypy
/
testtypes.py
618 lines (486 loc) · 24.6 KB
/
testtypes.py
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
"""Test cases for mypy types and type operations."""
from myunit import Suite, assert_equal, assert_true
from erasetype import erase_type
from expandtype import expand_type
from join import join_types
from meet import meet_types
from mtypes import (
UnboundType, Any, Void, Callable, TupleType, TypeVarDef, TypeVars, Type,
Instance, NoneTyp, ErrorType
)
from nodes import ARG_POS, ARG_OPT, ARG_STAR
from replacetvars import replace_type_vars
from subtypes import is_subtype
from typefixture import TypeFixture, InterfaceTypeFixture
class TypesSuite(Suite):
def __init__(self):
super().__init__()
self.x = UnboundType('X') # Helpers
self.y = UnboundType('Y')
def test_any(self):
assert_equal(str(Any()), 'any')
def test_simple_unbound_type(self):
u = UnboundType('Foo')
assert_equal(str(u), 'Foo?')
def test_generic_unbound_type(self):
u = UnboundType('Foo', [UnboundType('T'), Any()])
assert_equal(str(u), 'Foo?<T?, any>')
def test_void_type(self):
assert_equal(str(Void(None)), 'void')
def test_callable_type(self):
c = Callable([self.x, self.y],
[ARG_POS, ARG_POS],
[None, None],
Any(), False)
assert_equal(str(c), 'def (X?, Y?) -> any')
c2 = Callable([], [], [], Void(None), False)
assert_equal(str(c2), 'def ()')
def test_callable_type_with_default_args(self):
c = Callable([self.x, self.y], [ARG_POS, ARG_OPT], [None, None],
Any(), False)
assert_equal(str(c), 'def (X?, Y?=) -> any')
c2 = Callable([self.x, self.y], [ARG_OPT, ARG_OPT], [None, None],
Any(), False)
assert_equal(str(c2), 'def (X?=, Y?=) -> any')
def test_callable_type_with_var_args(self):
c = Callable([self.x], [ARG_STAR], [None], Any(), False)
assert_equal(str(c), 'def (*X?) -> any')
c2 = Callable([self.x, self.y], [ARG_POS, ARG_STAR],
[None, None], Any(), False)
assert_equal(str(c2), 'def (X?, *Y?) -> any')
c3 = Callable([self.x, self.y], [ARG_OPT, ARG_STAR], [None, None],
Any(), False)
assert_equal(str(c3), 'def (X?=, *Y?) -> any')
def test_tuple_type(self):
assert_equal(str(TupleType([])), 'tuple<>')
assert_equal(str(TupleType([self.x])), 'tuple<X?>')
assert_equal(str(TupleType([self.x, Any()])), 'tuple<X?, any>')
def test_type_variable_binding(self):
assert_equal(str(TypeVarDef('X', 1)), 'X')
assert_equal(str(TypeVarDef('X', 1, UnboundType('Y'))), 'X is Y?')
def test_generic_function_type(self):
c = Callable([self.x, self.y], [ARG_POS, ARG_POS], [None, None],
self.y, False, None,
TypeVars([TypeVarDef('X', -1)]))
assert_equal(str(c), 'def <X> (X?, Y?) -> Y?')
v = TypeVars([TypeVarDef('Y', -1, UnboundType('X')),
TypeVarDef('X', -2)])
c2 = Callable([], [], [], Void(None), False, None, v)
assert_equal(str(c2), 'def <Y is X?, X> ()')
class TypeOpsSuite(Suite):
def set_up(self):
self.fx = TypeFixture()
# ExpandTypes
def test_trivial_expand(self):
for t in (self.fx.a, self.fx.o, self.fx.t, self.fx.void, self.fx.nonet,
self.tuple(self.fx.a),
self.callable([], self.fx.a, self.fx.a), self.fx.anyt):
self.assert_expand(t, [], t)
self.assert_expand(t, [], t)
self.assert_expand(t, [], t)
def test_expand_naked_type_var(self):
self.assert_expand(self.fx.t, [(1, self.fx.a)], self.fx.a)
self.assert_expand(self.fx.t, [(2, self.fx.a)], self.fx.t)
def test_expand_basic_generic_types(self):
self.assert_expand(self.fx.gt, [(1, self.fx.a)], self.fx.ga)
# IDEA: Add test cases for
# tuple types
# callable types
# multiple arguments
def assert_expand(self, orig, map_items, result):
lower_bounds = {}
for id, t in map_items:
lower_bounds[id] = t
exp = expand_type(orig, lower_bounds)
# Remove erased tags (asterisks).
assert_equal(str(exp).replace('*', ''), str(result))
# ReplaceTypeVars
def test_trivial_replace(self):
for t in (self.fx.a, self.fx.o, self.fx.void, self.fx.nonet,
self.tuple(self.fx.a),
self.callable([], self.fx.a, self.fx.a), self.fx.anyt,
self.fx.err):
self.assert_replace(t, t)
def test_replace_type_var(self):
self.assert_replace(self.fx.t, self.fx.anyt)
def test_replace_generic_instance(self):
self.assert_replace(self.fx.ga, self.fx.ga)
self.assert_replace(self.fx.gt, self.fx.gdyn)
def assert_replace(self, orig, result):
assert_equal(str(replace_type_vars(orig)), str(result))
# EraseType
def test_trivial_erase(self):
for t in (self.fx.a, self.fx.o, self.fx.void, self.fx.nonet,
self.fx.anyt, self.fx.err):
self.assert_erase(t, t)
def test_erase_with_type_variable(self):
self.assert_erase(self.fx.t, self.fx.anyt)
def test_erase_with_generic_type(self):
self.assert_erase(self.fx.ga, self.fx.gdyn)
self.assert_erase(self.fx.hab,
Instance(self.fx.hi, [self.fx.anyt, self.fx.anyt]))
def test_erase_with_tuple_type(self):
self.assert_erase(self.tuple(self.fx.a), self.fx.std_tuple)
def test_erase_with_function_type(self):
self.assert_erase(self.fx.callable(self.fx.a, self.fx.b),
self.fx.callable_type(self.fx.void))
def test_erase_with_type_object(self):
self.assert_erase(self.fx.callable_type(self.fx.a, self.fx.b),
self.fx.callable_type(self.fx.void))
def assert_erase(self, orig, result):
assert_equal(str(erase_type(orig, self.fx.basic)), str(result))
# Helpers
def tuple(self, *a):
return TupleType(a)
Callable callable(self, vars, *a):
"""callable(args, a1, ..., an, r) constructs a callable with
argument types a1, ... an and return type r and type arguments
vars.
"""
tv = <TypeVarDef> []
n = -1
for v in vars:
tv.append(TypeVarDef(v, n))
n -= 1
return Callable(a[:-1], [ARG_POS] * (len(a) - 1),
<str> [None] * (len(a) - 1),
a[-1],
False,
None,
TypeVars(tv))
class JoinSuite(Suite):
def set_up(self):
self.fx = TypeFixture()
def test_trivial_cases(self):
for simple in self.fx.void, self.fx.a, self.fx.o, self.fx.b:
self.assert_join(simple, simple, simple)
def test_class_subtyping(self):
self.assert_join(self.fx.a, self.fx.o, self.fx.o)
self.assert_join(self.fx.b, self.fx.o, self.fx.o)
self.assert_join(self.fx.a, self.fx.d, self.fx.o)
self.assert_join(self.fx.b, self.fx.c, self.fx.a)
self.assert_join(self.fx.b, self.fx.d, self.fx.o)
def test_tuples(self):
self.assert_join(self.tuple(), self.tuple(), self.tuple())
self.assert_join(self.tuple(self.fx.a),
self.tuple(self.fx.a),
self.tuple(self.fx.a))
self.assert_join(self.tuple(self.fx.b, self.fx.c),
self.tuple(self.fx.a, self.fx.d),
self.tuple(self.fx.a, self.fx.o))
self.assert_join(self.tuple(self.fx.a, self.fx.a),
self.fx.std_tuple,
self.fx.o)
self.assert_join(self.tuple(self.fx.a),
self.tuple(self.fx.a, self.fx.a),
self.fx.o)
def test_function_types(self):
self.assert_join(self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.a, self.fx.b))
self.assert_join(self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.b, self.fx.b),
self.fx.o)
self.assert_join(self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.a, self.fx.a),
self.fx.o)
def test_type_vars(self):
self.assert_join(self.fx.t, self.fx.t, self.fx.t)
self.assert_join(self.fx.s, self.fx.s, self.fx.s)
self.assert_join(self.fx.t, self.fx.s, self.fx.o)
def test_void(self):
self.assert_join(self.fx.void, self.fx.void, self.fx.void)
self.assert_join(self.fx.void, self.fx.anyt, self.fx.anyt)
# Join of any other type against void results in ErrorType, since there
# is no other meaningful result.
for t in [self.fx.a, self.fx.o, NoneTyp(), UnboundType('x'),
self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_join(t, self.fx.void, self.fx.err)
def test_none(self):
# Any type t joined with None results in t.
for t in [NoneTyp(), self.fx.a, self.fx.o, UnboundType('x'),
self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b), self.fx.anyt]:
self.assert_join(t, NoneTyp(), t)
def test_unbound_type(self):
self.assert_join(UnboundType('x'), UnboundType('x'), self.fx.anyt)
self.assert_join(UnboundType('x'), UnboundType('y'), self.fx.anyt)
# Any type t joined with an unbound type results in dynamic. Unbound
# type means that there is an error somewhere in the program, so this
# does not affect type safety (whatever the result).
for t in [self.fx.a, self.fx.o, self.fx.ga, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_join(t, UnboundType('X'), self.fx.anyt)
def test_any_type(self):
# Join against 'any' type always results in 'any'.
for t in [self.fx.anyt, self.fx.a, self.fx.o, NoneTyp(),
UnboundType('x'), self.fx.void, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_join(t, self.fx.anyt, self.fx.anyt)
def test_other_mixed_types(self):
# In general, joining unrelated types produces object.
for t1 in [self.fx.a, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
for t2 in [self.fx.a, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
if str(t1) != str(t2):
self.assert_join(t1, t2, self.fx.o)
def test_error_type(self):
self.assert_join(self.fx.err, self.fx.anyt, self.fx.anyt)
# Meet against any type except dynamic results in ErrorType.
for t in [self.fx.a, self.fx.o, NoneTyp(), UnboundType('x'),
self.fx.void, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_join(t, self.fx.err, self.fx.err)
def test_simple_generics(self):
self.assert_join(self.fx.ga, self.fx.ga, self.fx.ga)
self.assert_join(self.fx.ga, self.fx.gb, self.fx.o)
self.assert_join(self.fx.ga, self.fx.g2a, self.fx.o)
self.assert_join(self.fx.ga, self.fx.nonet, self.fx.ga)
self.assert_join(self.fx.ga, self.fx.anyt, self.fx.anyt)
for t in [self.fx.a, self.fx.o, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_join(t, self.fx.ga, self.fx.o)
def test_generics_with_multiple_args(self):
self.assert_join(self.fx.hab, self.fx.hab, self.fx.hab)
self.assert_join(self.fx.hab, self.fx.haa, self.fx.o)
self.assert_join(self.fx.hab, self.fx.hbb, self.fx.o)
def test_generics_with_inheritance(self):
self.assert_join(self.fx.gsab, self.fx.gb, self.fx.gb)
self.assert_join(self.fx.gsba, self.fx.gb, self.fx.o)
def test_generics_with_inheritance_and_shared_supertype(self):
self.assert_join(self.fx.gsba, self.fx.gs2a, self.fx.ga)
self.assert_join(self.fx.gsab, self.fx.gs2a, self.fx.o)
def test_generic_types_and_any(self):
self.assert_join(self.fx.gdyn, self.fx.ga, self.fx.gdyn)
def test_callables_with_any(self):
self.assert_join(self.callable(self.fx.a, self.fx.a, self.fx.anyt,
self.fx.a),
self.callable(self.fx.a, self.fx.anyt, self.fx.a,
self.fx.anyt),
self.callable(self.fx.a, self.fx.anyt, self.fx.anyt,
self.fx.anyt))
def test_join_interface_types(self):
self.assert_join(self.fx.f, self.fx.f, self.fx.f)
self.assert_join(self.fx.f, self.fx.f2, self.fx.o)
self.assert_join(self.fx.f, self.fx.f3, self.fx.f)
def test_join_interface_and_class_types(self):
self.skip() # FIX
self.assert_join(self.fx.o, self.fx.f, self.fx.o)
self.assert_join(self.fx.a, self.fx.f, self.fx.o)
self.assert_join(self.fx.e, self.fx.f, self.fx.f)
def test_join_class_types_with_interface_result(self):
# Unique result
self.assert_join(self.fx.e, self.fx.e2, self.fx.f)
# Ambiguous result
self.assert_join(self.fx.e2, self.fx.e3, self.fx.err)
def test_generic_interfaces(self):
self.skip() # FIX
fx = InterfaceTypeFixture()
self.assert_join(fx.gfa, fx.gfa, fx.gfa)
self.assert_join(fx.gfa, fx.gfb, fx.o)
self.assert_join(fx.m1, fx.gfa, fx.gfa)
self.assert_join(fx.m1, fx.gfb, fx.o)
def test_simple_type_objects(self):
t1 = self.type_callable(self.fx.a, self.fx.a)
t2 = self.type_callable(self.fx.b, self.fx.b)
self.assert_join(t1, t1, t1)
assert_true(join_types(t1, t1, self.fx.basic).is_type_obj())
self.assert_join(t1, t2, self.fx.std_type)
self.assert_join(t1, self.fx.std_type, self.fx.std_type)
self.assert_join(self.fx.std_type, self.fx.std_type, self.fx.std_type)
# There are additional test cases in check-inference.test.
# FIX interfaces with different paths
# FIX generic interfaces + inheritance
# FIX generic interfaces + ranges
# FIX function types + varargs and default args
def assert_join(self, s, t, join):
self.assert_simple_join(s, t, join)
self.assert_simple_join(t, s, join)
def assert_simple_join(self, s, t, join):
result = join_types(s, t, self.fx.basic)
actual = str(result)
expected = str(join)
assert_equal(actual, expected,
'join({}, {}) == {{}} ({{}} expected)'.format(s, t))
if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(s, result),
'{} not subtype of {}'.format(s, result))
if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(t, result),
'{} not subtype of {}'.format(t, result))
def tuple(self, *a):
return TupleType(a)
def callable(self, *a):
"""callable(a1, ..., an, r) constructs a callable with argument types
a1, ... an and return type r.
"""
n = len(a) - 1
return Callable(a[:-1], [ARG_POS] * n, <str> [None] * n,
a[-1], False)
def type_callable(self, *a):
"""type_callable(a1, ..., an, r) constructs a callable with
argument types a1, ... an and return type r, and which
represents a type.
"""
n = len(a) - 1
return Callable(a[:-1], [ARG_POS] * n, <str> [None] * n,
a[-1], True)
class MeetSuite(Suite):
def set_up(self):
self.fx = TypeFixture()
def test_trivial_cases(self):
for simple in self.fx.void, self.fx.a, self.fx.o, self.fx.b:
self.assert_meet(simple, simple, simple)
def test_class_subtyping(self):
self.assert_meet(self.fx.a, self.fx.o, self.fx.a)
self.assert_meet(self.fx.a, self.fx.b, self.fx.b)
self.assert_meet(self.fx.b, self.fx.o, self.fx.b)
self.assert_meet(self.fx.a, self.fx.d, NoneTyp())
self.assert_meet(self.fx.b, self.fx.c, NoneTyp())
def test_tuples(self):
self.assert_meet(self.tuple(), self.tuple(), self.tuple())
self.assert_meet(self.tuple(self.fx.a),
self.tuple(self.fx.a),
self.tuple(self.fx.a))
self.assert_meet(self.tuple(self.fx.b, self.fx.c),
self.tuple(self.fx.a, self.fx.d),
self.tuple(self.fx.b, NoneTyp()))
self.assert_meet(self.tuple(self.fx.a, self.fx.a),
self.fx.std_tuple,
NoneTyp())
self.assert_meet(self.tuple(self.fx.a),
self.tuple(self.fx.a, self.fx.a),
NoneTyp())
def test_function_types(self):
self.assert_meet(self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.a, self.fx.b))
self.assert_meet(self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.b, self.fx.b),
NoneTyp())
self.assert_meet(self.callable(self.fx.a, self.fx.b),
self.callable(self.fx.a, self.fx.a),
NoneTyp())
def test_type_vars(self):
self.assert_meet(self.fx.t, self.fx.t, self.fx.t)
self.assert_meet(self.fx.s, self.fx.s, self.fx.s)
self.assert_meet(self.fx.t, self.fx.s, NoneTyp())
def test_void(self):
self.assert_meet(self.fx.void, self.fx.void, self.fx.void)
self.assert_meet(self.fx.void, self.fx.anyt, self.fx.anyt)
# Meet of any other type against void results in ErrorType, since there
# is no meaningful valid result.
for t in [self.fx.a, self.fx.o, UnboundType('x'), NoneTyp(),
self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(t, self.fx.void, self.fx.err)
def test_none(self):
self.assert_meet(NoneTyp(), NoneTyp(), NoneTyp())
self.assert_meet(NoneTyp(), self.fx.anyt, self.fx.anyt)
self.assert_meet(NoneTyp(), self.fx.void, self.fx.err)
# Any type t joined with None results in None, unless t is any or
# void.
for t in [self.fx.a, self.fx.o, UnboundType('x'), self.fx.t,
self.tuple(), self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(t, NoneTyp(), NoneTyp())
def test_unbound_type(self):
self.assert_meet(UnboundType('x'), UnboundType('x'), self.fx.anyt)
self.assert_meet(UnboundType('x'), UnboundType('y'), self.fx.anyt)
self.assert_meet(UnboundType('x'), self.fx.void, self.fx.err)
# The meet of any type t with an unbound type results in dynamic
# (except for void). Unbound type means that there is an error
# somewhere in the program, so this does not affect type safety.
for t in [self.fx.anyt, self.fx.a, self.fx.o, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(t, UnboundType('X'), self.fx.anyt)
def test_dynamic_type(self):
# Meet against dynamic type always results in dynamic.
for t in [self.fx.anyt, self.fx.a, self.fx.o, NoneTyp(),
UnboundType('x'), self.fx.void, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(t, self.fx.anyt, self.fx.anyt)
def test_error_type(self):
self.assert_meet(self.fx.err, self.fx.anyt, self.fx.anyt)
# Meet against any type except dynamic results in ErrorType.
for t in [self.fx.a, self.fx.o, NoneTyp(), UnboundType('x'),
self.fx.void, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(t, self.fx.err, self.fx.err)
def test_simple_generics(self):
self.assert_meet(self.fx.ga, self.fx.ga, self.fx.ga)
self.assert_meet(self.fx.ga, self.fx.o, self.fx.ga)
self.assert_meet(self.fx.ga, self.fx.gb, self.fx.nonet)
self.assert_meet(self.fx.ga, self.fx.g2a, self.fx.nonet)
self.assert_meet(self.fx.ga, self.fx.nonet, self.fx.nonet)
self.assert_meet(self.fx.ga, self.fx.anyt, self.fx.anyt)
for t in [self.fx.a, self.fx.t, self.tuple(),
self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(t, self.fx.ga, self.fx.nonet)
def test_generics_with_multiple_args(self):
self.assert_meet(self.fx.hab, self.fx.hab, self.fx.hab)
self.assert_meet(self.fx.hab, self.fx.haa, self.fx.nonet)
self.assert_meet(self.fx.hab, self.fx.hbb, self.fx.nonet)
def test_generics_with_inheritance(self):
self.assert_meet(self.fx.gsab, self.fx.gb, self.fx.gsab)
self.assert_meet(self.fx.gsba, self.fx.gb, self.fx.nonet)
def test_generics_with_inheritance_and_shared_supertype(self):
self.assert_meet(self.fx.gsba, self.fx.gs2a, self.fx.nonet)
self.assert_meet(self.fx.gsab, self.fx.gs2a, self.fx.nonet)
def test_generic_types_and_dynamic(self):
self.assert_meet(self.fx.gdyn, self.fx.ga, self.fx.gdyn)
def test_callables_with_dynamic(self):
self.assert_meet(self.callable(self.fx.a, self.fx.a, self.fx.anyt,
self.fx.a),
self.callable(self.fx.a, self.fx.anyt, self.fx.a,
self.fx.anyt),
self.callable(self.fx.a, self.fx.anyt, self.fx.anyt,
self.fx.anyt))
def test_meet_interface_types(self):
self.assert_meet(self.fx.f, self.fx.f, self.fx.f)
self.assert_meet(self.fx.f, self.fx.f2, self.fx.nonet)
self.assert_meet(self.fx.f, self.fx.f3, self.fx.f3)
def test_join_interface_and_class_types(self):
self.assert_meet(self.fx.o, self.fx.f, self.fx.f)
self.assert_meet(self.fx.a, self.fx.f, self.fx.nonet)
self.assert_meet(self.fx.e, self.fx.f, self.fx.e)
def test_join_class_types_with_shared_interfaces(self):
# These have nothing special with respect to meets, unlike joins. These
# are for completeness only.
self.assert_meet(self.fx.e, self.fx.e2, self.fx.nonet)
self.assert_meet(self.fx.e2, self.fx.e3, self.fx.nonet)
def test_meet_with_generic_interfaces(self):
# TODO fix
self.skip()
fx = InterfaceTypeFixture()
self.assert_meet(fx.gfa, fx.m1, fx.m1)
self.assert_meet(fx.gfa, fx.gfa, fx.gfa)
self.assert_meet(fx.gfb, fx.m1, fx.nonet)
# FIX generic interfaces + ranges
def assert_meet(self, s, t, meet):
self.assert_simple_meet(s, t, meet)
self.assert_simple_meet(t, s, meet)
def assert_simple_meet(self, s, t, meet):
result = meet_types(s, t, self.fx.basic)
actual = str(result)
expected = str(meet)
assert_equal(actual, expected,
'meet({}, {}) == {{}} ({{}} expected)'.format(s, t))
if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(result, s),
'{} not subtype of {}'.format(result, s))
if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(result, t),
'{} not subtype of {}'.format(result, t))
def tuple(self, *a):
return TupleType(a)
def callable(self, *a):
"""callable(a1, ..., an, r) constructs a callable with argument types
a1, ... an and return type r.
"""
n = len(a) - 1
return Callable(a[:-1],
[ARG_POS] * n,
<str> [None] * n,
a[-1], False)