Debian Codegraph @

test-data/unit/fine-grained-modules.test

raw ◀ ▶
1 Debian patch touch this file
1 -- Test cases for fine-grained incremental mode related to modules
2 --
3 -- Covers adding and deleting modules, changes to multiple modules,
4 -- changes to import graph, and changes to module references.
5 --
6 -- The comments in fine-grained.test explain how these tests work.
7
8
9 -- Add file
10 -- --------
11
12
13 [case testAddFile]
14 import b
15 [file b.py]
16 [file a.py.2]
17 def f() -> None: pass
18 [file b.py.3]
19 import a
20 a.f(1)
21 [out]
22 ==
23 ==
24 b.py:2: error: Too many arguments for "f"
25
26 [case testAddFileWithErrors]
27 import b
28 [file b.py]
29 [file a.py.2]
30 def f() -> str:
31 return 1
32 [file b.py.3]
33 import a
34 a.f(1)
35 [file a.py.4]
36 def f(x: int) -> None: pass
37 [out]
38 ==
39 a.py:2: error: Incompatible return value type (got "int", expected "str")
40 ==
41 a.py:2: error: Incompatible return value type (got "int", expected "str")
42 b.py:2: error: Too many arguments for "f"
43 ==
44
45 [case testAddFileFixesError]
46 import b
47 [file b.py]
48 [file b.py.2]
49 from a import f
50 f()
51 [file a.py.3]
52 def f() -> None: pass
53 [out]
54 ==
55 b.py:1: error: Cannot find implementation or library stub for module named "a"
56 b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
57 ==
58
59 [case testAddFileFixesAndGeneratesError1]
60 import b
61 [file b.py]
62 [file b.py.2]
63 from a import f
64 [file b.py.3]
65 from a import f
66 f(1)
67 [file a.py.4]
68 def f() -> None: pass
69 [out]
70 ==
71 b.py:1: error: Cannot find implementation or library stub for module named "a"
72 b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
73 ==
74 b.py:1: error: Cannot find implementation or library stub for module named "a"
75 b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
76 ==
77 b.py:2: error: Too many arguments for "f"
78
79 [case testAddFileFixesAndGeneratesError2]
80 import b
81 [file b.py]
82 [file b.py.2]
83 from a import f
84 f(1)
85 [file c.py.3]
86 x = 'whatever'
87 [file a.py.4]
88 def f() -> None: pass
89 [out]
90 ==
91 b.py:1: error: Cannot find implementation or library stub for module named "a"
92 b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
93 ==
94 b.py:1: error: Cannot find implementation or library stub for module named "a"
95 b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
96 ==
97 b.py:2: error: Too many arguments for "f"
98
99 [case testAddFileGeneratesError1]
100 # flags: --ignore-missing-imports
101 import a
102 [file a.py]
103 from b import f
104 f(1)
105 [file b.py.2]
106 def f() -> None: pass
107 [out]
108 ==
109 a.py:2: error: Too many arguments for "f"
110
111 [case testAddFilePreservesError1]
112 import b
113 [file b.py]
114 [file b.py.2]
115 from a import f
116 f(1)
117 [file x.py.3]
118 # unrelated change
119 [out]
120 ==
121 b.py:1: error: Cannot find implementation or library stub for module named "a"
122 b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
123 ==
124 b.py:1: error: Cannot find implementation or library stub for module named "a"
125 b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
126
127 [case testAddFilePreservesError2]
128 import b
129 [file b.py]
130 f()
131 [file a.py.2]
132 [out]
133 b.py:1: error: Name "f" is not defined
134 ==
135 b.py:1: error: Name "f" is not defined
136
137 [case testRemoveSubmoduleFromBuild1]
138 # cmd1: mypy a.py b/__init__.py b/c.py
139 # cmd2: mypy a.py b/__init__.py
140 # flags: --follow-imports=skip --ignore-missing-imports
141 [file a.py]
142 from b import c
143 x=1
144 [file a.py.2]
145 from b import c
146 x=2
147 [file a.py.3]
148 from b import c
149 x=3
150 [file b/__init__.py]
151 [file b/c.py]
152 [out]
153 ==
154 ==
155
156 [case testImportLineNumber1]
157 import b
158 [file b.py]
159 [file b.py.2]
160 x = 1
161 import a
162 [out]
163 ==
164 b.py:2: error: Cannot find implementation or library stub for module named "a"
165 b.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
166
167 [case testImportLineNumber2]
168 import b
169 [file b.py]
170 [file b.py.2]
171 x = 1
172 import a
173 from c import f
174 [file x.py.3]
175 [out]
176 ==
177 b.py:2: error: Cannot find implementation or library stub for module named "a"
178 b.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
179 b.py:3: error: Cannot find implementation or library stub for module named "c"
180 ==
181 b.py:2: error: Cannot find implementation or library stub for module named "a"
182 b.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
183 b.py:3: error: Cannot find implementation or library stub for module named "c"
184
185 [case testAddPackage1]
186 import p.a
187 p.a.f(1)
188 [file p/__init__.py.2]
189 [file p/a.py.2]
190 def f(x: str) -> None: pass
191 [out]
192 main:1: error: Cannot find implementation or library stub for module named "p.a"
193 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
194 main:1: error: Cannot find implementation or library stub for module named "p"
195 ==
196 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
197
198 [case testAddPackage2]
199 import p
200 p.f(1)
201 [file p/__init__.py.2]
202 from p.a import f
203 [file p/a.py.2]
204 def f(x: str) -> None: pass
205 [out]
206 main:1: error: Cannot find implementation or library stub for module named "p"
207 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
208 ==
209 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
210
211 [case testAddPackage3]
212 import p.a
213 p.a.f(1)
214 [file p/__init__.py.2]
215 [file p/a.py.3]
216 def f(x: str) -> None: pass
217 [out]
218 main:1: error: Cannot find implementation or library stub for module named "p.a"
219 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
220 main:1: error: Cannot find implementation or library stub for module named "p"
221 ==
222 main:1: error: Cannot find implementation or library stub for module named "p.a"
223 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
224 ==
225 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
226 [builtins fixtures/module.pyi]
227
228 [case testAddPackage4]
229 import p.a
230 p.a.f(1)
231 [file p/a.py.2]
232 def f(x: str) -> None: pass
233 [file p/__init__.py.3]
234 [out]
235 main:1: error: Cannot find implementation or library stub for module named "p.a"
236 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
237 main:1: error: Cannot find implementation or library stub for module named "p"
238 ==
239 main:1: error: Cannot find implementation or library stub for module named "p.a"
240 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
241 main:1: error: Cannot find implementation or library stub for module named "p"
242 ==
243 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
244
245 [case testAddNonPackageSubdir]
246 # cmd: mypy x.py
247 # cmd2: mypy x.py foo/a.py foo/b.py
248 [file x.py]
249 [file foo/a.py.2]
250 import b
251 b.foo(5)
252 [file foo/b.py.2]
253 def foo(x: str) -> None: pass
254 [out]
255 ==
256 foo/a.py:2: error: Argument 1 to "foo" has incompatible type "int"; expected "str"
257
258 [case testAddPackage5]
259 # cmd: mypy main p/a.py
260 # cmd2: mypy main p/a.py
261 # cmd3: mypy main p/a.py p/__init__.py
262 import p.a
263 p.a.f(1)
264 [file p/a.py]
265 [file p/a.py.2]
266 def f(x: str) -> None: pass
267 [file p/__init__.py.3]
268 [out]
269 main:4: error: Cannot find implementation or library stub for module named "p.a"
270 main:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
271 main:4: error: Cannot find implementation or library stub for module named "p"
272 ==
273 main:4: error: Cannot find implementation or library stub for module named "p.a"
274 main:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
275 main:4: error: Cannot find implementation or library stub for module named "p"
276 ==
277 main:5: error: Argument 1 to "f" has incompatible type "int"; expected "str"
278
279 [case testAddPackage6]
280 # flags: --follow-imports=skip --ignore-missing-imports
281 # cmd: mypy x.py
282 # cmd2: mypy x.py p/a.py
283 [file x.py]
284 import p.a
285 p.a.f(1)
286 [file p/a.py.2]
287 def f(x: str) -> None: pass
288 [file p/__init__.py.2]
289 [out]
290 ==
291 -- It is a bug (#4797) that this isn't an error, but not a fine-grained specific one
292
293 [case testAddPackage7]
294 # flags: --follow-imports=skip
295 # cmd: mypy x.py
296 # cmd2: mypy x.py p/a.py
297 [file x.py]
298 from p.a import f
299 f(1)
300 [file p/a.py.2]
301 def f(x: str) -> None: pass
302 [file p/__init__.py.2]
303 [out]
304 x.py:1: error: Cannot find implementation or library stub for module named "p.a"
305 x.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
306 ==
307 x.py:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
308
309
310 [case testAddPackage8]
311 # cmd: mypy x.py p/a.py
312 # cmd2: mypy x.py p/a.py p/__init__.py
313 # cmd3: mypy x.py p/a.py p/__init__.py
314 [file x.py]
315 [file p/a.py]
316 1+'hi'
317 [file p/__init__.py.2]
318 [file p/a.py.3]
319 1+3
320 [out]
321 p/a.py:1: error: Unsupported operand types for + ("int" and "str")
322 ==
323 p/a.py:1: error: Unsupported operand types for + ("int" and "str")
324 ==
325
326
327 -- Delete file
328 -- -----------
329
330
331 [case testDeleteBasic]
332 import a
333 [file a.py]
334 import b
335 [file b.py]
336 def f() -> None: pass
337 [file a.py.2]
338 [delete b.py.3]
339 [out]
340 ==
341 ==
342
343 [case testDeleteDepOfDunderInit1]
344 [file p/__init__.py]
345 from .foo import Foo
346 [file p/foo.py]
347 class Foo: pass
348 [file p/__init__.py.2]
349 [delete p/foo.py.2]
350 [out]
351 ==
352
353 [case testDeleteDepOfDunderInit2]
354 [file p/__init__.py]
355 from p.foo import Foo
356 [file p/foo.py]
357 class Foo: pass
358 [file p/__init__.py.2]
359 [delete p/foo.py.2]
360 [out]
361 ==
362
363 [case testDeletionTriggersImportFrom]
364 import a
365 [file a.py]
366 from b import f
367
368 def g() -> None:
369 f()
370 [file b.py]
371 def f() -> None: pass
372 [delete b.py.2]
373 [file b.py.3]
374 def f(x: int) -> None: pass
375 [out]
376 ==
377 a.py:1: error: Cannot find implementation or library stub for module named "b"
378 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
379 ==
380 a.py:4: error: Missing positional argument "x" in call to "f"
381
382 [case testDeletionTriggersImport]
383 import a
384 [file a.py]
385 def f() -> None: pass
386 [delete a.py.2]
387 [file a.py.3]
388 def f() -> None: pass
389 [out]
390 ==
391 main:1: error: Cannot find implementation or library stub for module named "a"
392 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
393 ==
394
395 [case testDeletionOfSubmoduleTriggersImportFrom1-only_when_nocache]
396 -- Different cache/no-cache tests because:
397 -- missing module error message mismatch
398 from p import q
399 [file p/__init__.py]
400 [file p/q.py]
401 [delete p/q.py.2]
402 [file p/q.py.3]
403 [out]
404 ==
405 main:1: error: Module "p" has no attribute "q"
406 -- TODO: The following messages are different compared to non-incremental mode
407 main:1: error: Cannot find implementation or library stub for module named "p.q"
408 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
409 ==
410
411 -- TODO: Fix this bug. It is a real bug that was been papered over
412 -- by the test harness.
413 [case testDeletionOfSubmoduleTriggersImportFrom1_2-only_when_cache-skip]
414 -- Different cache/no-cache tests because:
415 -- missing module error message mismatch
416 from p import q
417 [file p/__init__.py]
418 [file p/q.py]
419 [delete p/q.py.2]
420 [file p/q.py.3]
421 [out]
422 ==
423 main:1: error: Cannot find implementation or library stub for module named "p.q"
424 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
425 ==
426
427 [case testDeletionOfSubmoduleTriggersImportFrom2]
428 from p.q import f
429 f()
430 [file p/__init__.py]
431 [file p/q.py]
432 def f() -> None: pass
433 [delete p/q.py.2]
434 [file p/q.py.3]
435 def f(x: int) -> None: pass
436 [out]
437 ==
438 main:1: error: Cannot find implementation or library stub for module named "p.q"
439 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
440 ==
441 main:2: error: Missing positional argument "x" in call to "f"
442
443 [case testDeletionOfSubmoduleTriggersImport]
444 import p.q
445 [file p/__init__.py]
446 [file p/q.py]
447 def f() -> None: pass
448 [delete p/q.py.2]
449 [file p/q.py.3]
450 def f(x: int) -> None: pass
451 [out]
452 ==
453 main:1: error: Cannot find implementation or library stub for module named "p.q"
454 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
455 ==
456
457 [case testDeleteSubpackageWithNontrivialParent1]
458 [file p/__init__.py]
459 def g() -> None:
460 pass
461 [file p/b.py.2]
462 def foo() -> None: pass
463 foo()
464 [delete p/b.py.3]
465 [out]
466 ==
467 ==
468
469 [case testDeleteModuleWithError]
470 import a
471 [file a.py]
472 def f() -> int:
473 return 1
474 [file a.py.2]
475 def f() -> str:
476 return 1
477 [delete a.py.3]
478 def f() -> str:
479 return 1
480 [out]
481 ==
482 a.py:2: error: Incompatible return value type (got "int", expected "str")
483 ==
484 main:1: error: Cannot find implementation or library stub for module named "a"
485 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
486
487 [case testDeleteModuleWithErrorInsidePackage]
488 import a.b
489 [file a/__init__.py]
490 [file a/b.py]
491 def f() -> int:
492 return ''
493 [delete a/b.py.2]
494 def f() -> str:
495 return 1
496 [out]
497 a/b.py:2: error: Incompatible return value type (got "str", expected "int")
498 ==
499 main:1: error: Cannot find implementation or library stub for module named "a.b"
500 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
501
502 [case testModifyTwoFilesNoError1]
503 import a
504 [file a.py]
505 import b
506 b.f()
507 [file b.py]
508 def f() -> None: pass
509 [file a.py.2]
510 import b
511 b.f(1)
512 [file b.py.2]
513 def f(x: int) -> None: pass
514 [out]
515 ==
516
517 [case testDeleteSubpackageInit1]
518 # cmd: mypy q/r/s.py
519 # flags: --follow-imports=skip --ignore-missing-imports
520 [file q/__init__.py]
521 [file q/r/__init__.py]
522 [file q/r/s.py]
523 [delete q/__init__.py.2]
524 [out]
525 ==
526
527 [case testAddSubpackageInit2]
528 # cmd: mypy q/r/s.py
529 # flags: --follow-imports=skip --ignore-missing-imports
530 [file q/r/__init__.py]
531 [file q/r/s.py]
532 1
533 [file q/r/s.py.2]
534 2
535 [file q/__init__.py.2]
536 [out]
537 ==
538
539 [case testModifyTwoFilesNoError2]
540 import a
541 [file a.py]
542 from b import g
543 def f() -> None: pass
544 [file b.py]
545 import a
546 def g() -> None: pass
547 a.f()
548 [file a.py.2]
549 from b import g
550 def f(x: int) -> None: pass
551 [file b.py.2]
552 import a
553 def g() -> None: pass
554 a.f(1)
555 [out]
556 ==
557
558 [case testModifyTwoFilesErrorsElsewhere]
559 import a
560 import b
561 a.f()
562 b.g(1)
563 [file a.py]
564 def f() -> None: pass
565 [file b.py]
566 def g(x: int) -> None: pass
567 [file a.py.2]
568 def f(x: int) -> None: pass
569 [file b.py.2]
570 def g() -> None: pass
571 [out]
572 ==
573 main:3: error: Missing positional argument "x" in call to "f"
574 main:4: error: Too many arguments for "g"
575
576 [case testModifyTwoFilesErrorsInBoth]
577 import a
578 [file a.py]
579 import b
580 def f() -> None: pass
581 b.g(1)
582 [file b.py]
583 import a
584 def g(x: int) -> None: pass
585 a.f()
586 [file a.py.2]
587 import b
588 def f(x: int) -> None: pass
589 b.g(1)
590 [file b.py.2]
591 import a
592 def g() -> None: pass
593 a.f()
594 [out]
595 ==
596 b.py:3: error: Missing positional argument "x" in call to "f"
597 a.py:3: error: Too many arguments for "g"
598
599 [case testModifyTwoFilesFixErrorsInBoth]
600 import a
601 [file a.py]
602 import b
603 def f(x: int) -> None: pass
604 b.g(1)
605 [file b.py]
606 import a
607 def g() -> None: pass
608 a.f()
609 [file a.py.2]
610 import b
611 def f() -> None: pass
612 b.g(1)
613 [file b.py.2]
614 import a
615 def g(x: int) -> None: pass
616 a.f()
617 [out]
618 b.py:3: error: Missing positional argument "x" in call to "f"
619 a.py:3: error: Too many arguments for "g"
620 ==
621
622 [case testAddTwoFilesNoError]
623 import a
624 [file a.py]
625 import b
626 import c
627 b.f()
628 c.g()
629 [file b.py.2]
630 import c
631 def f() -> None: pass
632 c.g()
633 [file c.py.2]
634 import b
635 def g() -> None: pass
636 b.f()
637 [out]
638 a.py:1: error: Cannot find implementation or library stub for module named "b"
639 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
640 a.py:2: error: Cannot find implementation or library stub for module named "c"
641 ==
642
643 [case testAddTwoFilesErrorsInBoth]
644 import a
645 [file a.py]
646 import b
647 import c
648 b.f()
649 c.g()
650 [file b.py.2]
651 import c
652 def f() -> None: pass
653 c.g(1)
654 [file c.py.2]
655 import b
656 def g() -> None: pass
657 b.f(1)
658 [out]
659 a.py:1: error: Cannot find implementation or library stub for module named "b"
660 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
661 a.py:2: error: Cannot find implementation or library stub for module named "c"
662 ==
663 c.py:3: error: Too many arguments for "f"
664 b.py:3: error: Too many arguments for "g"
665
666 [case testAddTwoFilesErrorsElsewhere]
667 import a
668 import b
669 a.f(1)
670 b.g(1)
671 [file a.py.2]
672 def f() -> None: pass
673 [file b.py.2]
674 def g() -> None: pass
675 [out]
676 main:1: error: Cannot find implementation or library stub for module named "a"
677 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
678 main:2: error: Cannot find implementation or library stub for module named "b"
679 ==
680 main:3: error: Too many arguments for "f"
681 main:4: error: Too many arguments for "g"
682
683 [case testDeleteTwoFilesErrorsElsewhere]
684 import a
685 import b
686 a.f()
687 b.g()
688 [file a.py]
689 def f() -> None: pass
690 [file b.py]
691 def g() -> None: pass
692 [delete a.py.2]
693 [delete b.py.2]
694 [out]
695 ==
696 main:1: error: Cannot find implementation or library stub for module named "a"
697 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
698 main:2: error: Cannot find implementation or library stub for module named "b"
699
700 [case testDeleteTwoFilesNoErrors]
701 import a
702 [file a.py]
703 import b
704 import c
705 b.f()
706 c.g()
707 [file b.py]
708 def f() -> None: pass
709 [file c.py]
710 def g() -> None: pass
711 [file a.py.2]
712 [delete b.py.3]
713 [delete c.py.3]
714 [out]
715 ==
716 ==
717
718 [case testDeleteTwoFilesFixErrors]
719 import a
720 import b
721 a.f()
722 b.g()
723 [file a.py]
724 import b
725 def f() -> None: pass
726 b.g(1)
727 [file b.py]
728 import a
729 def g() -> None: pass
730 a.f(1)
731 [delete a.py.2]
732 [delete b.py.2]
733 [out]
734 b.py:3: error: Too many arguments for "f"
735 a.py:3: error: Too many arguments for "g"
736 ==
737 main:1: error: Cannot find implementation or library stub for module named "a"
738 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
739 main:2: error: Cannot find implementation or library stub for module named "b"
740
741 [case testAddFileWhichImportsLibModule]
742 import a
743 a.x = 0
744 [file a.py.2]
745 import sys
746 x = sys.platform
747 [builtins fixtures/tuple.pyi]
748 [out]
749 main:1: error: Cannot find implementation or library stub for module named "a"
750 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
751 ==
752 main:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")
753
754 [case testAddFileWhichImportsLibModuleWithErrors]
755 # flags: --no-silence-site-packages
756 import a
757 a.x = 0
758 [file a.py.2]
759 import broken
760 x = broken.x
761 z
762 [out]
763 main:2: error: Cannot find implementation or library stub for module named "a"
764 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
765 ==
766 a.py:3: error: Name "z" is not defined
767 <ROOT>/test-data/unit/lib-stub/broken.pyi:2: error: Name "y" is not defined
768
769 [case testRenameModule]
770 import a
771 [file a.py]
772 import b
773 b.f()
774 [file b.py]
775 def f() -> None: pass
776 [file a.py.2]
777 import c
778 c.f()
779 [file c.py.2]
780 def f() -> None: pass
781 [file a.py.3]
782 import c
783 c.f(1)
784 [out]
785 ==
786 ==
787 a.py:2: error: Too many arguments for "f"
788
789 [case testDeleteFileWSuperClass]
790 # flags: --ignore-missing-imports
791 [file a.py]
792 from c import Bar
793 from b import Foo
794 z = (1, Foo())
795 [file b.py]
796 from e import Quux
797 from d import Baz
798 class Foo(Baz, Quux):
799 pass
800
801 [file e.py]
802 from c import Bar
803 class Quux(Bar):
804 pass
805 [file c.py]
806 class Bar:
807 pass
808 [file d.py]
809 class Baz:
810 pass
811 [delete c.py.2]
812 [builtins fixtures/tuple.pyi]
813 [out]
814 ==
815
816 [case testDeleteFileWithinPackage]
817
818 import a
819 [file a.py]
820 import m.x
821 m.x.g(1)
822 [file m/__init__.py]
823 [file m/x.py]
824 def g() -> None: pass
825 [delete m/x.py.2]
826 [builtins fixtures/module.pyi]
827 [out]
828 a.py:2: error: Too many arguments for "g"
829 ==
830 a.py:1: error: Cannot find implementation or library stub for module named "m.x"
831 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
832 a.py:2: error: Module has no attribute "x"
833
834 [case testDeletePackage1]
835 import p.a
836 p.a.f(1)
837 [file p/__init__.py]
838 [file p/a.py]
839 def f(x: str) -> None: pass
840 [delete p.2]
841 [out]
842 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
843 ==
844 main:1: error: Cannot find implementation or library stub for module named "p.a"
845 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
846 main:1: error: Cannot find implementation or library stub for module named "p"
847
848 [case testDeletePackage2]
849 import p
850 p.f(1)
851 [file p/__init__.py]
852 from p.a import f
853 [file p/a.py]
854 def f(x: str) -> None: pass
855 [delete p.2]
856 [out]
857 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
858 ==
859 main:1: error: Cannot find implementation or library stub for module named "p"
860 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
861
862 [case testDeletePackage3]
863
864 import p.a
865 p.a.f(1)
866 [file p/__init__.py]
867 [file p/a.py]
868 def f(x: str) -> None: pass
869 [delete p/a.py.2]
870 [delete p.3]
871 [builtins fixtures/module.pyi]
872 [out]
873 main:3: error: Argument 1 to "f" has incompatible type "int"; expected "str"
874 ==
875 main:2: error: Cannot find implementation or library stub for module named "p.a"
876 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
877 main:3: error: Module has no attribute "a"
878 ==
879 main:2: error: Cannot find implementation or library stub for module named "p.a"
880 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
881 main:2: error: Cannot find implementation or library stub for module named "p"
882
883 [case testDeletePackage4]
884 # flags: --no-namespace-packages
885 import p.a
886 p.a.f(1)
887 [file p/a.py]
888 def f(x: str) -> None: pass
889 [file p/__init__.py]
890 [delete p/__init__.py.2]
891 [delete p.3]
892 [out]
893 main:3: error: Argument 1 to "f" has incompatible type "int"; expected "str"
894 ==
895 main:2: error: Cannot find implementation or library stub for module named "p.a"
896 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
897 main:2: error: Cannot find implementation or library stub for module named "p"
898 ==
899 main:2: error: Cannot find implementation or library stub for module named "p.a"
900 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
901 main:2: error: Cannot find implementation or library stub for module named "p"
902
903 [case testDeletePackage5]
904 # flags: --no-namespace-packages
905 # cmd1: mypy -m main -m p.a -m p.__init__
906 # cmd2: mypy -m main -m p.a
907 # cmd3: mypy -m main
908
909 import p.a
910 p.a.f(1)
911 [file p/a.py]
912 def f(x: str) -> None: pass
913 [file p/__init__.py]
914 [delete p/__init__.py.2]
915 [delete p.3]
916 [out]
917 main:7: error: Argument 1 to "f" has incompatible type "int"; expected "str"
918 ==
919 main:6: error: Cannot find implementation or library stub for module named "p.a"
920 main:6: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
921 main:6: error: Cannot find implementation or library stub for module named "p"
922 ==
923 main:6: error: Cannot find implementation or library stub for module named "p.a"
924 main:6: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
925 main:6: error: Cannot find implementation or library stub for module named "p"
926
927
928 [case testDeletePackage6]
929 # flags: --no-namespace-packages
930 # cmd1: mypy -m p.a -m p.b -m p.__init__
931 # cmd2: mypy -m p.a -m p.b
932 # cmd3: mypy -m p.a -m p.b
933 [file p/a.py]
934 def f(x: str) -> None: pass
935 [file p/b.py]
936 from p.a import f
937 f(12)
938 [file p/__init__.py]
939 [delete p/__init__.py.2]
940 [file p/b.py.3]
941 from a import f
942 f(12)
943 [out]
944 p/b.py:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
945 ==
946 p/b.py:1: error: Cannot find implementation or library stub for module named "p.a"
947 p/b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
948 ==
949 p/b.py:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
950
951
952 -- TODO:
953 -- - add one file which imports another new file, blocking error in new file
954 -- - arbitrary blocking errors
955 -- - order of processing makes a difference
956 -- - mix of modify, add and delete in one iteration
957
958
959 -- Controlling imports using command line options
960 -- ----------------------------------------------
961
962
963 [case testIgnoreMissingImports]
964 # flags: --ignore-missing-imports
965 import a
966 [file a.py]
967 import b
968 import c
969 [file c.py]
970 [delete c.py.2]
971 [file b.py.3]
972 import d
973 1 + ''
974 [out]
975 ==
976 ==
977 b.py:2: error: Unsupported operand types for + ("int" and "str")
978
979 [case testSkipImports]
980 # cmd: mypy main a.py
981 # flags: --follow-imports=skip --ignore-missing-imports
982 import a
983 [file a.py]
984 import b
985 [file b.py]
986 1 + ''
987 class A: pass
988 [file a.py.2]
989 import b
990 reveal_type(b)
991 reveal_type(b.A)
992 [file a.py.3]
993 import b
994 reveal_type(b)
995 reveal_type(b.A)
996 [file b.py.3]
997 1 + ''
998 class A: pass
999 [out]
1000 ==
1001 a.py:2: note: Revealed type is "Any"
1002 a.py:3: note: Revealed type is "Any"
1003 ==
1004 a.py:2: note: Revealed type is "Any"
1005 a.py:3: note: Revealed type is "Any"
1006
1007 [case testSkipImportsWithinPackage]
1008 # cmd: mypy a/b.py
1009 # flags: --follow-imports=skip --ignore-missing-imports
1010 [file a/__init__.py]
1011 1 + ''
1012 [file a/b.py]
1013 import a.c
1014 [file a/b.py.2]
1015 import a.c
1016 import x
1017 reveal_type(a.c)
1018 [file a/b.py.3]
1019 import a.c
1020 import x
1021 1 + ''
1022 [out]
1023 ==
1024 a/b.py:3: note: Revealed type is "Any"
1025 ==
1026 a/b.py:3: error: Unsupported operand types for + ("int" and "str")
1027
1028 [case testDeleteModuleWithinPackageInitIgnored-only_when_nocache]
1029 -- Disabled in cache mode because incorrect behavior:
1030 -- Having deleted files specified on command line seems dodgy, though.
1031 # cmd: mypy x.py a/b.py
1032 # flags: --follow-imports=skip --ignore-missing-imports
1033 [file x.py]
1034 import a.b
1035 [file a/__init__.py]
1036 [file a/b.py]
1037 x = 1
1038 [delete a/b.py.2]
1039 [out]
1040 ==
1041
1042 [case testAddImport]
1043 import what.b
1044 [file aaa/__init__.py]
1045 [file aaa/z.py]
1046 def foo(x: int) -> None:
1047 pass
1048 [file aaa/z.py.2]
1049 import config
1050 def foo() -> None:
1051 pass
1052 [file what/__init__.py]
1053 [file what/b.py]
1054 import config
1055 import aaa.z
1056 def main() -> None:
1057 aaa.z.foo(5)
1058 [file what/b.py.2]
1059 import aaa.z
1060 def main() -> None:
1061 aaa.z.foo()
1062 [file config.py]
1063 [out]
1064 ==
1065
1066 [case testAddImport2]
1067 import what.b
1068 [file aaa/__init__.py]
1069 [file aaa/z.py]
1070 def foo(x: int) -> None:
1071 pass
1072 [file aaa/z.py.2]
1073 def foo() -> None:
1074 pass
1075 [file what/__init__.py]
1076 [file what/b.py]
1077 import aaa.z
1078 def main() -> None:
1079 aaa.z.foo(5)
1080 [file what/b.py.2]
1081 import config
1082 import aaa.z
1083 def main() -> None:
1084 aaa.z.foo()
1085 [file config.py]
1086 [out]
1087 ==
1088
1089
1090 -- Misc
1091 -- ----
1092
1093
1094 [case testChangeModuleToVariable]
1095 from a import m
1096 m.x
1097 [file a.py]
1098 from b import m
1099 [file b.py]
1100 import m
1101 [file b.py.2]
1102 m = ''
1103 [file m.py]
1104 x = 1
1105 [file m2.py]
1106 [out]
1107 ==
1108 main:2: error: "str" has no attribute "x"
1109
1110 [case testChangeVariableToModule]
1111 from a import m
1112 y: str = m
1113 [file a.py]
1114 from b import m
1115 [file b.py]
1116 m = ''
1117 [file b.py.2]
1118 import m
1119 [file m.py]
1120 x = 1
1121 [file m2.py]
1122 [builtins fixtures/module.pyi]
1123 [out]
1124 ==
1125 main:2: error: Incompatible types in assignment (expression has type Module, variable has type "str")
1126
1127 [case testRefreshImportOfIgnoredModule1]
1128 # flags: --follow-imports=skip --ignore-missing-imports
1129 # cmd: mypy c.py a/__init__.py b.py
1130 [file c.py]
1131 from a import a2
1132 import b
1133 b.x
1134 [file a/__init__.py]
1135 [file b.py]
1136 x = 0
1137 [file b.py.2]
1138 x = ''
1139 [file b.py.3]
1140 x = 0
1141 [file a/a2.py]
1142 [out]
1143 ==
1144 ==
1145
1146 [case testRefreshImportOfIgnoredModule2]
1147 # flags: --follow-imports=skip --ignore-missing-imports
1148 # cmd: mypy c.py a/__init__.py b.py
1149 [file c.py]
1150 from a import a2
1151 import b
1152 b.x
1153 [file a/__init__.py]
1154 [file b.py]
1155 x = 0
1156 [file b.py.2]
1157 x = ''
1158 [file b.py.3]
1159 x = 0
1160 [file a/a2/__init__.py]
1161 [out]
1162 ==
1163 ==
1164
1165 [case testIncrementalWithIgnoresTwice]
1166 import a
1167 [file a.py]
1168 import b
1169 import foo # type: ignore
1170 [file b.py]
1171 x = 1
1172 [file b.py.2]
1173 x = 'hi'
1174 [file b.py.3]
1175 x = 1
1176 [out]
1177 ==
1178 ==
1179
1180 [case testIgnoredImport2]
1181 import x
1182 [file y.py]
1183 import xyz # type: ignore
1184 B = 0
1185 from x import A
1186 [file x.py]
1187 A = 0
1188 from y import B
1189 [file x.py.2]
1190 A = 1
1191 from y import B
1192 [file x.py.3]
1193 A = 2
1194 from y import B
1195 [out]
1196 ==
1197 ==
1198
1199 [case testDeleteIndirectDependency]
1200 import b
1201 b.x.foo()
1202 [file b.py]
1203 import c
1204 x = c.Foo()
1205 [file c.py]
1206 class Foo:
1207 def foo(self) -> None: pass
1208 [delete c.py.2]
1209 [file b.py.2]
1210 class Foo:
1211 def foo(self) -> None: pass
1212 x = Foo()
1213 [file b.py.3]
1214 class Foo:
1215 def foo(self, x: int) -> None: pass
1216 x = Foo()
1217 [out]
1218 ==
1219 ==
1220 main:2: error: Missing positional argument "x" in call to "foo" of "Foo"
1221
1222 -- This series of tests is designed to test adding a new module that
1223 -- does not appear in the cache, for cache mode. They are run in
1224 -- cache mode only because stale and rechecked differ heavily between
1225 -- the modes.
1226 [case testAddModuleAfterCache1-only_when_cache]
1227 # cmd: mypy main a.py
1228 # cmd2: mypy main a.py b.py
1229 # cmd3: mypy main a.py b.py
1230 import a
1231 [file a.py]
1232 pass
1233 [file a.py.2]
1234 import b
1235 b.foo(0)
1236 [file b.py.2]
1237 def foo() -> None: pass
1238
1239 [stale a, b]
1240 [rechecked a, b]
1241
1242 [file b.py.3]
1243 def foo(x: int) -> None: pass
1244
1245 [stale2 b]
1246 [rechecked2 b]
1247
1248 [out]
1249 ==
1250 a.py:2: error: Too many arguments for "foo"
1251 ==
1252
1253 [case testAddModuleAfterCache2-only_when_cache]
1254 # cmd: mypy main a.py
1255 # cmd2: mypy main a.py b.py
1256 # cmd3: mypy main a.py b.py
1257 # flags: --ignore-missing-imports --follow-imports=skip
1258 import a
1259 [file a.py]
1260 import b
1261 b.foo(0)
1262 [file b.py.2]
1263 def foo() -> None: pass
1264
1265 [stale b]
1266 [rechecked a, b]
1267
1268 [file b.py.3]
1269 def foo(x: int) -> None: pass
1270
1271 [stale2 b]
1272
1273 [out]
1274 ==
1275 a.py:2: error: Too many arguments for "foo"
1276 ==
1277
1278 [case testAddModuleAfterCache3-only_when_cache]
1279 # cmd: mypy main a.py
1280 # cmd2: mypy main a.py b.py c.py d.py e.py f.py g.py h.py i.py j.py
1281 # cmd3: mypy main a.py b.py c.py d.py e.py f.py g.py h.py i.py j.py
1282 # flags: --ignore-missing-imports --follow-imports=skip
1283 import a
1284 [file a.py]
1285 import b, c, d, e, f, g, h, i, j
1286 b.foo(10)
1287 [file b.py.2]
1288 def foo() -> None: pass
1289 [file c.py.2]
1290 [file d.py.2]
1291 [file e.py.2]
1292 [file f.py.2]
1293 [file g.py.2]
1294 [file h.py.2]
1295 [file i.py.2]
1296 [file j.py.2]
1297
1298 -- No files should be stale or reprocessed in the first step since the large number
1299 -- of missing files will force build to give up on cache loading.
1300 [stale]
1301
1302 [file b.py.3]
1303 def foo(x: int) -> None: pass
1304 [stale2 b]
1305
1306 [out]
1307 ==
1308 a.py:2: error: Too many arguments for "foo"
1309 ==
1310
1311
1312 [case testAddModuleAfterCache4-only_when_cache]
1313 # cmd: mypy main a.py
1314 # cmd2: mypy main a.py b.py
1315 # cmd3: mypy main a.py b.py
1316 # flags: --ignore-missing-imports --follow-imports=skip
1317 import a
1318 import b
1319 [file a.py]
1320 def foo() -> None: pass
1321 [file b.py.2]
1322 import a
1323 a.foo(10)
1324 [file a.py.3]
1325 def foo(x: int) -> None: pass
1326 [out]
1327 ==
1328 b.py:2: error: Too many arguments for "foo"
1329 ==
1330
1331 [case testAddModuleAfterCache5-only_when_cache]
1332 # cmd: mypy main a.py
1333 # cmd2: mypy main a.py b.py
1334 # cmd3: mypy main a.py b.py
1335 # flags: --ignore-missing-imports --follow-imports=skip
1336 import a
1337 import b
1338 [file a.py]
1339 def foo(x: int) -> None: pass
1340 [file a.py.2]
1341 def foo() -> None: pass
1342 [file b.py.2]
1343 import a
1344 a.foo(10)
1345
1346 [stale a, b]
1347
1348 [file a.py.3]
1349 def foo(x: int) -> None: pass
1350
1351 [stale2 a]
1352
1353 [out]
1354 ==
1355 b.py:2: error: Too many arguments for "foo"
1356 ==
1357
1358 [case testAddModuleAfterCache6-only_when_cache]
1359 # cmd: mypy main a.py
1360 # cmd2: mypy main a.py b.py
1361 # cmd3: mypy main a.py b.py
1362 # flags: --ignore-missing-imports --follow-imports=skip
1363 import a
1364 [file a.py]
1365 import b
1366 b.foo()
1367
1368 [file a.py.2]
1369 import b
1370 b.foo(0)
1371 [file b.py.2]
1372 def foo() -> None: pass
1373
1374 [stale a, b]
1375
1376 [file b.py.3]
1377 def foo(x: int) -> None: pass
1378
1379 [stale2 b]
1380
1381 [out]
1382 ==
1383 a.py:2: error: Too many arguments for "foo"
1384 ==
1385
1386 [case testRenameAndDeleteModuleAfterCache-only_when_cache]
1387 import a
1388 [file a.py]
1389 from b1 import f
1390 f()
1391 [file b1.py]
1392 def f() -> None: pass
1393 [file b2.py.2]
1394 def f() -> None: pass
1395 [delete b1.py.2]
1396 [file a.py.2]
1397 from b2 import f
1398 f()
1399
1400 -- in cache mode, there is no way to know about b1 yet
1401 [stale a, b2]
1402
1403 [out]
1404 ==
1405
1406 [case testDeleteModuleAfterCache-only_when_cache]
1407 import a
1408 [file a.py]
1409 from b import f
1410 f()
1411 [file b.py]
1412 def f() -> None: pass
1413 [delete b.py.2]
1414
1415 -- in cache mode, there is no way to know about b yet,
1416 -- but a should get flagged as changed by the initial cache
1417 -- check, since one of its dependencies is missing.
1418 [stale a]
1419
1420 [out]
1421 ==
1422 a.py:1: error: Cannot find implementation or library stub for module named "b"
1423 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
1424
1425
1426 [case testRefreshImportIfMypyElse1]
1427 import a
1428 [file a.py]
1429 from b import foo
1430
1431 MYPY = False
1432 if MYPY:
1433 x = 0
1434 else:
1435 from fictional import x
1436
1437 x = 1
1438 [file b/__init__.py]
1439 [file b/foo.py]
1440 [file b/__init__.py.2]
1441 # Dummy change
1442 [builtins fixtures/primitives.pyi]
1443 [out]
1444 ==
1445
1446 [case testImportStarPropagateChange1]
1447 from b import f
1448 f()
1449 [file b.py]
1450 from c import *
1451 [file c.py]
1452 def f() -> None: pass
1453 [file c.py.2]
1454 def f(x: int) -> None: pass
1455 [out]
1456 ==
1457 main:2: error: Missing positional argument "x" in call to "f"
1458
1459 [case testImportStarPropagateChange2]
1460 from b import *
1461 f()
1462 [file b.py]
1463 def f() -> None: pass
1464 [file b.py.2]
1465 def f(x: int) -> None: pass
1466 [out]
1467 ==
1468 main:2: error: Missing positional argument "x" in call to "f"
1469
1470 [case testImportStarAddMissingDependency1]
1471 from b import f
1472 f()
1473 [file b.py]
1474 from c import *
1475 [file c.py]
1476 [file c.py.2]
1477 def f(x: int) -> None: pass
1478 [out]
1479 main:1: error: Module "b" has no attribute "f"
1480 ==
1481 main:2: error: Missing positional argument "x" in call to "f"
1482
1483 [case testImportStarAddMissingDependency2]
1484 from b import *
1485 f()
1486 [file b.py]
1487 [file b.py.2]
1488 def f(x: int) -> None: pass
1489 [out]
1490 main:2: error: Name "f" is not defined
1491 ==
1492 main:2: error: Missing positional argument "x" in call to "f"
1493
1494 [case testImportStarAddMissingDependencyWithinClass]
1495 class A:
1496 from b import *
1497 f()
1498 x: C
1499 [file b.py]
1500 [file b.py.2]
1501 def f(x: int) -> None: pass
1502 [file b.py.3]
1503 def f(x: int) -> None: pass
1504 class C: pass
1505 [file b.py.4]
1506 def f() -> None: pass
1507 class C: pass
1508 [out]
1509 main:3: error: Name "f" is not defined
1510 main:4: error: Name "C" is not defined
1511 ==
1512 main:2: error: Unsupported class scoped import
1513 main:4: error: Name "C" is not defined
1514 ==
1515 main:2: error: Unsupported class scoped import
1516 ==
1517 main:2: error: Unsupported class scoped import
1518
1519 [case testImportStarAddMissingDependencyInsidePackage1]
1520 from p.b import f
1521 f()
1522 [file p/__init__.py]
1523 [file p/b.py]
1524 from p.c import *
1525 [file p/c.py]
1526 [file p/c.py.2]
1527 def f(x: int) -> None: pass
1528 [out]
1529 main:1: error: Module "p.b" has no attribute "f"
1530 ==
1531 main:2: error: Missing positional argument "x" in call to "f"
1532
1533 [case testImportStarAddMissingDependencyInsidePackage2]
1534 import p.a
1535 [file p/__init__.py]
1536 [file p/a.py]
1537 from p.b import *
1538 f()
1539 [file p/b.py]
1540 [file p/b.py.2]
1541 def f(x: int) -> None: pass
1542 [out]
1543 p/a.py:2: error: Name "f" is not defined
1544 ==
1545 p/a.py:2: error: Missing positional argument "x" in call to "f"
1546
1547 [case testImportStarRemoveDependency1]
1548 from b import f
1549 f()
1550 [file b.py]
1551 from c import *
1552 [file c.py]
1553 def f() -> None: pass
1554 [file c.py.2]
1555 [out]
1556 ==
1557 main:1: error: Module "b" has no attribute "f"
1558
1559 [case testImportStarRemoveDependency2]
1560 from b import *
1561 f()
1562 [file b.py]
1563 def f() -> None: pass
1564 [file b.py.2]
1565 [out]
1566 ==
1567 main:2: error: Name "f" is not defined
1568
1569 [case testImportStarWithinFunction]
1570 def f() -> None:
1571 from m import *
1572 f()
1573 [file m.py]
1574 [file m.py.2]
1575 def f(x: int) -> None: pass
1576 [file m.py.3]
1577 def f() -> None: pass
1578 [out]
1579 ==
1580 main:3: error: Missing positional argument "x" in call to "f"
1581 ==
1582
1583 [case testImportStarMutuallyRecursive-skip]
1584 # FIXME: busted with new analyzer?
1585 import a
1586 [file a.py]
1587 from b import *
1588 [file b.py]
1589 from a import *
1590 [file b.py.2]
1591 from a import *
1592 x = 0
1593 [file b.py.3]
1594 from a import *
1595 x = ''
1596 [out]
1597 ==
1598 ==
1599
1600 [case testImportStarSomethingMoved]
1601 import p
1602 [file p.py]
1603 from r2 import *
1604 [file r2.py]
1605 class A: pass
1606
1607 [file p.py.2]
1608 from r1 import *
1609 from r2 import *
1610 [file r2.py.2]
1611
1612 [file r1.py.2]
1613 class A: pass
1614
1615 [out]
1616 ==
1617
1618 [case testImportStarOverlap]
1619 from b import *
1620 from c import * # type: ignore
1621 [file b.py]
1622 from d import T
1623 [file c.py]
1624 from d import T
1625 [file c.py.2]
1626 from d import T
1627 z = 10
1628 [file d.py]
1629 from typing import TypeVar
1630 T = TypeVar('T')
1631 [out]
1632 ==
1633
1634 [case testImportStarOverlap2]
1635 from b import *
1636 import typing
1637 def foo(x: typing.List[int]) -> int:
1638 return x[0]
1639 [file b.py]
1640 import typing
1641 z = 10
1642 [file b.py.2]
1643 import typing
1644 z = '10'
1645 [builtins fixtures/list.pyi]
1646 [out]
1647 ==
1648
1649
1650 [case testImportStarOverlap3]
1651 from b import *
1652 from c import typing
1653 def foo(x: typing.List[int]) -> int:
1654 return x[0]
1655 [file b.py]
1656 import typing
1657 z = 10
1658 [file b.py.2]
1659 import typing
1660 z = '10'
1661 [file c.py]
1662 import typing
1663 z = 10
1664 [builtins fixtures/list.pyi]
1665 [out]
1666 ==
1667
1668
1669 [case testImportPartialAssign]
1670 import a
1671 [file a.py]
1672 from c import *
1673 from b import A, x
1674 [file b.py]
1675 A = 10
1676 x = 1
1677 [file b.py.2]
1678 class A: pass
1679 x = 1
1680 [file c.py]
1681 x = 10
1682 [out]
1683 ==
1684
1685 [case testDeleteFileWithErrors]
1686 # cmd: mypy main a.py
1687 # cmd2: mypy main
1688 # flags: --follow-imports=skip --ignore-missing-imports
1689 import a
1690 [file a.py]
1691 def f() -> None:
1692 1()
1693 ''()
1694 [file b.py.2]
1695 # unrelated change
1696 [out]
1697 a.py:2: error: "int" not callable
1698 a.py:3: error: "str" not callable
1699 ==
1700
1701 [case testAddAndUseClass1]
1702 [file a.py]
1703 [file a.py.2]
1704 from b import Foo
1705 def bar(f: Foo) -> None:
1706 f.foo(12)
1707 [file b.py.2]
1708 class Foo:
1709 def foo(self, s: str) -> None: pass
1710 [out]
1711 ==
1712 a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"
1713
1714 [case testAddAndUseClass2]
1715 [file a.py]
1716 [file a.py.3]
1717 from b import Foo
1718 def bar(f: Foo) -> None:
1719 f.foo(12)
1720 [file b.py.2]
1721 class Foo:
1722 def foo(self, s: str) -> None: pass
1723 [out]
1724 ==
1725 ==
1726 a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"
1727
1728 [case testAddAndUseClass3]
1729 # flags: --ignore-missing-imports
1730 [file a.py]
1731 [file a.py.2]
1732 from b import Foo
1733 def bar(f: Foo) -> None:
1734 f.foo(12)
1735 [file b.py.3]
1736 class Foo:
1737 def foo(self, s: str) -> None: pass
1738 [out]
1739 ==
1740 ==
1741 a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"
1742
1743 [case testAddAndUseClass4]
1744 [file a.py]
1745 [file a.py.2]
1746 from b import *
1747 def bar(f: Foo) -> None:
1748 f.foo(12)
1749 [file b.py.2]
1750 class Foo:
1751 def foo(self, s: str) -> None: pass
1752 [out]
1753 ==
1754 a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"
1755
1756 [case testAddAndUseClass4_2]
1757 [file a.py]
1758 [file a.py.2]
1759 from p.b import *
1760 def bar(f: Foo) -> None:
1761 f.foo(12)
1762 [file p/__init__.py]
1763 [file p/b.py.2]
1764 class Foo:
1765 def foo(self, s: str) -> None: pass
1766 [out]
1767 ==
1768 a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"
1769
1770 [case testAddAndUseClass5]
1771 [file a.py]
1772 [file a.py.2]
1773 from b import *
1774 def bar(f: Foo) -> None:
1775 f.foo(12)
1776 [file b.py.2]
1777 class Foo:
1778 def foo(self, s: str) -> None: pass
1779 [out]
1780 ==
1781 a.py:3: error: Argument 1 to "foo" of "Foo" has incompatible type "int"; expected "str"
1782
1783 [case testSkipButDontIgnore1]
1784 # cmd: mypy a.py c.py
1785 # flags: --follow-imports=skip
1786 [file a.py]
1787 import b
1788 from c import x
1789 [file b.py]
1790 1+'lol'
1791 [file c.py]
1792 x = 1
1793 [file c.py.2]
1794 x = '2'
1795 [file b.py.3]
1796 [out]
1797 ==
1798 ==
1799
1800 [case testSkipButDontIgnore2]
1801 # cmd: mypy a.py c.py
1802 # flags: --follow-imports=skip
1803 [file a.py]
1804 from c import x
1805 import b
1806 [file b.py]
1807 [file c.py]
1808 x = 1
1809 [file b.py.2]
1810 1+'x'
1811 [file c.py.2]
1812 x = '2'
1813 [file c.py.3]
1814 x = 2
1815 [delete b.py.3]
1816 [out]
1817 ==
1818 ==
1819 a.py:2: error: Cannot find implementation or library stub for module named "b"
1820 a.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
1821
1822 [case testErrorButDontIgnore1]
1823 # cmd: mypy a.py c.py
1824 # flags: --follow-imports=error
1825 [file a.py]
1826 from c import x
1827 import b
1828 [file b.py]
1829 [file c.py]
1830 x = 1
1831 [file c.py.2]
1832 x = '2'
1833 [out]
1834 a.py:2: error: Import of "b" ignored
1835 a.py:2: note: (Using --follow-imports=error, module not passed on command line)
1836 ==
1837 a.py:2: error: Import of "b" ignored
1838 a.py:2: note: (Using --follow-imports=error, module not passed on command line)
1839
1840 [case testErrorButDontIgnore2]
1841 # cmd1: mypy a.py c.py b.py
1842 # cmd2: mypy a.py c.py
1843 # flags: --follow-imports=error
1844 [file a.py]
1845 from c import x
1846 import b
1847 [file b.py]
1848 [file c.py]
1849 x = 1
1850 [file c.py.2]
1851 x = '2'
1852 [out]
1853 ==
1854 a.py:2: error: Import of "b" ignored
1855 a.py:2: note: (Using --follow-imports=error, module not passed on command line)
1856
1857 -- TODO: This test fails because p.b does not depend on p (#4847)
1858 [case testErrorButDontIgnore3-skip]
1859 # cmd1: mypy a.py c.py p/b.py p/__init__.py
1860 # cmd2: mypy a.py c.py p/b.py
1861 # flags: --follow-imports=error
1862 [file a.py]
1863 from c import x
1864 from p.b import y
1865 [file p/b.py]
1866 y = 12
1867 [file p/__init__.py]
1868 [file c.py]
1869 x = 1
1870 [file c.py.2]
1871 x = '2'
1872 [out]
1873 ==
1874 p/b.py: error: Ancestor package "p" ignored
1875 p/b.py: note: (Using --follow-imports=error, submodule passed on command line)
1876
1877 [case testErrorButDontIgnore4]
1878 # cmd: mypy a.py z.py p/b.py p/__init__.py
1879 # cmd2: mypy a.py p/b.py
1880 # flags: --follow-imports=error
1881 [file a.py]
1882 from p.b import y
1883 [file p/b.py]
1884 from z import x
1885 y = 12
1886 [file p/__init__.py]
1887 [file z.py]
1888 x = 1
1889 [delete z.py.2]
1890 [out]
1891 ==
1892 p/b.py: error: Ancestor package "p" ignored
1893 p/b.py: note: (Using --follow-imports=error, submodule passed on command line)
1894 p/b.py:1: error: Cannot find implementation or library stub for module named "z"
1895 p/b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
1896
1897 [case testTurnPackageToModule]
1898 [file a.py]
1899 [file b.py]
1900 import p
1901 x = p.f()
1902 [file p/__init__.py]
1903 def f() -> int: pass
1904 [delete p/__init__.py.2]
1905 [file p.py.2]
1906 def f() -> str: pass
1907 [file a.py.3]
1908 import b
1909 reveal_type(b.x)
1910 [out]
1911 ==
1912 ==
1913 a.py:2: note: Revealed type is "builtins.str"
1914
1915 [case testModuleToPackage]
1916 [file a.py]
1917 [file b.py]
1918 import p
1919 x = p.f()
1920 [file p.py]
1921 def f() -> str: pass
1922 [delete p.py.2]
1923 [file p/__init__.py.2]
1924 def f() -> int: pass
1925 [file a.py.3]
1926 import b
1927 reveal_type(b.x)
1928 [out]
1929 ==
1930 ==
1931 a.py:2: note: Revealed type is "builtins.int"
1932
1933 [case testQualifiedSubpackage1]
1934 [file c/__init__.py]
1935 [file c/a.py]
1936 from lurr import x
1937 from c.d import f
1938
1939 [file c/d.py]
1940 def f() -> None: pass
1941 def g(x: int) -> None: pass
1942 [file lurr.py]
1943 x = 10
1944 [file lurr.py.2]
1945 x = '10'
1946 [out]
1947 ==
1948
1949 [case testImportedMissingSubpackage]
1950 # flags: --follow-imports=skip --ignore-missing-imports
1951 # cmd: mypy a.py b/__init__.py
1952 [file a.py]
1953 from b.foo import bar
1954 x = 10
1955 [file b/__init__.py]
1956 [file a.py.2]
1957 from b.foo import bar
1958 x = '10'
1959 [out]
1960 ==
1961
1962 [case testFineAddedMissingStubs]
1963 # flags: --ignore-missing-imports
1964 from missing import f
1965 f(int())
1966 [file missing.pyi.2]
1967 def f(x: str) -> None: pass
1968 [out]
1969 ==
1970 main:3: error: Argument 1 to "f" has incompatible type "int"; expected "str"
1971
1972 [case testFineAddedMissingStubsPackage]
1973 # flags: --ignore-missing-imports
1974 import package.missing
1975 package.missing.f(int())
1976 [file package/__init__.pyi.2]
1977 [file package/missing.pyi.2]
1978 def f(x: str) -> None: pass
1979 [out]
1980 ==
1981 main:3: error: Argument 1 to "f" has incompatible type "int"; expected "str"
1982
1983 [case testFineAddedMissingStubsPackageFrom]
1984 # flags: --ignore-missing-imports
1985 from package import missing
1986 missing.f(int())
1987 [file package/__init__.pyi.2]
1988 [file package/missing.pyi.2]
1989 def f(x: str) -> None: pass
1990 [out]
1991 ==
1992 main:3: error: Argument 1 to "f" has incompatible type "int"; expected "str"
1993
1994 [case testFineAddedMissingStubsPackagePartial]
1995 # flags: --ignore-missing-imports
1996 import package.missing
1997 package.missing.f(int())
1998 [file package/__init__.pyi]
1999 [file package/missing.pyi.2]
2000 def f(x: str) -> None: pass
2001 [out]
2002 ==
2003 main:3: error: Argument 1 to "f" has incompatible type "int"; expected "str"
2004
2005 [case testFineAddedMissingStubsPackagePartialGetAttr]
2006 import package.missing
2007 package.missing.f(int())
2008 [file package/__init__.pyi]
2009 from typing import Any
2010 def __getattr__(attr: str) -> Any: ...
2011 [file package/missing.pyi.2]
2012 def f(x: str) -> None: pass
2013 [out]
2014 ==
2015 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
2016
2017 [case testFineAddedMissingStubsIgnore]
2018 from missing import f # type: ignore
2019 f(int())
2020 [file missing.pyi.2]
2021 def f(x: str) -> None: pass
2022 [out]
2023 ==
2024 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
2025
2026 [case testFineAddedMissingStubsIgnorePackage]
2027 import package.missing # type: ignore
2028 package.missing.f(int())
2029 [file package/__init__.pyi.2]
2030 [file package/missing.pyi.2]
2031 def f(x: str) -> None: pass
2032 [out]
2033 ==
2034 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
2035
2036 [case testFineAddedMissingStubsIgnorePackageFrom]
2037 from package import missing # type: ignore
2038 missing.f(int())
2039 [file package/__init__.pyi.2]
2040 [file package/missing.pyi.2]
2041 def f(x: str) -> None: pass
2042 [out]
2043 ==
2044 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
2045
2046 [case testFineAddedMissingStubsIgnorePackagePartial]
2047 import package.missing # type: ignore
2048 package.missing.f(int())
2049 [file package/__init__.pyi]
2050 [file package/missing.pyi.2]
2051 def f(x: str) -> None: pass
2052 [out]
2053 ==
2054 main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
2055
2056 [case testFineFollowImportSkipNotInvalidatedOnPresent]
2057 # flags: --follow-imports=skip
2058 # cmd: mypy main.py
2059 [file main.py]
2060 import other
2061 [file other.py]
2062 x = 1
2063 [file other.py.2]
2064 x = 'hi'
2065 [stale]
2066 [rechecked]
2067 [out]
2068 ==
2069
2070 [case testFineFollowImportSkipNotInvalidatedOnPresentPackage]
2071 # flags: --follow-imports=skip
2072 # cmd: mypy main.py
2073 [file main.py]
2074 import other
2075 [file other/__init__.py]
2076 x = 1
2077 [file other/__init__.py.2]
2078 x = 'hi'
2079 [stale]
2080 [rechecked]
2081 [out]
2082 ==
2083
2084 [case testFineFollowImportSkipNotInvalidatedOnAdded]
2085 # flags: --follow-imports=skip --ignore-missing-imports
2086 # cmd: mypy main.py
2087 [file main.py]
2088 import other
2089 [file other.py.2]
2090 x = 1
2091 [stale]
2092 [rechecked]
2093 [out]
2094 ==
2095
2096 -- TODO: Fix this: stubs should be followed normally even with follow-imports=skip
2097 [case testFineFollowImportSkipInvalidatedOnAddedStub-skip]
2098 # flags: --follow-imports=skip --ignore-missing-imports
2099 # cmd: mypy main.py
2100 [file main.py]
2101 import other
2102 x: str = other.x
2103 [file other.pyi.2]
2104 x = 1
2105 [stale main, other]
2106 [rechecked main, other]
2107 [out]
2108 ==
2109 main:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")
2110
2111 [case testFineAddedSkippedStubsPackageFrom]
2112 # flags: --follow-imports=skip --ignore-missing-imports
2113 # cmd: mypy main.py
2114 # cmd2: mypy main.py package/__init__.py package/missing.py
2115 [file main.py]
2116 from package import missing
2117 missing.f(int())
2118 [file package/__init__.py]
2119 [file package/missing.py]
2120 def f(x: str) -> None: pass
2121 [out]
2122 ==
2123 main.py:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
2124
2125 [case testIgnoredAttrReprocessedModule]
2126 import a
2127 [file a.py]
2128 import b
2129 x = b.x # type: ignore
2130 y: int = x
2131 [file b.py]
2132 import c
2133 [file b.py.2]
2134 import c
2135 x = c.x
2136 [file c.py]
2137 x: str
2138 [out]
2139 ==
2140 a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
2141
2142 [case testMissingStubAdded1]
2143 # flags: --follow-imports=skip
2144 # cmd: mypy main.py
2145
2146 [file main.py]
2147 import foo
2148 foo.x = 1
2149 [file foo.pyi.2]
2150 x = 'x'
2151 [file main.py.3]
2152 import foo
2153 foo.x = 'y'
2154 [out]
2155 main.py:1: error: Cannot find implementation or library stub for module named "foo"
2156 main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2157 ==
2158 main.py:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")
2159 ==
2160
2161 [case testMissingStubAdded2]
2162 # flags: --follow-imports=skip
2163 # cmd: mypy main.py
2164
2165 [file main.py]
2166 import foo # type: ignore
2167 foo.x = 1
2168 [file foo.pyi.2]
2169 x = 'x'
2170 [file main.py.3]
2171 import foo
2172 foo.x = 'y'
2173 [out]
2174 ==
2175 main.py:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")
2176 ==
2177
2178 [case testDoNotFollowImportToNonStubFile]
2179 # flags: --follow-imports=skip
2180 # cmd: mypy main.py
2181
2182 [file main.py]
2183 import foo # type: ignore
2184 foo.x = 1
2185 [file foo.py.2]
2186 x = 'x'
2187 1 + 'x'
2188
2189 [out]
2190 ==
2191
2192 [case testLibraryStubsNotInstalled]
2193 import a
2194 [file a.py]
2195 import requests
2196 [file a.py.2]
2197 # nothing
2198 [file a.py.3]
2199 import jack
2200 [out]
2201 a.py:1: error: Library stubs not installed for "requests"
•2202 a.py:1: note: Hint: On Debian systems, you can install the python3-typeshed package to provide mypy with stubs for many popular libraries. In virtual Python environments, you can instead run "python3 -m pip install types-requests".
2203 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2204 ==
2205 ==
2206 a.py:1: error: Library stubs not installed for "jack"
•2207 a.py:1: note: Hint: On Debian systems, you can install the python3-typeshed package to provide mypy with stubs for many popular libraries. In virtual Python environments, you can instead run "python3 -m pip install types-JACK-Client".
2208 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2209
2210 [case testIgnoreErrorsFromTypeshed]
2211 # flags: --custom-typeshed-dir tmp/ts --follow-imports=normal
2212 # cmd1: mypy a.py
2213 # cmd2: mypy a.py
2214
2215 [file a.py]
2216 import foobar
2217
2218 [file ts/stdlib/abc.pyi]
2219 [file ts/stdlib/builtins.pyi]
2220 class object: pass
2221 class str: pass
2222 class ellipsis: pass
2223 [file ts/stdlib/sys.pyi]
2224 [file ts/stdlib/types.pyi]
2225 [file ts/stdlib/typing.pyi]
2226 def cast(x): ...
2227 [file ts/stdlib/typing_extensions.pyi]
2228 [file ts/stdlib/VERSIONS]
2229 [file ts/stubs/mypy_extensions/mypy_extensions.pyi]
2230
2231 [file ts/stdlib/foobar.pyi.2]
2232 # We report no errors from typeshed. It would be better to test ignoring
2233 # errors from PEP 561 packages, but it's harder to test and uses the
2234 # same code paths, so we are using typeshed instead.
2235 import baz
2236 import zar
2237 undefined
2238
2239 [file ts/stdlib/baz.pyi.2]
2240 import whatever
2241 undefined
2242
2243 [out]
2244 a.py:1: error: Cannot find implementation or library stub for module named "foobar"
2245 a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2246 ==
2247
2248 [case testDaemonImportMapRefresh]
2249 # cmd: mypy main.py
2250 [file main.py]
2251 [file main.py.2]
2252 import a.b
2253 reveal_type(a.b.foo())
2254 [file a/__init__.pyi]
2255 [file a/b.pyi]
2256 import a.c
2257 def foo() -> a.c.C: ...
2258 [file a/c.pyi]
2259 class C: ...
2260 [out]
2261 ==
2262 main.py:2: note: Revealed type is "a.c.C"