forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tuples.test
274 lines (215 loc) · 6.34 KB
/
run-tuples.test
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
# Test cases for tuples (compile and run)
[case testTuple]
from typing import List, Optional, Tuple
from typing import Tuple
def f(x: Tuple[int, int]) -> Tuple[int,int]:
return x
def lurr(x: List[Optional[Tuple[int, str]]]) -> object:
return x[0]
def asdf(x: Tuple[int, str]) -> None:
pass
[file driver.py]
from testutil import assertRaises
from native import f, lurr, asdf
assert f((1,2)) == (1, 2)
assert lurr([(1, '2')]) == (1, '2')
with assertRaises(TypeError):
print(lurr([(1, 2)]))
with assertRaises(TypeError):
asdf((1, 2))
[case testTupleGet]
from typing import Tuple
def f(x: Tuple[Tuple[int, bool], int]) -> int:
return x[0][0]
[file driver.py]
from native import f
print(f(((1,True),2)))
big_number = pow(2, 80)
print(f(((big_number,True),2)))
[out]
1
1208925819614629174706176
[case testSequenceTupleArg]
from typing import Tuple
def f(x: Tuple[int, ...]) -> int:
return x[1]
[file driver.py]
from native import f
print(f((1,2,3,4)))
[out]
2
[case testTupleAttr]
from typing import Tuple
class C:
b: Tuple[Tuple[Tuple[int, int], int], int, str, object]
c: Tuple[()]
def f() -> None:
c = C()
c.b = (((1, 2), 2), 1, 'hi', 'hi2')
print(c.b)
def g() -> None:
try:
h()
except Exception:
print('caught the exception')
def h() -> Tuple[Tuple[Tuple[int, int], int], int, str, object]:
raise Exception('Intentional exception')
[file driver.py]
from native import f, g, C
f()
g()
assert not hasattr(C(), 'c')
[out]
(((1, 2), 2), 1, 'hi', 'hi2')
caught the exception
[case testNamedTupleAttributeRun]
from typing import NamedTuple
NT = NamedTuple('NT', [('x', int), ('y', int)])
def f(nt: NT) -> int:
if nt.x > nt.y:
return nt.x
return nt.y
nt = NT(1, 2)
[file driver.py]
from native import NT, nt, f
assert f(nt) == 2
assert f(NT(3, 2)) == 3
class Sub(NT):
pass
assert f(Sub(3, 2)) == 3
-- Ref: https://github.com/mypyc/mypyc/issues/924
[case testNamedTupleClassSyntax]
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
class FileData(NamedTuple):
st_mtime: float
st_size: int
hash: str
class SearchPaths(NamedTuple):
python_path: Tuple[str, ...]
mypy_path: Tuple[str, ...]
package_path: Tuple[str, ...]
typeshed_path: Tuple[str, ...]
class ClassIR: pass
class FuncIR: pass
class VTableMethod(NamedTuple):
cls: 'ClassIR'
name: str
method: FuncIR
shadow_method: Optional[FuncIR]
class DeserMaps(NamedTuple):
classes: Dict[str, 'ClassIR']
functions: Dict[str, 'FuncIR']
StealsDescription = Union[bool, List[bool]]
class RType: pass
class CFunctionDescription(NamedTuple):
name: str
arg_types: List[RType]
return_type: RType
var_arg_type: Optional[RType]
truncated_type: Optional[RType]
c_function_name: str
error_kind: int
steals: StealsDescription
is_borrowed: bool
ordering: Optional[List[int]]
extra_int_constants: List[Tuple[int, RType]]
priority: int
class IntComparisonOpDescription(NamedTuple):
binary_op_variant: int
c_func_description: CFunctionDescription
c_func_negated: bool
c_func_swap_operands: bool
class LoadAddressDescription(NamedTuple):
name: str
type: RType
src: str
[file driver.py]
from native import FileData, SearchPaths, DeserMaps, LoadAddressDescription, RType
assert FileData.__annotations__ == {'st_mtime': float, 'st_size': int, 'hash': str}
assert SearchPaths.__annotations__ == {'python_path': tuple, 'mypy_path': tuple,
'package_path': tuple, 'typeshed_path': tuple}
assert DeserMaps.__annotations__ == {'classes': dict, 'functions': dict}
assert LoadAddressDescription.__annotations__ == {'name': str, 'type': RType, 'src': str}
[case testTupleOps]
from typing import Tuple, List, Any, Optional
from typing_extensions import Final
def f() -> Tuple[()]:
return ()
def test_empty_tuple() -> None:
assert f() == ()
def f2() -> Any:
return ()
def test_empty_tuple_with_any_type():
assert f2() == ()
def f3() -> int:
x = (False, 1)
return x[1]
def test_new_tuple() -> None:
assert f3() == 1
def f4(y: int) -> int:
x = (False, y)
return x[1]
def test_new_tuple_boxed_int() -> None:
big_number = 1208925819614629174706176
assert f4(big_number) == big_number
def f5(x: List[int]) -> int:
return tuple(x)[1]
def test_sequence_tuple() -> None:
assert f5([1,2,3,4]) == 2
def f6(x: List[int]) -> int:
return len(tuple(x))
def test_sequence_tuple_len() -> None:
assert f6([1,2,3,4]) == 4
def f7(x: List[Tuple[int, int]]) -> int:
a, b = x[0]
return a + b
def test_unbox_tuple() -> None:
assert f7([(5, 6)]) == 11
# Test that order is irrelevant to unions. Really I only care that this builds.
class A:
pass
def lol() -> A:
return A()
def foo(x: bool, y: bool) -> Tuple[Optional[A], bool]:
z = lol()
return None if y else z, x
def test_slicing() -> None:
# Use dummy adds to avoid constant folding
zero = int()
two = zero + 2
s: Tuple[str, ...] = ("f", "o", "o", "b", "a", "r")
assert s[two:] == ("o", "b", "a", "r")
assert s[:two] == ("f", "o")
assert s[two:-two] == ("o", "b")
assert s[two:two] == ()
assert s[two:two + 1] == ("o",)
assert s[-two:] == ("a", "r")
assert s[:-two] == ("f", "o", "o", "b")
assert s[:] == ("f", "o", "o", "b", "a", "r")
assert s[two:333] == ("o", "b", "a", "r")
assert s[333:two] == ()
assert s[two:-333] == ()
assert s[-333:two] == ("f", "o")
long_int: int = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
assert s[1:long_int] == ("o", "o", "b", "a", "r")
assert s[long_int:] == ()
assert s[-long_int:-1] == ("f", "o", "o", "b", "a")
def f8(val: int) -> bool:
return val % 2 == 0
def test_sequence_generator() -> None:
source_list = [1, 2, 3]
a = tuple(f8(x) for x in source_list)
assert a == (False, True, False)
source_tuple: Tuple[int, ...] = (1, 2, 3)
a = tuple(f8(x) for x in source_tuple)
assert a == (False, True, False)
source_fixed_length_tuple = (1, 2, 3, 4)
a = tuple(f8(x) for x in source_fixed_length_tuple)
assert a == (False, True, False, True)
source_str = 'abbc'
b = tuple('s:' + x for x in source_str)
assert b == ('s:a', 's:b', 's:b', 's:c')
TUPLE: Final[Tuple[str, ...]] = ('x', 'y')
def test_final_boxed_tuple() -> None:
t = TUPLE
assert t == ('x', 'y')