Debian Codegraph @

test-data/unit/check-type-promotion.test

raw
1 -- Test cases for type promotion (e.g. int -> float).
2
3
4 [case testPromoteIntToFloat]
5 def f(x: float) -> None: pass
6 f(1)
7 [builtins fixtures/primitives.pyi]
8
9 [case testCantPromoteFloatToInt]
10 def f(x: int) -> None: pass
11 f(1.1) # E: Argument 1 to "f" has incompatible type "float"; expected "int"
12 [builtins fixtures/primitives.pyi]
13
14 [case testPromoteFloatToComplex]
15 def f(x: complex) -> None: pass
16 f(1)
17 [builtins fixtures/primitives.pyi]
18
19 [case testPromoteIntToComplex]
20 def f(x: complex) -> None: pass
21 f(1)
22 [builtins fixtures/primitives.pyi]
23
24 [case testPromoteBytearrayToByte]
25 # flags: --no-strict-bytes
26 def f(x: bytes) -> None: pass
27 f(bytearray(b''))
28 [builtins fixtures/primitives.pyi]
29
30 [case testPromoteMemoryviewToBytes]
31 # flags: --no-strict-bytes
32 def f(x: bytes) -> None: pass
33 f(memoryview(b''))
34 [builtins fixtures/primitives.pyi]
35
36 [case testDisableBytearrayMemoryviewPromotion]
37 # flags: --strict-bytes --strict-equality --warn-unreachable
38 def f(x: bytes) -> None: ...
39 f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
40 f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
41 ba = bytearray(b"")
42 if ba == b"":
43 f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
44 if b"" == ba:
45 f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
46 if ba == bytes():
47 f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
48 if bytes() == ba:
49 f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
50 [builtins fixtures/primitives.pyi]
51
52 [case testEnableBytearrayMemoryviewPromotion]
53 # flags: --no-strict-bytes --strict-equality --warn-unreachable
54 def f(x: bytes) -> None: ...
55 f(bytearray(b"asdf"))
56 f(memoryview(b"asdf"))
57 ba = bytearray(b"")
58 if ba == b"":
59 f(ba)
60 if b"" == ba:
61 f(ba)
62 if ba == bytes():
63 f(ba)
64 if bytes() == ba:
65 f(ba)
66 [builtins fixtures/primitives.pyi]
67
68 [case testDisableBytearrayMemoryviewPromotionStrictEquality]
69 # flags: --strict-equality --strict-bytes
70 def f(x: bytes, y: bytearray, z: memoryview) -> None:
71 x == y
72 y == z
73 x == z
74 97 in x
75 97 in y
76 97 in z
77 x in y
78 x in z
79 [builtins fixtures/primitives.pyi]
80
81 [case testEnableBytearrayMemoryviewPromotionStrictEquality]
82 # flags: --strict-equality --no-strict-bytes
83 def f(x: bytes, y: bytearray, z: memoryview) -> None:
84 x == y
85 y == z
86 x == z
87 97 in x
88 97 in y
89 97 in z
90 x in y
91 x in z
92 [builtins fixtures/primitives.pyi]
93
94 [case testNarrowingDownFromPromoteTargetType]
95 y = 0.0
96 y = 1
97 y() # E: "int" not callable
98 [builtins fixtures/primitives.pyi]
99
100 [case testNarrowingDownFromPromoteTargetType2]
101 y = 0.0
102 y = 1
103 y.x # E: "int" has no attribute "x"
104 [builtins fixtures/primitives.pyi]
105
106 [case testTypePromotionsDontInterfereWithProtocols]
107 from typing import TypeVar, Union, Protocol
108
109 class SupportsFloat(Protocol):
110 def __float__(self) -> float: pass
111
112 T = TypeVar('T')
113 def f(x: Union[SupportsFloat, T]) -> Union[SupportsFloat, T]: pass
114 f(0) # should not crash
115 [builtins fixtures/primitives.pyi]
116 [out]
117
118 [case testIntersectionUsingPromotion1]
119 # flags: --warn-unreachable
120 from typing import Union
121
122 x: complex = 1
123 reveal_type(x) # N: Revealed type is "builtins.complex"
124 if isinstance(x, int):
125 reveal_type(x) # N: Revealed type is "builtins.int"
126 else:
127 reveal_type(x) # N: Revealed type is "builtins.complex"
128 reveal_type(x) # N: Revealed type is "builtins.complex"
129
130 y: Union[int, float]
131 if isinstance(y, float):
132 reveal_type(y) # N: Revealed type is "builtins.float"
133 else:
134 reveal_type(y) # N: Revealed type is "builtins.int"
135
136 reveal_type(y) # N: Revealed type is "builtins.int | builtins.float"
137
138 if isinstance(y, int):
139 reveal_type(y) # N: Revealed type is "builtins.int"
140 else:
141 reveal_type(y) # N: Revealed type is "builtins.float"
142 [builtins fixtures/primitives.pyi]
143
144 [case testIntersectionUsingPromotion2]
145 # flags: --warn-unreachable
146 x: complex = 1
147 reveal_type(x) # N: Revealed type is "builtins.complex"
148 if isinstance(x, (int, float)):
149 reveal_type(x) # N: Revealed type is "builtins.int | builtins.float"
150 else:
151 reveal_type(x) # N: Revealed type is "builtins.complex"
152
153 # Note we make type precise, since type promotions are involved
154 reveal_type(x) # N: Revealed type is "builtins.complex"
155 [builtins fixtures/primitives.pyi]
156
157 [case testIntersectionUsingPromotion3]
158 # flags: --warn-unreachable
159 x: object
160 if isinstance(x, int) and isinstance(x, complex):
161 reveal_type(x) # N: Revealed type is "builtins.int"
162 if isinstance(x, complex) and isinstance(x, int):
163 reveal_type(x) # N: Revealed type is "builtins.int"
164 [builtins fixtures/primitives.pyi]
165
166 [case testIntersectionUsingPromotion4]
167 # flags: --warn-unreachable
168 x: object
169 if isinstance(x, int):
170 if isinstance(x, complex):
171 reveal_type(x) # N: Revealed type is "builtins.int"
172 else:
173 reveal_type(x) # N: Revealed type is "builtins.int"
174 if isinstance(x, complex):
175 if isinstance(x, int):
176 reveal_type(x) # N: Revealed type is "builtins.int"
177 else:
178 reveal_type(x) # N: Revealed type is "builtins.complex"
179 [builtins fixtures/primitives.pyi]
180
181 [case testIntersectionUsingPromotion5]
182 # flags: --warn-unreachable
183 from typing import Union
184
185 x: Union[float, complex]
186 if isinstance(x, int):
187 reveal_type(x) # N: Revealed type is "builtins.int"
188 else:
189 reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
190 reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
191 [builtins fixtures/primitives.pyi]
192
193 [case testIntersectionUsingPromotion6]
194 # flags: --warn-unreachable
195 from typing import Union
196
197 x: Union[str, complex]
198 if isinstance(x, int):
199 reveal_type(x) # N: Revealed type is "builtins.int"
200 else:
201 reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex"
202 reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex"
203 [builtins fixtures/primitives.pyi]
204
205 [case testIntersectionUsingPromotion7]
206 # flags: --warn-unreachable
207 from typing import Union
208
209 x: Union[int, float, complex]
210 if isinstance(x, int):
211 reveal_type(x) # N: Revealed type is "builtins.int"
212 else:
213 reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
214
215 reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex"
216
217 if isinstance(x, float):
218 reveal_type(x) # N: Revealed type is "builtins.float"
219 else:
220 reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex"
221
222 reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex"
223
224 if isinstance(x, complex):
225 reveal_type(x) # N: Revealed type is "builtins.complex"
226 else:
227 reveal_type(x) # N: Revealed type is "builtins.int | builtins.float"
228
229 reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex"
230 [builtins fixtures/primitives.pyi]
231
232 [case testIntersectionUsingPromotion8]
233 # flags: --warn-unreachable
234 from typing import Union
235
236 x: Union[int, float, complex]
237 if isinstance(x, (int, float)):
238 reveal_type(x) # N: Revealed type is "builtins.int | builtins.float"
239 else:
240 reveal_type(x) # N: Revealed type is "builtins.complex"
241 if isinstance(x, (int, complex)):
242 reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex"
243 else:
244 reveal_type(x) # N: Revealed type is "builtins.float"
245 if isinstance(x, (float, complex)):
246 reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex"
247 else:
248 reveal_type(x) # N: Revealed type is "builtins.int"
249 [builtins fixtures/primitives.pyi]
250
251 [case testRejectPromotionsForProtocols]
252 from typing import Protocol
253
254 class H(Protocol):
255 def hex(self, /) -> str: ...
256
257 f: H = 1.0
258 o: H = object() # E: Incompatible types in assignment (expression has type "object", variable has type "H")
259 c: H = 1j # E: Incompatible types in assignment (expression has type "complex", variable has type "H")
260 i: H = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "H")
261 b: H = False # E: Incompatible types in assignment (expression has type "bool", variable has type "H")
262
263 class N(float): ...
264 n: H = N()
265 [builtins fixtures/primitives.pyi]