-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathcheck-optional.test
340 lines (284 loc) · 8.45 KB
/
check-optional.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
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
-- Tests for strict Optional behavior
[case testImplicitNoneType]
x = None
x() # E: None not callable
[case testExplicitNoneType]
x = None # type: None
x() # E: None not callable
[case testNoneMemberOfOptional]
from typing import Optional
x = None # type: Optional[int]
[case testTypeMemberOfOptional]
from typing import Optional
x = 0 # type: Optional[int]
[case testNoneNotMemberOfType]
x = None # type: int
[out]
main:1: error: Incompatible types in assignment (expression has type None, variable has type "int")
[case testTypeNotMemberOfNone]
x = 0 # type: None
[out]
main:1: error: Incompatible types in assignment (expression has type "int", variable has type None)
[case testOptionalNotMemberOfType]
from typing import Optional
def f(a: int) -> None: pass
x = None # type: Optional[int]
f(x) # E: Argument 1 to "f" has incompatible type "Optional[int]"; expected "int"
[case testIsinstanceCases]
from typing import Optional
x = None # type: Optional[int]
if isinstance(x, int):
reveal_type(x) # E: Revealed type is 'builtins.int'
else:
reveal_type(x) # E: Revealed type is 'builtins.None'
[builtins fixtures/isinstance.py]
[case testIfCases]
from typing import Optional
x = None # type: Optional[int]
if x:
reveal_type(x) # E: Revealed type is 'builtins.int'
else:
reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.None]'
[builtins fixtures/bool.py]
[case testIfNotCases]
from typing import Optional
x = None # type: Optional[int]
if not x:
reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.None]'
else:
reveal_type(x) # E: Revealed type is 'builtins.int'
[builtins fixtures/bool.py]
[case testIsNotNoneCases]
from typing import Optional
x = None # type: Optional[int]
if x is not None:
reveal_type(x) # E: Revealed type is 'builtins.int'
else:
reveal_type(x) # E: Revealed type is 'builtins.None'
[builtins fixtures/bool.py]
[case testIsNoneCases]
from typing import Optional
x = None # type: Optional[int]
if x is None:
reveal_type(x) # E: Revealed type is 'builtins.None'
else:
reveal_type(x) # E: Revealed type is 'builtins.int'
[builtins fixtures/bool.py]
[case testLambdaReturningNone]
f = lambda: None
x = f()
[case testNoneArgumentType]
def f(x: None) -> None: pass
f(None)
[case testInferOptionalFromDefaultNone]
def f(x: int = None) -> None:
x + 1 # E: Unsupported left operand type for + (some union)
f(None)
[out]
main: note: In function "f":
[case testInferOptionalFromDefaultNoneWithFastParser]
# options: fast_parser
def f(x: int = None) -> None:
x + 1 # E: Unsupported left operand type for + (some union)
f(None)
[out]
main: note: In function "f":
[case testInferOptionalType]
x = None
if bool():
# scope limit assignment
x = 1
# in scope of the assignment, x is an int
reveal_type(x) # E: Revealed type is 'builtins.int'
# out of scope of the assignment, it's an Optional[int]
reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.None]'
[builtins fixtures/bool.py]
[case testInferOptionalTypeLocallyBound]
x = None
x = 1
reveal_type(x) # E: Revealed type is 'builtins.int'
[case testInferOptionalTypeFromOptional]
from typing import Optional
y = None # type: Optional[int]
x = None
x = y
reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.None]'
[case testInferOptionalListType]
x = [None]
x.append(1)
reveal_type(x) # E: Revealed type is 'builtins.list[Union[builtins.int, builtins.None]]'
[builtins fixtures/list.py]
[case testInferNonOptionalListType]
x = []
x.append(1)
x() # E: List[int] not callable
[builtins fixtures/list.py]
[case testInferOptionalDictKeyValueTypes]
x = {None: None}
x["bar"] = 1
reveal_type(x) # E: Revealed type is 'builtins.dict[Union[builtins.str, builtins.None], Union[builtins.int, builtins.None]]'
[builtins fixtures/dict.py]
[case testInferNonOptionalDictType]
x = {}
x["bar"] = 1
x() # E: Dict[str, int] not callable
[builtins fixtures/dict.py]
[case testNoneClassVariable]
from typing import Optional
class C:
x = None # type: int
def __init__(self) -> None:
self.x = 0
[case testNoneClassVariableInInit]
from typing import Optional
class C:
x = None # type: int
def __init__(self) -> None:
self.x = None # E: Incompatible types in assignment (expression has type None, variable has type "int")
[out]
main: note: In member "__init__" of class "C":
[case testMultipleAssignmentNoneClassVariableInInit]
from typing import Optional
class C:
x, y = None, None # type: int, str
def __init__(self) -> None:
self.x = None # E: Incompatible types in assignment (expression has type None, variable has type "int")
self.y = None # E: Incompatible types in assignment (expression has type None, variable has type "str")
[out]
main: note: In member "__init__" of class "C":
[case testOverloadWithNone]
from typing import overload
@overload
def f(x: None) -> str: pass
@overload
def f(x: int) -> int: pass
reveal_type(f(None)) # E: Revealed type is 'builtins.str'
reveal_type(f(0)) # E: Revealed type is 'builtins.int'
[case testOptionalTypeOrTypePlain]
from typing import Optional
def f(a: Optional[int]) -> int:
return a or 0
[out]
[case testOptionalTypeOrTypeTypeVar]
from typing import Optional, TypeVar
T = TypeVar('T')
def f(a: Optional[T], b: T) -> T:
return a or b
[out]
[case testOptionalTypeOrTypeBothOptional]
from typing import Optional
def f(a: Optional[int], b: Optional[int]) -> None:
reveal_type(a or b)
def g(a: int, b: Optional[int]) -> None:
reveal_type(a or b)
[out]
main: note: In function "f":
main:3: error: Revealed type is 'Union[builtins.int, builtins.None]'
main: note: In function "g":
main:5: error: Revealed type is 'Union[builtins.int, builtins.None]'
[case testOptionalTypeOrTypeComplexUnion]
from typing import Union
def f(a: Union[int, str, None]) -> None:
reveal_type(a or 'default')
[out]
main: note: In function "f":
main:3: error: Revealed type is 'Union[builtins.int, builtins.str]'
[case testOptionalTypeOrTypeNoTriggerPlain]
from typing import Optional
def f(a: Optional[int], b: int) -> int:
return b or a
[out]
main: note: In function "f":
main:3: error: Incompatible return value type (got "Optional[int]", expected "int")
[case testOptionalTypeOrTypeNoTriggerTypeVar]
from typing import Optional, TypeVar
T = TypeVar('T')
def f(a: Optional[T], b: T) -> T:
return b or a
[out]
main: note: In function "f":
main:4: error: Incompatible return value type (got "Optional[T]", expected "T")
[case testNoneOrStringIsString]
def f() -> str:
a = None
b = ''
return a or b
[out]
[case testNoneOrTypeVarIsTypeVar]
from typing import TypeVar
T = TypeVar('T')
def f(b: T) -> T:
a = None
return a or b
[out]
[case testYieldNothingInFunctionReturningGenerator]
from typing import Generator
def f() -> Generator[None, None, None]:
yield
[out]
[case testNoneAndStringIsNone]
a = None
b = "foo"
reveal_type(a and b) # E: Revealed type is 'builtins.None'
[case testNoneMatchesObjectInOverload]
import a
a.f(None)
[file a.pyi]
from typing import overload
@overload
def f() -> None: ...
@overload
def f(o: object) -> None: ...
[case testGenericSubclassReturningNone]
from typing import Generic, TypeVar
T = TypeVar('T')
class Base(Generic[T]):
def f(self) -> T:
pass
class SubNone(Base[None]):
def f(self) -> None:
pass
class SubInt(Base[int]):
def f(self) -> int:
return 1
[case testUseOfNoneReturningFunction]
from typing import Optional
def f() -> None:
pass
def g(x: Optional[int]) -> int:
pass
x = f() # E: Function does not return a value
f() + 1 # E: Function does not return a value
g(f()) # E: Function does not return a value
[case testEmptyReturn]
def f() -> None:
return
[case testReturnNone]
def f() -> None:
return None
[case testNoneCallable]
from typing import Callable
def f() -> None: pass
x = f # type: Callable[[], None]
[case testOptionalCallable]
from typing import Callable, Optional
T = Optional[Callable[..., None]]
[case testAnyTypeInPartialTypeList]
# options: check_untyped_defs
def f(): ...
def lookup_field(name, obj):
try:
pass
except:
attr = f()
else:
attr = None
[out]
main: note: In function "lookup_field":
main:10: error: Need type annotation for variable
[case testTernaryWithNone]
reveal_type(None if bool() else 0) # E: Revealed type is 'Union[builtins.int, builtins.None]'
[builtins fixtures/bool.py]
[case testListWithNone]
reveal_type([0, None, 0]) # E: Revealed type is 'builtins.list[Union[builtins.int, builtins.None]]'
[builtins fixtures/list.py]