Debian Codegraph @

test-data/unit/check-python38.test

raw
1 [case testDecoratedClassLine]
2 def d(c): ...
3 @d
4
5 class C: ...
6 class C: ... # E: Name "C" already defined on line 4
7
8 [case testDecoratedFunctionLine]
9 # flags: --disallow-untyped-defs
10 def d(f): ... # type: ignore
11 @d
12
13 def f(): ... # E: Function is missing a return type annotation \
14 # N: Use "-> None" if function does not return a value
15
16 [case testIgnoreDecoratedFunction1]
17 # flags: --disallow-untyped-defs --warn-unused-ignores
18 def d(f): ... # type: ignore
19 @d
20 # type: ignore # E: Unused "type: ignore" comment
21 def f(): ... # type: ignore
22
23 [case testIgnoreDecoratedFunction2]
24 # flags: --disallow-untyped-defs
25 def d(f): ... # type: ignore
26 @d
27
28 def f(): ... # type: ignore
29
30 [case testIgnoreScopeIssue1032]
31 def f(a: int): ...
32 f(
33 1,
34 2,
35 ) # type: ignore
36
37 [case testIgnoreScopeNested1]
38 def g(x: str) -> str: ...
39 def f(a: int) -> int: ...
40 f(
41 f(
42 g(
43 "IGNORE"
44 ) # type: ignore
45 )
46 )
47
48 [case testIgnoreScopeNested2]
49 [
50 "IGNORE" # type: ignore
51 &
52 "IGNORE",
53 ]
54 [builtins fixtures/list.pyi]
55
56 [case testIgnoreScopeNested3]
57 {
58 "IGNORE"
59 | # type: ignore
60 "IGNORE",
61 }
62 [builtins fixtures/set.pyi]
63
64 [case testIgnoreScopeNested4]
65 {
66 None: "IGNORE"
67 ^
68 "IGNORE", # type: ignore
69 }
70 [builtins fixtures/dict.pyi]
71
72 [case testIgnoreScopeNestedNonOverlapping]
73 def f(x: int): ...
74 def g(x: int): ...
75 (
76 f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
77 g("IGNORE"), # type: ignore
78 f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
79 )
80 [builtins fixtures/tuple.pyi]
81
82 [case testIgnoreScopeNestedOverlapping]
83 def f(x: int): ...
84 def g(x: int): ...
85 (
86 f("ERROR"), g( # E: Argument 1 to "f" has incompatible type "str"; expected "int"
87 "IGNORE" # type: ignore
88 ), f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
89 )
90 [builtins fixtures/tuple.pyi]
91
92 [case testIgnoreScopeUnused1]
93 # flags: --warn-unused-ignores
94 ( # type: ignore # E: Unused "type: ignore" comment
95 "IGNORE" # type: ignore # E: Unused "type: ignore" comment
96 + # type: ignore # E: Unused "type: ignore" comment
97 0 # type: ignore
98 ) # type: ignore # E: Unused "type: ignore" comment
99 [builtins fixtures/primitives.pyi]
100
101 [case testIgnoreScopeUnused2]
102 # flags: --warn-unused-ignores
103 ( # type: ignore # E: Unused "type: ignore" comment
104 "IGNORE"
105 - # type: ignore
106 0 # type: ignore # E: Unused "type: ignore" comment
107 ) # type: ignore # E: Unused "type: ignore" comment
108
109 [case testIgnoreScopeUnused3]
110 # flags: --warn-unused-ignores
111 ( # type: ignore # E: Unused "type: ignore" comment
112 "IGNORE"
113 /
114 0 # type: ignore
115 ) # type: ignore # E: Unused "type: ignore" comment
116
117 [case testPEP570ParamTypesMissing]
118 # flags: --disallow-untyped-defs
119 def f(x, /) -> None: ... # E: Function is missing a type annotation for one or more parameters
120
121 [case testPEP570ParamTypesBadDefault]
122 def f(x: int = "ERROR", /) -> None: ... # E: Incompatible default for parameter "x" (default has type "str", parameter has type "int")
123
124 [case testPEP570ParamTypesDefault]
125 def f(x: int = 0, /) -> None:
126 reveal_type(x) # N: Revealed type is "builtins.int"
127
128 [case testPEP570ParamTypesRequired]
129 def f(x: int, /) -> None:
130 reveal_type(x) # N: Revealed type is "builtins.int"
131
132 [case testPEP570Required]
133 def f(x: int, /) -> None: ...
134 f(1)
135 f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int"
136 f(x=1) # E: Unexpected keyword argument "x" for "f"
137 f(x="ERROR") # E: Unexpected keyword argument "x" for "f"
138
139 [case testPEP570Default]
140 def f(x: int = 0, /) -> None: ...
141 f()
142 f(1)
143 f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int"
144 f(x=1) # E: Unexpected keyword argument "x" for "f"
145 f(x="ERROR") # E: Unexpected keyword argument "x" for "f"
146
147 [case testPEP570Calls]
148 # flags: --no-strict-optional
149 from typing import Any, Dict
150 def f(p, /, p_or_kw, *, kw) -> None: ...
151 d = None # type: Dict[Any, Any]
152 f(0, 0, 0) # E: Too many positional arguments for "f"
153 f(0, 0, kw=0)
154 f(0, p_or_kw=0, kw=0)
155 f(p=0, p_or_kw=0, kw=0) # E: Unexpected keyword argument "p" for "f"
156 f(0, **d)
157 f(**d) # E: Missing positional argument "p_or_kw" in call to "f"
158 [builtins fixtures/dict.pyi]
159
160 [case testPEP570Signatures1]
161 def f(p1: bytes, p2: float, /, p_or_kw: int, *, kw: str) -> None:
162 reveal_type(p1) # N: Revealed type is "builtins.bytes"
163 reveal_type(p2) # N: Revealed type is "builtins.float"
164 reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
165 reveal_type(kw) # N: Revealed type is "builtins.str"
166
167 [case testPEP570Signatures2]
168 def f(p1: bytes, p2: float = 0.0, /, p_or_kw: int = 0, *, kw: str) -> None:
169 reveal_type(p1) # N: Revealed type is "builtins.bytes"
170 reveal_type(p2) # N: Revealed type is "builtins.float"
171 reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
172 reveal_type(kw) # N: Revealed type is "builtins.str"
173
174 [case testPEP570Signatures3]
175 def f(p1: bytes, p2: float = 0.0, /, *, kw: int) -> None:
176 reveal_type(p1) # N: Revealed type is "builtins.bytes"
177 reveal_type(p2) # N: Revealed type is "builtins.float"
178 reveal_type(kw) # N: Revealed type is "builtins.int"
179
180 [case testPEP570Signatures4]
181 def f(p1: bytes, p2: int = 0, /) -> None:
182 reveal_type(p1) # N: Revealed type is "builtins.bytes"
183 reveal_type(p2) # N: Revealed type is "builtins.int"
184
185 [case testPEP570Signatures5]
186 def f(p1: bytes, p2: float, /, p_or_kw: int) -> None:
187 reveal_type(p1) # N: Revealed type is "builtins.bytes"
188 reveal_type(p2) # N: Revealed type is "builtins.float"
189 reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
190
191 [case testPEP570Signatures6]
192 def f(p1: bytes, p2: float, /) -> None:
193 reveal_type(p1) # N: Revealed type is "builtins.bytes"
194 reveal_type(p2) # N: Revealed type is "builtins.float"
195
196 [case testPEP570Unannotated]
197 def f(arg, /): ...
198 g = lambda arg, /: arg
199 def h(arg=0, /): ...
200 i = lambda arg=0, /: arg
201
202 f(1)
203 reveal_type(g(1)) # N: Revealed type is "Any"
204 h()
205 h(1)
206 reveal_type(i()) # N: Revealed type is "Any"
207 reveal_type(i(1)) # N: Revealed type is "Any"
208 f(arg=0) # E: Unexpected keyword argument "arg" for "f"
209 g(arg=0) # E: Unexpected keyword argument "arg"
210 h(arg=0) # E: Unexpected keyword argument "arg" for "h"
211 i(arg=0) # E: Unexpected keyword argument "arg"
212
213 [case testWalrus]
214 from typing import Final, NamedTuple, Optional, List
215
216 if a := 2:
217 reveal_type(a) # N: Revealed type is "builtins.int"
218
219 while b := "x":
220 reveal_type(b) # N: Revealed type is "builtins.str"
221
222 l = [y2 := 1, y2 + 2, y2 + 3]
223 reveal_type(y2) # N: Revealed type is "builtins.int"
224 reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]"
225
226 filtered_data = [y3 for x in l if (y3 := a) is not None]
227 reveal_type(filtered_data) # N: Revealed type is "builtins.list[builtins.int]"
228 reveal_type(y3) # N: Revealed type is "builtins.int"
229
230 d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2}
231 reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
232 reveal_type(a2) # N: Revealed type is "builtins.int"
233
234 d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2}
235 reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
236 reveal_type(prefix) # N: Revealed type is "builtins.str"
237 reveal_type(start_val) # N: Revealed type is "builtins.int"
238
239 filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2}
240 reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
241 reveal_type(new_v) # N: Revealed type is "builtins.int"
242
243 def f(x: int = (c := 4)) -> int:
244 if a := 2:
245 reveal_type(a) # N: Revealed type is "builtins.int"
246
247 while b := "x":
248 reveal_type(b) # N: Revealed type is "builtins.str"
249
250 x = (y := 1) + (z := 2)
251 reveal_type(x) # N: Revealed type is "builtins.int"
252 reveal_type(y) # N: Revealed type is "builtins.int"
253 reveal_type(z) # N: Revealed type is "builtins.int"
254
255 l = [y2 := 1, y2 + 2, y2 + 3]
256 reveal_type(y2) # N: Revealed type is "builtins.int"
257 reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]"
258
259 filtered_data = [y3 for x in l if (y3 := a) is not None]
260 reveal_type(filtered_data) # N: Revealed type is "builtins.list[builtins.int]"
261 reveal_type(y3) # N: Revealed type is "builtins.int"
262
263 d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2}
264 reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
265 reveal_type(a2) # N: Revealed type is "builtins.int"
266
267 d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2}
268 reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
269 reveal_type(prefix) # N: Revealed type is "builtins.str"
270 reveal_type(start_val) # N: Revealed type is "builtins.int"
271
272 filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2}
273 reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
274 reveal_type(new_v) # N: Revealed type is "builtins.int"
275
276 # https://www.python.org/dev/peps/pep-0572/#exceptional-cases
277 (y4 := 3)
278 reveal_type(y4) # N: Revealed type is "builtins.int"
279
280 y5 = (y6 := 3)
281 reveal_type(y5) # N: Revealed type is "builtins.int"
282 reveal_type(y6) # N: Revealed type is "builtins.int"
283
284 f(x=(y7 := 3))
285 reveal_type(y7) # N: Revealed type is "builtins.int"
286
287 reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is "builtins.int"
288 y8 # E: Name "y8" is not defined
289
290 y7 = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
291 if y7 := "x": # E: Incompatible types in assignment (expression has type "str", variable has type "int")
292 pass
293
294 # Just make sure we don't crash on this sort of thing.
295 if NT := NamedTuple("NT", [("x", int)]): # E: "int" not callable
296 z2: NT # E: Variable "NT" is not valid as a type \
297 # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
298
299 if Alias := int: # E: Function "Alias" could always be true in boolean context
300 z3: Alias # E: Variable "Alias" is not valid as a type \
301 # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
302
303 if (reveal_type(y9 := 3) and # N: Revealed type is "Literal[3]?"
304 reveal_type(y9)): # N: Revealed type is "builtins.int"
305 reveal_type(y9) # N: Revealed type is "builtins.int"
306
307 return (y10 := 3) + y10
308
309 reveal_type(c) # N: Revealed type is "builtins.int"
310
311 def check_final() -> None:
312 x: Final = 3
313
314 if x := 4: # E: Cannot assign to final name "x"
315 pass
316
317 def check_binder(x: Optional[int], y: Optional[int], z: Optional[int], a: Optional[int],
318 b: Optional[int]) -> None:
319 reveal_type(x) # N: Revealed type is "builtins.int | None"
320
321 (x := 1)
322 reveal_type(x) # N: Revealed type is "builtins.int"
323
324 if x or (y := 1):
325 reveal_type(y) # N: Revealed type is "builtins.int | None"
326
327 if x and (y := 1):
328 reveal_type(y) # N: Revealed type is "builtins.int"
329
330 if (a := 1) and x:
331 reveal_type(a) # N: Revealed type is "builtins.int"
332
333 if (b := 1) or x:
334 reveal_type(b) # N: Revealed type is "builtins.int"
335
336 if z := 1:
337 reveal_type(z) # N: Revealed type is "builtins.int"
338
339 def check_partial() -> None:
340 x = None
341 if bool() and (x := 2):
342 pass
343
344 reveal_type(x) # N: Revealed type is "builtins.int | None"
345
346 def check_narrow(x: Optional[int], s: List[int]) -> None:
347 if (y := x):
348 reveal_type(y) # N: Revealed type is "builtins.int"
349
350 if (y := x) is not None:
351 reveal_type(y) # N: Revealed type is "builtins.int"
352
353 if (y := x) == 10:
354 reveal_type(y) # N: Revealed type is "builtins.int"
355
356 if (y := x) in s:
357 reveal_type(y) # N: Revealed type is "builtins.int"
358
359 if isinstance((y := x), int):
360 reveal_type(y) # N: Revealed type is "builtins.int"
361
362 class AssignmentExpressionsClass:
363 x = (y := 1) + (z := 2)
364 reveal_type(z) # N: Revealed type is "builtins.int"
365
366 l = [x2 := 1, 2, 3]
367 reveal_type(x2) # N: Revealed type is "builtins.int"
368
369 def __init__(self) -> None:
370 reveal_type(self.z) # N: Revealed type is "builtins.int"
371
372 l = [z2 := 1, z2 + 2, z2 + 3]
373 reveal_type(z2) # N: Revealed type is "builtins.int"
374 reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]"
375
376 filtered_data = [z3 for x in l if (z3 := 1) is not None]
377 reveal_type(filtered_data) # N: Revealed type is "builtins.list[builtins.int]"
378 reveal_type(z3) # N: Revealed type is "builtins.int"
379
380 # Assignment expressions from inside the class should not escape the class scope.
381 reveal_type(x2) # E: Name "x2" is not defined # N: Revealed type is "Any"
382 reveal_type(z2) # E: Name "z2" is not defined # N: Revealed type is "Any"
383
384 [builtins fixtures/isinstancelist.pyi]
385
386 [case testWalrusConditionalTypeBinder]
387 from typing import Literal, Tuple, Union
388
389 class Good:
390 @property
391 def is_good(self) -> Literal[True]: ...
392
393 class Bad:
394 @property
395 def is_good(self) -> Literal[False]: ...
396
397 def get_thing() -> Union[Good, Bad]: ...
398
399 if (thing := get_thing()).is_good:
400 reveal_type(thing) # N: Revealed type is "__main__.Good"
401 else:
402 reveal_type(thing) # N: Revealed type is "__main__.Bad"
403
404 def get_things() -> Union[Tuple[Good], Tuple[Bad]]: ...
405
406 if (things := get_things())[0].is_good:
407 reveal_type(things) # N: Revealed type is "tuple[__main__.Good]"
408 else:
409 reveal_type(things) # N: Revealed type is "tuple[__main__.Bad]"
410 [builtins fixtures/list.pyi]
411
412 [case testWalrusConditionalTypeCheck]
413 from typing import Optional
414
415 maybe_str: Optional[str]
416
417 if (is_str := maybe_str is not None):
418 reveal_type(is_str) # N: Revealed type is "Literal[True]"
419 reveal_type(maybe_str) # N: Revealed type is "builtins.str"
420 else:
421 reveal_type(is_str) # N: Revealed type is "Literal[False]"
422 reveal_type(maybe_str) # N: Revealed type is "None"
423
424 reveal_type(maybe_str) # N: Revealed type is "builtins.str | None"
425 [builtins fixtures/bool.pyi]
426
427 [case testWalrusConditionalTypeCheck2]
428 from typing import Optional
429
430 maybe_str: Optional[str]
431
432 if (x := maybe_str) is not None:
433 reveal_type(x) # N: Revealed type is "builtins.str"
434 reveal_type(maybe_str) # N: Revealed type is "builtins.str | None"
435 else:
436 reveal_type(x) # N: Revealed type is "None"
437 reveal_type(maybe_str) # N: Revealed type is "builtins.str | None"
438
439 reveal_type(maybe_str) # N: Revealed type is "builtins.str | None"
440 [builtins fixtures/bool.pyi]
441
442 [case testWalrusPartialTypes]
443 from typing import List
444
445 def check_partial_list() -> None:
446 if (x := []): # E: Need type annotation for "x" (hint: "x: list[<type>] = ...")
447 pass
448
449 y: List[str]
450 if (y := []):
451 pass
452
453 if (z := []):
454 z.append(3)
455 reveal_type(z) # N: Revealed type is "builtins.list[builtins.int]"
456 [builtins fixtures/list.pyi]
457
458 [case testWalrusAssignmentAndConditionScopeForLiteral]
459 # flags: --warn-unreachable
460
461 if (x := 0):
462 reveal_type(x) # E: Statement is unreachable
463 else:
464 reveal_type(x) # N: Revealed type is "Literal[0]"
465
466 reveal_type(x) # N: Revealed type is "Literal[0]"
467
468 [case testWalrusAssignmentAndConditionScopeForProperty]
469 # flags: --warn-unreachable
470 from typing import Literal
471
472 class PropertyWrapper:
473 @property
474 def f(self) -> str: ...
475 @property
476 def always_false(self) -> Literal[False]: ...
477
478 wrapper = PropertyWrapper()
479
480 if x := wrapper.f:
481 reveal_type(x) # N: Revealed type is "builtins.str"
482 else:
483 reveal_type(x) # N: Revealed type is "Literal['']"
484
485 reveal_type(x) # N: Revealed type is "builtins.str"
486
487 if y := wrapper.always_false:
488 reveal_type(y) # E: Statement is unreachable
489 else:
490 reveal_type(y) # N: Revealed type is "Literal[False]"
491
492 reveal_type(y) # N: Revealed type is "Literal[False]"
493 [builtins fixtures/property.pyi]
494
495 [case testWalrusAssignmentAndConditionScopeForFunction]
496 # flags: --warn-unreachable
497 from typing import Literal
498
499 def f() -> str: ...
500
501 if x := f():
502 reveal_type(x) # N: Revealed type is "builtins.str"
503 else:
504 reveal_type(x) # N: Revealed type is "Literal['']"
505
506 reveal_type(x) # N: Revealed type is "builtins.str"
507
508 def always_false() -> Literal[False]: ...
509
510 if y := always_false():
511 reveal_type(y) # E: Statement is unreachable
512 else:
513 reveal_type(y) # N: Revealed type is "Literal[False]"
514
515 reveal_type(y) # N: Revealed type is "Literal[False]"
516
517 def always_false_with_parameter(x: int) -> Literal[False]: ...
518
519 if z := always_false_with_parameter(5):
520 reveal_type(z) # E: Statement is unreachable
521 else:
522 reveal_type(z) # N: Revealed type is "Literal[False]"
523
524 reveal_type(z) # N: Revealed type is "Literal[False]"
525 [builtins fixtures/tuple.pyi]
526
527 [case testWalrusExpr]
528 def func() -> None:
529 foo = Foo()
530 if x := foo.x:
531 pass
532
533 class Foo:
534 def __init__(self) -> None:
535 self.x = 123
536
537 [case testWalrusTypeGuard]
538 from typing_extensions import TypeGuard
539 def is_float(a: object) -> TypeGuard[float]: pass
540 def main(a: object) -> None:
541 if is_float(x := a):
542 reveal_type(x) # N: Revealed type is "builtins.float"
543 reveal_type(a) # N: Revealed type is "builtins.object"
544 [builtins fixtures/tuple.pyi]
545
546 [case testWalrusRedefined]
547 def foo() -> None:
548 x = 0
549 [x := x + y for y in [1, 2, 3]]
550 [builtins fixtures/dict.pyi]
551
552 [case testWalrusUsedBeforeDef]
553 class C:
554 def f(self, c: 'C') -> None: pass
555
556 (x := C()).f(y) # E: Cannot determine type of "y" # E: Name "y" is used before definition
557 (y := C()).f(y)
558
559 [case testOverloadWithPositionalOnlySelf]
560 from typing import overload, Optional
561
562 class Foo:
563 @overload
564 def f(self, a: str, /) -> None: ...
565
566 @overload
567 def f(self, *, b: bool = False) -> None: ...
568
569 def f(self, a: Optional[str] = None, /, *, b: bool = False) -> None: # E: Overloaded function implementation does not accept all possible parameters of signature 2
570 ...
571
572 class Bar:
573 @overload
574 def f(self, a: str, /) -> None: ...
575
576 @overload # Notice `/` in sig below:
577 def f(self, /, *, b: bool = False) -> None: ...
578
579 def f(self, a: Optional[str] = None, /, *, b: bool = False) -> None:
580 ...
581 [builtins fixtures/bool.pyi]
582
583 [case testOverloadPositionalOnlyErrorMessage]
584 from typing import overload
585
586 @overload
587 def foo(a: int, /): ...
588 @overload
589 def foo(a: str): ...
590 def foo(a): ...
591
592 foo(a=1)
593 [out]
594 main:9: error: No overload variant of "foo" matches argument type "int"
595 main:9: note: Possible overload variants:
596 main:9: note: def foo(int, /) -> Any
597 main:9: note: def foo(a: str) -> Any
598
599 [case testOverloadPositionalOnlyErrorMessageAllTypes]
600 from typing import overload
601
602 @overload
603 def foo(a: int, /, b: int, *, c: int): ...
604 @overload
605 def foo(a: str, b: int, *, c: int): ...
606 def foo(a, b, *, c): ...
607
608 foo(a=1)
609 [out]
610 main:9: error: No overload variant of "foo" matches argument type "int"
611 main:9: note: Possible overload variants:
612 main:9: note: def foo(int, /, b: int, *, c: int) -> Any
613 main:9: note: def foo(a: str, b: int, *, c: int) -> Any
614
615 [case testOverloadPositionalOnlyErrorMessageMultiplePosArgs]
616 from typing import overload
617
618 @overload
619 def foo(a: int, b: int, c: int, /, d: str): ...
620 @overload
621 def foo(a: str, b: int, c: int, d: str): ...
622 def foo(a, b, c, d): ...
623
624 foo(a=1)
625 [out]
626 main:9: error: No overload variant of "foo" matches argument type "int"
627 main:9: note: Possible overload variants:
628 main:9: note: def foo(int, int, int, /, d: str) -> Any
629 main:9: note: def foo(a: str, b: int, c: int, d: str) -> Any
630
631 [case testOverloadPositionalOnlyErrorMessageMethod]
632 from typing import overload
633
634 class Some:
635 @overload
636 def foo(self, __a: int): ...
637 @overload
638 def foo(self, a: float, /): ...
639 @overload
640 def foo(self, a: str): ...
641 def foo(self, a): ...
642
643 Some().foo(a=1)
644 [out]
645 main:12: error: No overload variant of "foo" of "Some" matches argument type "int"
646 main:12: note: Possible overload variants:
647 main:12: note: def foo(self, int, /) -> Any
648 main:12: note: def foo(self, float, /) -> Any
649 main:12: note: def foo(self, a: str) -> Any
650
651 [case testOverloadPositionalOnlyErrorMessageClassMethod]
652 from typing import overload
653
654 class Some:
655 @overload
656 @classmethod
657 def foo(cls, __a: int): ...
658 @overload
659 @classmethod
660 def foo(cls, a: float, /): ...
661 @overload
662 @classmethod
663 def foo(cls, a: str): ...
664 @classmethod
665 def foo(cls, a): ...
666
667 Some.foo(a=1)
668 [builtins fixtures/classmethod.pyi]
669 [out]
670 main:16: error: No overload variant of "foo" of "Some" matches argument type "int"
671 main:16: note: Possible overload variants:
672 main:16: note: def foo(cls, int, /) -> Any
673 main:16: note: def foo(cls, float, /) -> Any
674 main:16: note: def foo(cls, a: str) -> Any
675
676 [case testUnpackWithDuplicateNamePositionalOnly]
677 from typing import TypedDict
678 from typing_extensions import Unpack
679
680 class Person(TypedDict):
681 name: str
682 age: int
683 def foo(name: str, /, **kwargs: Unpack[Person]) -> None: # Allowed
684 ...
685 [builtins fixtures/dict.pyi]
686 [typing fixtures/typing-typeddict.pyi]
687
688 [case testPossiblyUndefinedWithAssignmentExpr]
689 # flags: --enable-error-code possibly-undefined
690 def f1() -> None:
691 d = {0: 1}
692 if int():
693 x = 1
694
695 if (x := d[x]) is None: # E: Name "x" may be undefined
696 y = x
697 z = x
698 [builtins fixtures/dict.pyi]
699
700 [case testNarrowOnSelfInGeneric]
701 from typing import Generic, TypeVar, Optional
702
703 T = TypeVar("T", int, str)
704
705 class C(Generic[T]):
706 x: Optional[T]
707 def meth(self) -> Optional[T]:
708 if (y := self.x) is not None:
709 reveal_type(y)
710 return None
711 [out]
712 main:9: note: Revealed type is "builtins.int"
713 main:9: note: Revealed type is "builtins.str"
714
715 [case testTypeGuardWithPositionalOnlyArg]
716 from typing_extensions import TypeGuard
717
718 def typeguard(x: object, /) -> TypeGuard[int]:
719 ...
720
721 n: object
722 if typeguard(n):
723 reveal_type(n)
724 [builtins fixtures/tuple.pyi]
725 [out]
726 main:8: note: Revealed type is "builtins.int"
727
728 [case testTypeGuardKeywordFollowingWalrus]
729 from typing import cast
730 from typing_extensions import TypeGuard
731
732 def typeguard(x: object) -> TypeGuard[int]:
733 ...
734
735 if typeguard(x=(n := cast(object, "hi"))):
736 reveal_type(n)
737 [builtins fixtures/tuple.pyi]
738 [out]
739 main:8: note: Revealed type is "builtins.int"
740
741 [case testWalrusAssignmentAcrossCallArguments]
742 def f(notifications: str, initial_reference: str) -> int:
743 return 42
744
745 f(
746 initial_reference=(request_reference := "abc"),
747 notifications=reveal_type(request_reference), # N: Revealed type is "builtins.str"
748 )
749
750 [case testNoCrashOnAssignmentExprClass]
751 class C:
752 [(j := i) for i in [1, 2, 3]] # E: Assignment expression within a comprehension cannot be used in a class body
753 [builtins fixtures/list.pyi]
754
755 [case testNarrowedVariableInNestedModifiedInWalrus]
756 from typing import Optional
757
758 def walrus_with_nested_error(x: Optional[str]) -> None:
759 if x is None:
760 x = "a"
761 def nested() -> str:
762 return x # E: Incompatible return value type (got "str | None", expected "str")
763 if x := None:
764 pass
765 nested()
766
767 def walrus_with_nested_ok(x: Optional[str]) -> None:
768 if x is None:
769 x = "a"
770 def nested() -> str:
771 return x
772 if y := x:
773 pass
774 nested()
775
776 [case testIgnoreWholeModule]
777 # flags: --warn-unused-ignores
778 # type: ignore
779 IGNORE # type: ignore
780
781 [case testUnusedIgnoreVersionCheck]
782 # flags: --warn-unused-ignores
783 import sys
784
785 if sys.version_info < (3, 6):
786 42 # type: ignore
787 else:
788 42 # type: ignore # E: Unused "type: ignore" comment
789 [builtins fixtures/ops.pyi]
790
791 [case testDictExpressionErrorLocations]
792 # flags: --pretty
793 from typing import Dict
794
795 other: Dict[str, str]
796 dct: Dict[str, int] = {"a": "b", **other}
797 [builtins fixtures/dict.pyi]
798 [out]
799 main:5: error: Dict entry 0 has incompatible type "str": "str"; expected "str": "int"
800 dct: Dict[str, int] = {"a": "b", **other}
801 ^~~~~~~~
802 main:5: error: Unpacked dict entry 1 has incompatible type "dict[str, str]"; expected "SupportsKeysAndGetItem[str, int]"
803 dct: Dict[str, int] = {"a": "b", **other}
804 ^~~~~
805
806 [case testWalrusAssignmentEmptyCollection]
807 from typing import List
808
809 y: List[int]
810 if (y := []):
811 reveal_type(y) # N: Revealed type is "builtins.list[builtins.int]"
812 [builtins fixtures/list.pyi]
813
814 [case testAssignToOptionalTupleWalrus]
815 from typing import Optional
816
817 def condition() -> bool: return False
818
819 i: Optional[int] = 0 if condition() else None
820 x: Optional[tuple[int, int]] = (i, (i := 1)) # E: Incompatible types in assignment (expression has type "tuple[int | None, int]", variable has type "tuple[int, int] | None")
821 [builtins fixtures/tuple.pyi]
822
823 [case testReturnTupleOptionalWalrus]
824 from typing import Optional
825
826 def condition() -> bool: return False
827
828 def fn() -> tuple[int, int]:
829 i: Optional[int] = 0 if condition() else None
830 return (i, (i := i + 1)) # E: Incompatible return value type (got "tuple[int | None, int]", expected "tuple[int, int]") \
831 # E: Unsupported operand types for + ("None" and "int") \
832 # N: Left operand is of type "int | None"
833 [builtins fixtures/dict.pyi]