-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-type-promotion.test
More file actions
265 lines (223 loc) · 8.01 KB
/
check-type-promotion.test
File metadata and controls
265 lines (223 loc) · 8.01 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
-- Test cases for type promotion (e.g. int -> float).
[case testPromoteIntToFloat]
def f(x: float) -> None: pass
f(1)
[builtins fixtures/primitives.pyi]
[case testCantPromoteFloatToInt]
def f(x: int) -> None: pass
f(1.1) # E: Argument 1 to "f" has incompatible type "float"; expected "int"
[builtins fixtures/primitives.pyi]
[case testPromoteFloatToComplex]
def f(x: complex) -> None: pass
f(1)
[builtins fixtures/primitives.pyi]
[case testPromoteIntToComplex]
def f(x: complex) -> None: pass
f(1)
[builtins fixtures/primitives.pyi]
[case testPromoteBytearrayToByte]
# flags: --no-strict-bytes
def f(x: bytes) -> None: pass
f(bytearray(b''))
[builtins fixtures/primitives.pyi]
[case testPromoteMemoryviewToBytes]
# flags: --no-strict-bytes
def f(x: bytes) -> None: pass
f(memoryview(b''))
[builtins fixtures/primitives.pyi]
[case testDisableBytearrayMemoryviewPromotion]
# flags: --strict-bytes --strict-equality --warn-unreachable
def f(x: bytes) -> None: ...
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
ba = bytearray(b"")
if ba == b"":
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
if b"" == ba:
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
if ba == bytes():
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
if bytes() == ba:
f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
[builtins fixtures/primitives.pyi]
[case testEnableBytearrayMemoryviewPromotion]
# flags: --no-strict-bytes --strict-equality --warn-unreachable
def f(x: bytes) -> None: ...
f(bytearray(b"asdf"))
f(memoryview(b"asdf"))
ba = bytearray(b"")
if ba == b"":
f(ba)
if b"" == ba:
f(ba)
if ba == bytes():
f(ba)
if bytes() == ba:
f(ba)
[builtins fixtures/primitives.pyi]
[case testDisableBytearrayMemoryviewPromotionStrictEquality]
# flags: --strict-equality --strict-bytes
def f(x: bytes, y: bytearray, z: memoryview) -> None:
x == y
y == z
x == z
97 in x
97 in y
97 in z
x in y
x in z
[builtins fixtures/primitives.pyi]
[case testEnableBytearrayMemoryviewPromotionStrictEquality]
# flags: --strict-equality --no-strict-bytes
def f(x: bytes, y: bytearray, z: memoryview) -> None:
x == y
y == z
x == z
97 in x
97 in y
97 in z
x in y
x in z
[builtins fixtures/primitives.pyi]
[case testNarrowingDownFromPromoteTargetType]
y = 0.0
y = 1
y() # E: "int" not callable
[builtins fixtures/primitives.pyi]
[case testNarrowingDownFromPromoteTargetType2]
y = 0.0
y = 1
y.x # E: "int" has no attribute "x"
[builtins fixtures/primitives.pyi]
[case testTypePromotionsDontInterfereWithProtocols]
from typing import TypeVar, Union, Protocol
class SupportsFloat(Protocol):
def __float__(self) -> float: pass
T = TypeVar('T')
def f(x: Union[SupportsFloat, T]) -> Union[SupportsFloat, T]: pass
f(0) # should not crash
[builtins fixtures/primitives.pyi]
[out]
[case testIntersectionUsingPromotion1]
# flags: --warn-unreachable
from typing import Union
x: complex = 1
reveal_type(x) # N: Revealed type is "builtins.complex"
if isinstance(x, int):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x) # N: Revealed type is "builtins.complex"
reveal_type(x) # N: Revealed type is "builtins.complex"
y: Union[int, float]
if isinstance(y, float):
reveal_type(y) # N: Revealed type is "builtins.float"
else:
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "builtins.int | builtins.float"
if isinstance(y, int):
reveal_type(y) # N: Revealed type is "builtins.int"
else:
reveal_type(y) # N: Revealed type is "builtins.float"
[builtins fixtures/primitives.pyi]
[case testIntersectionUsingPromotion2]
# flags: --warn-unreachable
x: complex = 1
reveal_type(x) # N: Revealed type is "builtins.complex"
if isinstance(x, (int, float)):
reveal_type(x) # N: Revealed type is "builtins.int | builtins.float"
else:
reveal_type(x) # N: Revealed type is "builtins.complex"
# Note we make type precise, since type promotions are involved
reveal_type(x) # N: Revealed type is "builtins.complex"
[builtins fixtures/primitives.pyi]
[case testIntersectionUsingPromotion3]
# flags: --warn-unreachable
x: object
if isinstance(x, int) and isinstance(x, complex):
reveal_type(x) # N: Revealed type is "builtins.int"
if isinstance(x, complex) and isinstance(x, int):
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/primitives.pyi]
[case testIntersectionUsingPromotion4]
# flags: --warn-unreachable
x: object
if isinstance(x, int):
if isinstance(x, complex):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x) # N: Revealed type is "builtins.int"
if isinstance(x, complex):
if isinstance(x, int):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x) # N: Revealed type is "builtins.complex"
[builtins fixtures/primitives.pyi]
[case testIntersectionUsingPromotion5]
# flags: --warn-unreachable
from typing import Union
x: Union[float, complex]
if isinstance(x, int):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
[builtins fixtures/primitives.pyi]
[case testIntersectionUsingPromotion6]
# flags: --warn-unreachable
from typing import Union
x: Union[str, complex]
if isinstance(x, int):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex"
reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex"
[builtins fixtures/primitives.pyi]
[case testIntersectionUsingPromotion7]
# flags: --warn-unreachable
from typing import Union
x: Union[int, float, complex]
if isinstance(x, int):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex"
if isinstance(x, float):
reveal_type(x) # N: Revealed type is "builtins.float"
else:
reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex"
reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex"
if isinstance(x, complex):
reveal_type(x) # N: Revealed type is "builtins.complex"
else:
reveal_type(x) # N: Revealed type is "builtins.int | builtins.float"
reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex"
[builtins fixtures/primitives.pyi]
[case testIntersectionUsingPromotion8]
# flags: --warn-unreachable
from typing import Union
x: Union[int, float, complex]
if isinstance(x, (int, float)):
reveal_type(x) # N: Revealed type is "builtins.int | builtins.float"
else:
reveal_type(x) # N: Revealed type is "builtins.complex"
if isinstance(x, (int, complex)):
reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex"
else:
reveal_type(x) # N: Revealed type is "builtins.float"
if isinstance(x, (float, complex)):
reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
else:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/primitives.pyi]
[case testRejectPromotionsForProtocols]
from typing import Protocol
class H(Protocol):
def hex(self, /) -> str: ...
f: H = 1.0
o: H = object() # E: Incompatible types in assignment (expression has type "object", variable has type "H")
c: H = 1j # E: Incompatible types in assignment (expression has type "complex", variable has type "H")
i: H = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "H")
b: H = False # E: Incompatible types in assignment (expression has type "bool", variable has type "H")
class N(float): ...
n: H = N()
[builtins fixtures/primitives.pyi]