forked from python/mypy
/
testsubtypes.py
147 lines (111 loc) · 5.65 KB
/
testsubtypes.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
from myunit import Suite, assert_true
from subtypes import is_subtype
from typefixture import TypeFixture, InterfaceTypeFixture
class SubtypingSuite(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_subtype(simple, simple)
def test_instance_subtyping(self):
self.assert_proper_subtype(self.fx.a, self.fx.o)
self.assert_proper_subtype(self.fx.b, self.fx.o)
self.assert_proper_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(self):
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_generic_subtyping_with_inheritance(self):
self.assert_subtype(self.fx.gsab, self.fx.gb)
self.assert_not_subtype(self.fx.gsab, self.fx.ga)
def test_interface_subtyping(self):
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):
# TODO make this work
self.skip()
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):
self.assert_proper_subtype(self.fx.callable(self.fx.o, self.fx.d),
self.fx.callable(self.fx.a, self.fx.d))
self.assert_proper_subtype(self.fx.callable(self.fx.d, self.fx.b),
self.fx.callable(self.fx.d, self.fx.a))
self.assert_unrelated(self.fx.callable(self.fx.a, self.fx.a),
self.fx.callable(self.fx.a, self.fx.void))
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):
self.assert_proper_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_proper_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_proper_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(self):
self.assert_proper_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))
self.assert_unrelated(
self.fx.callable_var_arg(0, self.fx.a, self.fx.a),
self.fx.callable(self.fx.a, self.fx.a))
self.assert_proper_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.a),
self.fx.callable(self.fx.a))
self.assert_proper_subtype(
self.fx.callable_var_arg(1, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable(self.fx.a, self.fx.a))
self.assert_proper_subtype(
self.fx.callable_var_arg(0, self.fx.a, self.fx.d, self.fx.a),
self.fx.callable(self.fx.a, self.fx.a))
def test_type_callable_subtyping(self):
self.assert_proper_subtype(
self.fx.callable_type(self.fx.d, self.fx.a), self.fx.std_type)
self.assert_proper_subtype(
self.fx.callable_type(self.fx.d, self.fx.b),
self.fx.callable(self.fx.d, self.fx.a))
self.assert_proper_subtype(self.fx.callable_type(self.fx.a, self.fx.b),
self.fx.callable(self.fx.a, self.fx.b))
# 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
# * void type
# * None type
# * any type
# * generic function types
def assert_subtype(self, s, t):
assert_true(is_subtype(s, t), '{} not subtype of {}'.format(s, t))
def assert_not_subtype(self, s, t):
assert_true(not is_subtype(s, t), '{} subtype of {}'.format(s, t))
def assert_proper_subtype(self, s, t):
self.assert_subtype(s, t)
self.assert_not_subtype(t, s)
def assert_equivalent(self, s, t):
self.assert_subtype(s, t)
self.assert_subtype(t, s)
def assert_unrelated(self, s, t):
self.assert_not_subtype(s, t)
self.assert_not_subtype(t, s)