Debian Codegraph @

test-data/unit/cmdline.test

raw
1 -- Tests for command line parsing
2 -- ------------------------------
3 --
4 -- The initial line specifies the command line, in the format
5 --
6 -- # cmd: mypy <options>
7 --
8 -- Note that # flags: --some-flag IS NOT SUPPORTED.
9 -- Use # cmd: mypy --some-flag ...
10 --
11 -- '== Return code: <value>' is added to the output when the process return code
12 -- is "nonobvious" -- that is, when it is something other than 0 if there are no
13 -- messages and 1 if there are.
14
15 -- Directories/packages on the command line
16 -- ----------------------------------------
17
18 [case testCmdlinePackage]
19 # cmd: mypy pkg
20 [file pkg/__init__.py]
21 [file pkg/a.py]
22 undef
23 [file pkg/subpkg/__init__.py]
24 [file pkg/subpkg/a.py]
25 undef
26 import pkg.subpkg.a
27 [out]
28 pkg/a.py:1: error: Name "undef" is not defined
29 pkg/subpkg/a.py:1: error: Name "undef" is not defined
30
31 [case testCmdlinePackageSlash]
32 # cmd: mypy pkg/
33 [file pkg/__init__.py]
34 [file pkg/a.py]
35 undef
36 [file pkg/subpkg/__init__.py]
37 [file pkg/subpkg/a.py]
38 undef
39 import pkg.subpkg.a
40 [out]
41 pkg/a.py:1: error: Name "undef" is not defined
42 pkg/subpkg/a.py:1: error: Name "undef" is not defined
43
44 [case testCmdlineNonPackage]
45 # cmd: mypy dir
46 [file dir/a.py]
47 undef
48 [file dir/subdir/b.py]
49 undef
50 [out]
51 dir/a.py:1: error: Name "undef" is not defined
52 dir/subdir/b.py:1: error: Name "undef" is not defined
53
54 [case testCmdlineNonPackageDuplicate]
55 # cmd: mypy dir
56 [file dir/a.py]
57 undef
58 [file dir/subdir/a.py]
59 undef
60 [out]
61 dir/a.py: error: Duplicate module named "a" (also at "dir/subdir/a.py")
62 dir/a.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
63 dir/a.py: note: Common resolutions include:
64 dir/a.py: note: a) using `--exclude` to avoid checking one of them,
65 dir/a.py: note: b) adding `__init__.py` somewhere,
66 dir/a.py: note: c) using `--explicit-package-bases` or adjusting `MYPYPATH`
67 == Return code: 2
68
69 [case testCmdlineNonPackageSlash]
70 # cmd: mypy dir/
71 [file dir/a.py]
72 undef
73 import b
74 [file dir/subdir/b.py]
75 undef
76 import a
77 [out]
78 dir/a.py:1: error: Name "undef" is not defined
79 dir/subdir/b.py:1: error: Name "undef" is not defined
80
81 [case testCmdlinePackageContainingSubdir]
82 # cmd: mypy pkg
83 [file pkg/__init__.py]
84 [file pkg/a.py]
85 undef
86 import pkg.a
87 [file pkg/subdir/a.py]
88 undef
89 import pkg.a
90 [out]
91 pkg/a.py:1: error: Name "undef" is not defined
92 pkg/subdir/a.py:1: error: Name "undef" is not defined
93
94 [case testCmdlineNonPackageContainingPackage]
95 # cmd: mypy dir
96 [file dir/a.py]
97 undef
98 import subpkg.a
99 [file dir/subpkg/__init__.py]
100 [file dir/subpkg/a.py]
101 undef
102 [out]
103 dir/subpkg/a.py:1: error: Name "undef" is not defined
104 dir/a.py:1: error: Name "undef" is not defined
105
106 [case testCmdlineInvalidPackageName]
107 # cmd: mypy dir/sub.pkg/a.py
108 [file dir/sub.pkg/__init__.py]
109 [file dir/sub.pkg/a.py]
110 undef
111 [out]
112 sub.pkg contains __init__.py but is not a valid Python package name
113 == Return code: 2
114
115 [case testBadFileEncoding]
116 # cmd: mypy a.py
117 [file a.py]
118 # coding: uft-8
119 [out]
120 a.py: error: Cannot decode file: unknown encoding: uft-8
121 == Return code: 2
122
123 [case testCannotIgnoreDuplicateModule]
124 # cmd: mypy one/mod/__init__.py two/mod/__init__.py
125 [file one/mod/__init__.py]
126 # type: ignore
127 [file two/mod/__init__.py]
128 # type: ignore
129 [out]
130 two/mod/__init__.py: error: Duplicate module named "mod" (also at "one/mod/__init__.py")
131 two/mod/__init__.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
132 two/mod/__init__.py: note: Common resolutions include:
133 two/mod/__init__.py: note: a) using `--exclude` to avoid checking one of them,
134 two/mod/__init__.py: note: b) adding `__init__.py` somewhere,
135 two/mod/__init__.py: note: c) using `--explicit-package-bases` or adjusting `MYPYPATH`
136 == Return code: 2
137
138 [case testPerFileConfigSectionMultipleMatchesDisallowed]
139 # cmd: mypy xx.py xy.py yx.py yy.py
140 [file mypy.ini]
141 \[mypy]
142 \[mypy-*x*]
143 disallow_untyped_defs = True
144 \[mypy-*y*]
145 disallow_untyped_calls = True
146 [file xx.py]
147 def f(a): pass
148 def g(a: int) -> int: return f(a)
149 [file xy.py]
150 def f(a): pass
151 def g(a: int) -> int: return f(a)
152 [file yx.py]
153 def f(a): pass
154 def g(a: int) -> int: return f(a)
155 [file yy.py]
156 def f(a): pass
157 def g(a: int) -> int: return f(a)
158 [out]
159 mypy.ini: [mypy-*x*]: Patterns must be fully-qualified module names, optionally with '*' in some components (e.g spam.*.eggs.*)
160 mypy.ini: [mypy-*y*]: Patterns must be fully-qualified module names, optionally with '*' in some components (e.g spam.*.eggs.*)
161 == Return code: 0
162
163 [case testConfigErrorNoSection]
164 # cmd: mypy -c pass
165 [file mypy.ini]
166 [out]
167 mypy.ini: No [mypy] section in config file
168 == Return code: 0
169
170 [case testConfigErrorUnknownFlag]
171 # cmd: mypy -c pass
172 [file mypy.ini]
173 \[mypy]
174 bad = 0
175 [out]
176 mypy.ini: [mypy]: Unrecognized option: bad = 0
177 == Return code: 0
178
179 [case testConfigErrorBadFlag]
180 # cmd: mypy a.py
181 [file mypy.ini]
182 \[mypy]
183 disallow-untyped-defs = True
184 [file a.py]
185 def f():
186 pass
187 [out]
188 mypy.ini: [mypy]: Unrecognized option: disallow-untyped-defs = True
189 == Return code: 0
190
191 [case testConfigErrorBadBoolean]
192 # cmd: mypy -c pass
193 [file mypy.ini]
194 \[mypy]
195 ignore_missing_imports = nah
196 [out]
197 mypy.ini: [mypy]: ignore_missing_imports: Not a boolean: nah
198 == Return code: 0
199
200 [case testConfigErrorNotPerFile]
201 # cmd: mypy -c pass
202 [file mypy.ini]
203 \[mypy]
204 \[mypy-*]
205 python_version = 3.11
206 [out]
207 mypy.ini: [mypy-*]: Per-module sections should only specify per-module flags (python_version)
208 == Return code: 0
209
210 [case testConfigFollowImportsSysPath]
211 # cmd: mypy main.py
212 [file main.py]
213 from a import x
214 x + 0
215 x + '' # E
216 import a
217 a.x + 0
218 a.x + '' # E
219 a.y # E
220 a + 0 # E
221 [file mypy.ini]
222 \[mypy]
223 follow_imports = normal
224 no_silence_site_packages = True
225 [file pypath/a/__init__.py]
226 x = 0
227 x += '' # Error reported here
228 [file pypath/a/py.typed]
229 [out]
230 pypath/a/__init__.py:2: error: Unsupported operand types for + ("int" and "str")
231 main.py:3: error: Unsupported operand types for + ("int" and "str")
232 main.py:6: error: Unsupported operand types for + ("int" and "str")
233 main.py:7: error: Module has no attribute "y"
234 main.py:8: error: Unsupported operand types for + (Module and "int")
235
236 [case testConfigFollowImportsInvalid]
237 # cmd: mypy main.py
238 [file mypy.ini]
239 \[mypy]
240 follow_imports =True
241 [file main.py]
242 [out]
243 mypy.ini: [mypy]: follow_imports: invalid choice 'True' (choose from 'normal', 'silent', 'skip', 'error')
244 == Return code: 0
245
246 [case testFailedImportOnWrongCWD]
247 # cmd: mypy main.py
248 # cwd: main/subdir1/subdir2
249 [file main/subdir1/subdir2/main.py]
250 import parent
251 import grandparent
252 import missing
253 [file main/subdir1/subdir2/__init__.py]
254 [file main/subdir1/parent.py]
255 [file main/subdir1/__init__.py]
256 [file main/grandparent.py]
257 [file main/__init__.py]
258 [out]
259 main.py:1: error: Cannot find implementation or library stub for module named "parent"
260 main.py:1: note: You may be running mypy in a subpackage, mypy should be run on the package root
261 main.py:2: error: Cannot find implementation or library stub for module named "grandparent"
262 main.py:3: error: Cannot find implementation or library stub for module named "missing"
263 main.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
264
265 [case testImportInParentButNoInit]
266 # cmd: mypy main.py
267 # cwd: main/not_a_package
268 [file main/not_a_package/main.py]
269 import needs_init
270 [file main/needs_init.py]
271 [file main/__init__.py]
272 [out]
273 main.py:1: error: Cannot find implementation or library stub for module named "needs_init"
274 main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
275
276 [case testConfigNoErrorForUnknownXFlagInSubsection]
277 # cmd: mypy -c pass
278 [file mypy.ini]
279 \[mypy]
280 \[mypy-foo]
281 x_bad = 0
282 [out]
283
284 [case testDotInFilenameOKScript]
285 # cmd: mypy a.b.py c.d.pyi
286 [file a.b.py]
287 undef
288 [file c.d.pyi]
289 whatever
290 [out]
291 c.d.pyi:1: error: Name "whatever" is not defined
292 a.b.py:1: error: Name "undef" is not defined
293
294 [case testDotInFilenameOKFolder]
295 # cmd: mypy my.folder
296 [file my.folder/tst.py]
297 undef
298 [out]
299 my.folder/tst.py:1: error: Name "undef" is not defined
300
301 [case testDotInFilenameNoImport]
302 # cmd: mypy main.py
303 [file main.py]
304 import a.b
305 [file a.b.py]
306 whatever
307 [out]
308 main.py:1: error: Cannot find implementation or library stub for module named "a.b"
309 main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
310 main.py:1: error: Cannot find implementation or library stub for module named "a"
311
312 [case testPythonVersionWrongFormatPyProjectTOML]
313 # cmd: mypy -c pass
314 [file pyproject.toml]
315 \[tool.mypy]
316 python_version = 3.10
317 [out]
318 pyproject.toml: [mypy]: python_version: Python 3.1 is not supported (must be 3.10 or higher). You may need to put quotes around your Python version
319 == Return code: 0
320
321 [case testPythonVersionTooOld10]
322 # cmd: mypy -c pass
323 [file mypy.ini]
324 \[mypy]
325 python_version = 1.0
326 [out]
327 mypy.ini: [mypy]: python_version: Python major version '1' out of range (must be 3)
328 == Return code: 0
329
330 [case testPythonVersionTooOld39]
331 # cmd: mypy -c pass
332 [file mypy.ini]
333 \[mypy]
334 python_version = 3.9
335 [out]
336 mypy.ini: [mypy]: python_version: Python 3.9 is not supported (must be 3.10 or higher)
337 == Return code: 0
338
339 [case testPythonVersionTooNew40]
340 # cmd: mypy -c pass
341 [file mypy.ini]
342 \[mypy]
343 python_version = 4.0
344 [out]
345 mypy.ini: [mypy]: python_version: Python major version '4' out of range (must be 3)
346 == Return code: 0
347
348 [case testPythonVersionTooDead27]
349 # cmd: mypy -c pass
350 [file mypy.ini]
351 \[mypy]
352 python_version = 2.7
353 [out]
354 usage: mypy [-h] [-v] [-V] [more options; see below]
355 [-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
356 mypy: error: Mypy no longer supports checking Python 2 code. Consider pinning to mypy<0.980 if you need to check Python 2 code.
357 == Return code: 2
358
359 [case testPythonVersionAccepted310]
360 # cmd: mypy -c pass
361 [file mypy.ini]
362 \[mypy]
363 python_version = 3.10
364 [out]
365
366 [case testPythonVersionAccepted315]
367 # cmd: mypy -c pass
368 [file mypy.ini]
369 \[mypy]
370 python_version = 3.15
371 [out]
372
373 [case testPythonVersionFallback]
374 # cmd: mypy main.py
375 [file main.py]
376 import sys
377 if sys.version_info < (3, 11): # Update here when bumping the min Python version!
378 reveal_type("good")
379 [file mypy.ini]
380 \[mypy]
381 python_version = 3.9
382 [out]
383 mypy.ini: [mypy]: python_version: Python 3.9 is not supported (must be 3.10 or higher)
384 main.py:3: note: Revealed type is "Literal['good']?"
385 == Return code: 0
386
387 -- This should be a dumping ground for tests of plugins that are sensitive to
388 -- typeshed changes.
389 [case testTypeshedSensitivePlugins]
390 # cmd: mypy int_pow.py
391
392 [file int_pow.py]
393 a = 1
394 b = a + 2
395 reveal_type(a**0)
396 reveal_type(a**1)
397 reveal_type(a**2)
398 reveal_type(a**-0)
399 reveal_type(a**-1)
400 reveal_type(a**(-2))
401 reveal_type(a**b)
402 reveal_type(a.__pow__(2))
403 reveal_type(a.__pow__(a))
404 [out]
405 int_pow.py:3: note: Revealed type is "Literal[1]"
406 int_pow.py:4: note: Revealed type is "int"
407 int_pow.py:5: note: Revealed type is "int"
408 int_pow.py:6: note: Revealed type is "Literal[1]"
409 int_pow.py:7: note: Revealed type is "float"
410 int_pow.py:8: note: Revealed type is "float"
411 int_pow.py:9: note: Revealed type is "Any"
412 int_pow.py:10: note: Revealed type is "int"
413 int_pow.py:11: note: Revealed type is "Any"
414 == Return code: 0
415
416 [case testMissingFile]
417 # cmd: mypy nope.py
418 [out]
419 mypy: error: Cannot read file 'nope.py': No such file or directory
420 == Return code: 2
421
422 [case testModulesAndPackages]
423 # cmd: mypy --package p.a --package p.b --module c
424 [file p/__init__.py]
425 [file p/a.py]
426 def foo(x):
427 # type: (int) -> str
428 return "x"
429 foo("wrong")
430 [file p/b/__init__.py]
431 from ..a import foo
432 def bar(a):
433 # type: (int) -> str
434 return foo(a)
435 bar("wrong")
436 [file c.py]
437 import p.b
438 p.b.bar("wrong")
439 [out]
440 p/a.py:4: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
441 p/b/__init__.py:5: error: Argument 1 to "bar" has incompatible type "str"; expected "int"
442 c.py:2: error: Argument 1 to "bar" has incompatible type "str"; expected "int"
443
444 [case testSrcPEP420Packages]
445 # cmd: mypy -p anamespace --namespace-packages
446 [file mypy.ini]
447 \[mypy]
448 mypy_path = src
449 [file src/setup.cfg]
450 [file src/anamespace/foo/__init__.py]
451 [file src/anamespace/foo/bar.py]
452 def bar(a: int, b: int) -> str:
453 return a + b
454 [out]
455 src/anamespace/foo/bar.py:2: error: Incompatible return value type (got "int", expected "str")
456
457 [case testNestedPEP420Packages]
458 # cmd: mypy -p pkg --namespace-packages
459 [file pkg/a1/b/c/d/e.py]
460 x = 0 # type: str
461 [file pkg/a1/b/f.py]
462 from pkg.a1.b.c.d.e import x
463 x()
464
465 [file pkg/a2/__init__.py]
466 [file pkg/a2/b/c/d/e.py]
467 x = 0 # type: str
468 [file pkg/a2/b/f.py]
469 from pkg.a2.b.c.d.e import x
470 x()
471 [out]
472 pkg/a2/b/c/d/e.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
473 pkg/a1/b/c/d/e.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
474 pkg/a2/b/f.py:2: error: "str" not callable
475 pkg/a1/b/f.py:2: error: "str" not callable
476
477 [case testShadowFile1]
478 # cmd: mypy --shadow-file source.py shadow.py source.py
479 [file source.py]
480 def foo() -> str:
481 return "bar"
482 [file shadow.py]
483 def bar() -> str:
484 return 14
485 [out]
486 source.py:2: error: Incompatible return value type (got "int", expected "str")
487
488 [case testShadowFile2]
489 # cmd: mypy --shadow-file s1.py shad1.py --shadow-file s2.py shad2.py --shadow-file s3.py shad3.py s1.py s2.py s3.py s4.py
490 [file s1.py]
491 def foo() -> str:
492 return "bar"
493 [file shad1.py]
494 def bar() -> str:
495 return 14
496 [file s2.py]
497 def baz() -> str:
498 return 14
499 [file shad2.py]
500 def baz() -> int:
501 return 14
502 [file s3.py]
503 def qux() -> str:
504 return "bar"
505 [file shad3.py]
506 def foo() -> int:
507 return [42]
508 [file s4.py]
509 def foo() -> str:
510 return 9
511 [out]
512 s4.py:2: error: Incompatible return value type (got "int", expected "str")
513 s3.py:2: error: Incompatible return value type (got "list[int]", expected "int")
514 s1.py:2: error: Incompatible return value type (got "int", expected "str")
515
516 [case testShadowFileWithPretty]
517 # cmd: mypy a.py --pretty --shadow-file a.py b.py
518 [file a.py]
519 b: bytes
520 [file b.py]
521 a: int = ""
522 b: bytes = 1
523 [out]
524 a.py:1: error: Incompatible types in assignment (expression has type "str",
525 variable has type "int")
526 a: int = ""
527 ^~
528 a.py:2: error: Incompatible types in assignment (expression has type "int",
529 variable has type "bytes")
530 b: bytes = 1
531 ^
532
533 [case testShadowFileWithNativeParser]
534 # cmd: mypy --native-parser --shadow-file source.py shadow.py source.py
535 [file source.py]
536 x: int = 1
537 reveal_type(x)
538 [file shadow.py]
539 x: str = "y"
540 reveal_type(x)
541 [out]
542 source.py:2: note: Revealed type is "str"
543 == Return code: 0
544
545 [case testShadowFileWithNativeParserParallel]
546 # cmd: mypy --native-parser --num-workers=4 --shadow-file a.py a_shadow.py main.py a.py b.py
547 [file main.py]
548 import a
549 import b
550 [file a.py]
551 x: int = 1
552 reveal_type(x)
553 [file b.py]
554 y: int = 2
555 [file a_shadow.py]
556 x: str = "y"
557 reveal_type(x)
558 [out]
559 a.py:2: note: Revealed type is "str"
560 == Return code: 0
561
562 [case testConfigWarnUnusedSection1]
563 # cmd: mypy foo.py quux.py spam/eggs.py
564 [file mypy.ini]
565 \[mypy]
566 warn_unused_configs = True
567 \[mypy-bar]
568 \[mypy-foo]
569 \[mypy-baz.*]
570 \[mypy-quux.*]
571 \[mypy-spam.*]
572 \[mypy-spam.eggs]
573 \[mypy-emarg.*]
574 \[mypy-emarg.hatch]
575 -- Currently we don't treat an unstructured pattern like a.*.b as unused
576 -- if it matches another section (like a.x.b). This would be reasonable
577 -- to change. '
578 \[mypy-a.*.b]
579 \[mypy-a.*.c]
580 \[mypy-a.x.b]
581 [file foo.py]
582 [file quux.py]
583 [file spam/__init__.py]
584 [file spam/eggs.py]
585 [out]
586 mypy.ini: note: unused section(s): [mypy-bar], [mypy-baz.*], [mypy-emarg.*], [mypy-emarg.hatch], [mypy-a.*.c], [mypy-a.x.b]
587 == Return code: 0
588
589 [case testPackageRootEmpty]
590 # cmd: mypy --no-namespace-packages --package-root= a/b/c.py main.py
591 [file a/b/c.py]
592 [file main.py]
593 import a.b.c
594
595 [case testPackageRootEmptyNamespacePackage]
596 # cmd: mypy --namespace-packages --package-root= a/b/c.py main.py
597 [file a/b/c.py]
598 [file main.py]
599 import a.b.c
600
601 [case testPackageRootNonEmpty]
602 # cmd: mypy --package-root=a/ a/b/c.py main.py
603 [file a/b/c.py]
604 [file main.py]
605 import b.c
606
607 [case testPackageRootMultiple1]
608 # cmd: mypy --package-root=. --package-root=a a/b/c.py d.py main.py
609 [file a/b/c.py]
610 [file d.py]
611 [file main.py]
612 import b.c
613 import d
614
615 [case testPackageRootMultiple2]
616 # cmd: mypy --package-root=a/ --package-root=./ a/b/c.py d.py main.py
617 [file a/b/c.py]
618 [file d.py]
619 [file main.py]
620 import b.c
621 import d
622
623 [case testPackageRootMultipleParallel]
624 # cmd: mypy --package-root=a/ --package-root=./ a/b/c.py d.py main.py --num-workers=2
625 [file a/b/c.py]
626 [file d.py]
627 [file main.py]
628 import b.c
629 import d
630
631 [case testCacheMap]
632 -- This just checks that a valid --cache-map triple is accepted.
633 -- (Errors are too verbose to check.)
634 # cmd: mypy a.py --no-sqlite-cache --cache-map a.py a.meta.json a.data.json
635 [file a.py]
636 [out]
637
638 [case testIniFiles]
639 # cmd: mypy
640 [file mypy.ini]
641 \[mypy]
642 files = a.py, b.py
643 [file a.py]
644 fail
645 [file b.py]
646 fail
647 [out]
648 b.py:1: error: Name "fail" is not defined
649 a.py:1: error: Name "fail" is not defined
650
651 [case testIniFilesGlobbing]
652 # cmd: mypy
653 [file mypy.ini]
654 \[mypy]
655 files = **/*.py
656 [file a/b.py]
657 fail
658 [file c.py]
659 fail
660 [out]
661 a/b.py:1: error: Name "fail" is not defined
662 c.py:1: error: Name "fail" is not defined
663
664 [case testIniFilesCmdlineOverridesConfig]
665 # cmd: mypy override.py
666 [file mypy.ini]
667 \[mypy]
668 files = config.py
669 [out]
670 mypy: error: Cannot read file 'override.py': No such file or directory
671 == Return code: 2
672
673 [case testErrorSummaryOnSuccess]
674 # cmd: mypy --error-summary good.py
675 [file good.py]
676 x = 2 + 2
677 [out]
678 Success: no issues found in 1 source file
679 == Return code: 0
680
681 [case testErrorSummaryOnFail]
682 # cmd: mypy --error-summary bad.py
683 [file bad.py]
684 42 + 'no'
685 [out]
686 bad.py:1: error: Unsupported operand types for + ("int" and "str")
687 Found 1 error in 1 file (checked 1 source file)
688
689 [case testErrorSummaryOnFailNotes]
690 # cmd: mypy --error-summary bad.py
691 [file bad.py]
692 from typing import List
693 x = [] # type: List[float]
694 y = [] # type: List[int]
695 x = y
696 [out]
697 bad.py:4: error: Incompatible types in assignment (expression has type "list[int]", variable has type "list[float]")
698 bad.py:4: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
699 bad.py:4: note: Consider using "Sequence" instead, which is covariant
700 Found 1 error in 1 file (checked 1 source file)
701
702 [case testErrorSummaryOnFailTwoErrors]
703 # cmd: mypy --error-summary bad.py foo.py
704 [file bad.py]
705 42 + 'no'
706 42 + 'no'
707 [file foo.py]
708 [out]
709 bad.py:1: error: Unsupported operand types for + ("int" and "str")
710 bad.py:2: error: Unsupported operand types for + ("int" and "str")
711 Found 2 errors in 1 file (checked 2 source files)
712
713 [case testErrorSummaryOnFailTwoFiles]
714 # cmd: mypy --error-summary bad.py bad2.py
715 [file bad.py]
716 42 + 'no'
717 [file bad2.py]
718 42 + 'no'
719 [out]
720 bad2.py:1: error: Unsupported operand types for + ("int" and "str")
721 bad.py:1: error: Unsupported operand types for + ("int" and "str")
722 Found 2 errors in 2 files (checked 2 source files)
723
724 [case testErrorSummaryOnBadUsage]
725 # cmd: mypy --error-summary missing.py
726 [out]
727 mypy: error: Cannot read file 'missing.py': No such file or directory
728 Found 1 error in 1 file (errors prevented further checking)
729 == Return code: 2
730
731 [case testShowSourceCodeSnippetsWrappedFormatting]
732 # cmd: mypy --pretty some_file.py
733 [file some_file.py]
734 from typing import Union
735
736 42 + 'no way'
737
738 class OneCustomClassName:
739 def some_interesting_method(self, arg: AnotherCustomClassDefinedBelow) -> AnotherCustomClassDefinedBelow:
740 ...
741
742 class AnotherCustomClassDefinedBelow:
743 def another_even_more_interesting_method(self, arg: Union[int, str, float]) -> None:
744 self.very_important_attribute_with_long_name: OneCustomClassName = OneCustomClassName().some_interesting_method(arg)
745 [out]
746 some_file.py:3: error: Unsupported operand types for + ("int" and "str")
747 42 + 'no way'
748 ^~~~~~~~
749 some_file.py:11: error: Incompatible types in assignment (expression has type
750 "AnotherCustomClassDefinedBelow", variable has type "OneCustomClassName")
751 ...t_attribute_with_long_name: OneCustomClassName = OneCustomClassName()....
752 ^~~~~~~~~~~~~~~~~~~~~...
753 some_file.py:11: error: Argument 1 to "some_interesting_method" of
754 "OneCustomClassName" has incompatible type "int | str | float"; expected
755 "AnotherCustomClassDefinedBelow"
756 ...OneCustomClassName = OneCustomClassName().some_interesting_method(arg)
757 ^~~
758 [case testShowSourceCodeSnippetsBlockingError]
759 # cmd: mypy --pretty --show-error-codes some_file.py
760 [file some_file.py]
761 it_looks_like_we_started_typing_something_but_then. = did_not_notice(an_extra_dot)
762 [out]
763 some_file.py:1: error: Invalid syntax [syntax]
764 ...ooks_like_we_started_typing_something_but_then. = did_not_notice(an_ex...
765 ^
766 == Return code: 2
767
768 [case testTabRenderingUponError]
769 # cmd: mypy --pretty tabs.py
770 [file tabs.py]
771 def test_tabs() -> str:
772 return None
773 def test_between(x: str) -> None: ...
774 test_between(1 + 1)
775 [out]
776 tabs.py:2: error: Incompatible return value type (got "None", expected "str")
777 return None
778 ^~~~
779 tabs.py:4: error: Argument 1 to "test_between" has incompatible type "int";
780 expected "str"
781 test_between(1 + 1)
782 ^~~~~~~~~~~~
783
784 [case testErrorMarkerSkipsLeftSpace]
785 # cmd: mypy --pretty --show-error-codes imp.py
786 [file imp.py]
787 try:
788 import foobar
789 except ValueError:
790 pass
791 [out]
792 imp.py:2: error: Cannot find implementation or library stub for module named
793 "foobar" [import-not-found]
794 import foobar
795 ^
796 imp.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
797
798 [case testErrorMessageWhenOpenPydFile]
799 # cmd: mypy a.pyd
800 [file a.pyd]
801 # coding: uft-8
802 [out]
803 a.pyd: error: Stubgen does not support .pyd files
804 == Return code: 2
805
806 [case testDuplicateModules]
807 # cmd: mypy src
808 [file mypy.ini]
809 \[mypy]
810 mypy_path = src
811 [file src/__init__.py]
812 [file src/a.py]
813 import foo.bar
814 [file src/foo/__init__.py]
815 [file src/foo/bar.py]
816 1+'x'
817 [out]
818 src/foo/bar.py: error: Source file found twice under different module names: "src.foo.bar" and "foo.bar"
819 src/foo/bar.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
820 src/foo/bar.py: note: Common resolutions include:
821 src/foo/bar.py: note: a) adding `__init__.py` somewhere,
822 src/foo/bar.py: note: b) using `--explicit-package-bases` or adjusting `MYPYPATH`
823 == Return code: 2
824
825 [case testEnableInvalidErrorCode]
826 # cmd: mypy --enable-error-code YOLO test.py
827 [file test.py]
828 x = 1
829 [out]
830 mypy: error: Invalid error code(s): YOLO
831 == Return code: 2
832
833 [case testDisableInvalidErrorCode]
834 # cmd: mypy --disable-error-code YOLO test.py
835 [file test.py]
836 x = 1
837 [out]
838 mypy: error: Invalid error code(s): YOLO
839 == Return code: 2
840
841 [case testEnableAndDisableInvalidErrorCode]
842 # cmd: mypy --disable-error-code YOLO --enable-error-code YOLO2 test.py
843 [file test.py]
844 x = 1
845 [out]
846 mypy: error: Invalid error code(s): YOLO, YOLO2
847 == Return code: 2
848
849 [case testEnableValidAndInvalidErrorCode]
850 # cmd: mypy --enable-error-code attr-defined --enable-error-code YOLO test.py
851 [file test.py]
852 x = 1
853 [out]
854 mypy: error: Invalid error code(s): YOLO
855 == Return code: 2
856
857 [case testDisableValidAndInvalidErrorCode]
858 # cmd: mypy --disable-error-code attr-defined --disable-error-code YOLO test.py
859 [file test.py]
860 x = 1
861 [out]
862 mypy: error: Invalid error code(s): YOLO
863 == Return code: 2
864
865 [case testStubsDirectory]
866 # cmd: mypy --error-summary pkg-stubs
867 [file pkg-stubs/__init__.pyi]
868 [file pkg-stubs/thing.pyi]
869 class Thing: ...
870 [out]
871 Success: no issues found in 2 source files
872 == Return code: 0
873
874 [case testStubsDirectoryFile]
875 # cmd: mypy --error-summary pkg-stubs/thing.pyi
876 [file pkg-stubs/__init__.pyi]
877 [file pkg-stubs/thing.pyi]
878 class Thing: ...
879 [out]
880 Success: no issues found in 1 source file
881 == Return code: 0
882
883 [case testStubsSubDirectory]
884 # cmd: mypy --error-summary src/pkg-stubs
885 [file src/pkg-stubs/__init__.pyi]
886 [file src/pkg-stubs/thing.pyi]
887 class Thing: ...
888 [out]
889 Success: no issues found in 2 source files
890 == Return code: 0
891
892 [case testStubsSubDirectoryFile]
893 # cmd: mypy --error-summary src/pkg-stubs/thing.pyi
894 [file src/pkg-stubs/__init__.pyi]
895 [file src/pkg-stubs/thing.pyi]
896 class Thing: ...
897 [out]
898 Success: no issues found in 1 source file
899 == Return code: 0
900
901 [case testBlocker]
902 # cmd: mypy pkg --error-summary --disable-error-code syntax
903 [file pkg/x.py]
904 public static void main(String[] args)
905 [file pkg/y.py]
906 x: str = 0
907 [out]
908 pkg/x.py:1: error: Invalid syntax. Perhaps you forgot a comma?
909 Found 1 error in 1 file (errors prevented further checking)
910 == Return code: 2
911 [out version>=3.10.3]
912 pkg/x.py:1: error: Invalid syntax
913 Found 1 error in 1 file (errors prevented further checking)
914 == Return code: 2
915
916 [case testCmdlinePackageAndFile]
917 # cmd: mypy -p pkg file
918 [out]
919 usage: mypy [-h] [-v] [-V] [more options; see below]
920 [-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
921 mypy: error: May only specify one of: module/package, files, or command.
922 == Return code: 2
923
924 [case testCmdlinePackageAndIniFiles]
925 # cmd: mypy -p pkg
926 [file mypy.ini]
927 \[mypy]
928 files=file
929 [file pkg.py]
930 x = 0 # type: str
931 [file file.py]
932 y = 0 # type: str
933 [out]
934 pkg.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
935
936
937 [case testCmdlineModuleAndIniFiles]
938 # cmd: mypy -m pkg
939 [file mypy.ini]
940 \[mypy]
941 files=file
942 [file pkg.py]
943 x = 0 # type: str
944 [file file.py]
945 y = 0 # type: str
946 [out]
947 pkg.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
948
949 [case testCmdlineNonInteractiveWithoutInstallTypes]
950 # cmd: mypy --non-interactive -m pkg
951 [out]
952 error: --non-interactive is only supported with --install-types
953 == Return code: 2
954
955 [case testCmdlineNonInteractiveInstallTypesNothingToDo]
956 # cmd: mypy --install-types --non-interactive -m pkg
957 [file pkg.py]
958 1()
959 [out]
960 pkg.py:1: error: "int" not callable
961
962 [case testCmdlineNonInteractiveInstallTypesNothingToDoNoError]
963 # cmd: mypy --install-types --non-interactive -m pkg
964 [file pkg.py]
965 1 + 2
966 [out]
967
968 [case testCmdlineNonInteractiveInstallTypesNoSitePackages]
969 # cmd: mypy --install-types --non-interactive --no-site-packages -m pkg
970 [out]
971 error: --install-types not supported without python executable or site packages
972 == Return code: 2
973
974 [case testCmdlineInteractiveInstallTypesNothingToDo]
975 # cmd: mypy --install-types -m pkg
976 [file pkg.py]
977 1()
978 [out]
979 pkg.py:1: error: "int" not callable
980
981 [case testCmdlineExclude]
982 # cmd: mypy --exclude abc .
983 [file abc/apkg.py]
984 1()
985 [file b/bpkg.py]
986 1()
987 [file c/cpkg.py]
988 1()
989 [out]
990 c/cpkg.py:1: error: "int" not callable
991 b/bpkg.py:1: error: "int" not callable
992
993 [case testCmdlineMultipleExclude]
994 # cmd: mypy --exclude abc --exclude b/ .
995 [file abc/apkg.py]
996 1()
997 [file b/bpkg.py]
998 1()
999 [file c/cpkg.py]
1000 1()
1001 [out]
1002 c/cpkg.py:1: error: "int" not callable
1003
1004 [case testCmdlineExcludeGitignore]
1005 # cmd: mypy --exclude-gitignore .
1006 [file .gitignore]
1007 abc
1008 [file abc/apkg.py]
1009 1()
1010 [file b/.gitignore]
1011 bpkg.*
1012 [file b/bpkg.py]
1013 1()
1014 [file c/cpkg.py]
1015 1()
1016 [out]
1017 c/cpkg.py:1: error: "int" not callable
1018
1019 [case testCmdlineExcludeGitignoreOnlyWithinTree]
1020 # cmd: mypy --exclude-gitignore b
1021 [file .gitignore]
1022 b
1023 [file b/.git/config]
1024 [file b/bpkg.py]
1025 1()
1026 [out]
1027 b/bpkg.py:1: error: "int" not callable
1028
1029 [case testCmdlineExcludeGitignoreWithGitInfoExclude]
1030 # cmd: mypy --exclude-gitignore .
1031 [file .git/info/exclude]
1032 abc
1033 [file abc/apkg.py]
1034 1()
1035 [file c/cpkg.py]
1036 1()
1037 [out]
1038 c/cpkg.py:1: error: "int" not callable
1039
1040 [case testCmdlineCfgExclude]
1041 # cmd: mypy .
1042 [file mypy.ini]
1043 \[mypy]
1044 exclude = abc
1045 [file abc/apkg.py]
1046 1()
1047 [file b/bpkg.py]
1048 1()
1049 [file c/cpkg.py]
1050 1()
1051 [out]
1052 c/cpkg.py:1: error: "int" not callable
1053 b/bpkg.py:1: error: "int" not callable
1054
1055 [case testCmdlineCfgMultipleExclude]
1056 # cmd: mypy .
1057 [file mypy.ini]
1058 \[mypy]
1059 exclude = (?x)(
1060 ^abc/
1061 |^b/
1062 )
1063 [file abc/apkg.py]
1064 1()
1065 [file b/bpkg.py]
1066 1()
1067 [file c/cpkg.py]
1068 1()
1069 [out]
1070 c/cpkg.py:1: error: "int" not callable
1071
1072 [case testCmdlineTimingStats]
1073 # cmd: mypy --timing-stats timing.txt .
1074 [file b/__init__.py]
1075 [file b/c.py]
1076 class C: pass
1077 [outfile-re timing.txt]
1078 .*
1079 b \d+
1080 b\.c \d+
1081 .*
1082
1083 [case testShadowTypingModuleEarlyLoad]
1084 # cmd: mypy dir
1085 [file dir/__init__.py]
1086 from typing import Union
1087
1088 def foo(a: Union[int, str]) -> str:
1089 return str
1090 [file typing.py]
1091 # Since this file will be picked by mypy itself, we need it to be a fully-working typing
1092 # A bare minimum would be NamedTuple and TypedDict, which are used in runtime,
1093 # everything else technically can be just mocked.
1094 import sys
1095 import os
1096 del sys.modules["typing"]
1097 path = sys.path
1098 try:
1099 sys.path.remove(os.getcwd())
1100 except ValueError:
1101 sys.path.remove("") # python 3.6
1102 from typing import *
1103 sys.path = path
1104 [out]
1105 typing.py: error: This file shadows library module "typing"
1106 typing.py: note: A user-defined top-level module with name "typing" is not supported
1107 == Return code: 2
1108
1109 [case testCustomTypeshedDirWithRelativePathDoesNotCrash]
1110 # cmd: mypy --custom-typeshed-dir dir dir/typing.pyi
1111 [file dir/stdlib/abc.pyi]
1112 [file dir/stdlib/builtins.pyi]
1113 [file dir/stdlib/sys.pyi]
1114 [file dir/stdlib/types.pyi]
1115 [file dir/stdlib/typing.pyi]
1116 [file dir/stdlib/typing_extensions.pyi]
1117 [file dir/stdlib/_typeshed.pyi]
1118 [file dir/stdlib/_collections_abc.pyi]
1119 [file dir/stdlib/collections/abc.pyi]
1120 [file dir/stdlib/collections/__init__.pyi]
1121 [file dir/stdlib/VERSIONS]
1122 [out]
1123 mypy: error: Failed to find builtin module "mypy_extensions", perhaps typeshed is broken?
1124 == Return code: 2
1125
1126 [case testCustomTypeshedDirFilePassedExplicitly]
1127 # cmd: mypy --custom-typeshed-dir dir m.py dir/stdlib/foo.pyi
1128 [file m.py]
1129 1()
1130 [file dir/stdlib/abc.pyi]
1131 1() # Errors are not reported from typeshed by default
1132 [file dir/stdlib/builtins.pyi]
1133 class object: pass
1134 class str(object): pass
1135 class int(object): pass
1136 class list: pass
1137 class dict: pass
1138 [file dir/stdlib/sys.pyi]
1139 [file dir/stdlib/types.pyi]
1140 [file dir/stdlib/typing.pyi]
1141 [file dir/stdlib/mypy_extensions.pyi]
1142 [file dir/stdlib/typing_extensions.pyi]
1143 [file dir/stdlib/_typeshed.pyi]
1144 [file dir/stdlib/_collections_abc.pyi]
1145 [file dir/stdlib/collections/abc.pyi]
1146 [file dir/stdlib/collections/__init__.pyi]
1147 [file dir/stdlib/foo.pyi]
1148 1() # Errors are reported if the file was explicitly passed on the command line
1149 [file dir/stdlib/VERSIONS]
1150 [out]
1151 dir/stdlib/foo.pyi:1: error: "int" not callable
1152 m.py:1: error: "int" not callable
1153
1154 [case testFileInPythonPathPassedExplicitly1]
1155 # cmd: mypy $CWD/pypath/foo.py
1156 [file pypath/foo.py]
1157 1()
1158 [out]
1159 pypath/foo.py:1: error: "int" not callable
1160
1161 [case testFileInPythonPathPassedExplicitly2]
1162 # cmd: mypy pypath/foo.py
1163 [file pypath/foo.py]
1164 1()
1165 [out]
1166 pypath/foo.py:1: error: "int" not callable
1167
1168 [case testFileInPythonPathPassedExplicitly3]
1169 # cmd: mypy -p foo
1170 # cwd: pypath
1171 [file pypath/foo/__init__.py]
1172 1()
1173 [file pypath/foo/m.py]
1174 1()
1175 [out]
1176 foo/m.py:1: error: "int" not callable
1177 foo/__init__.py:1: error: "int" not callable
1178
1179 [case testFileInPythonPathPassedExplicitly4]
1180 # cmd: mypy -m foo
1181 # cwd: pypath
1182 [file pypath/foo.py]
1183 1()
1184 [out]
1185 foo.py:1: error: "int" not callable
1186
1187 [case testFileInPythonPathPassedExplicitly5]
1188 # cmd: mypy -m foo.m
1189 # cwd: pypath
1190 [file pypath/foo/__init__.py]
1191 1() # TODO: Maybe this should generate errors as well? But how would we decide?
1192 [file pypath/foo/m.py]
1193 1()
1194 [out]
1195 foo/m.py:1: error: "int" not callable
1196
1197 [case testCmdlineCfgFilesTrailingComma]
1198 # cmd: mypy
1199 [file mypy.ini]
1200 \[mypy]
1201 files =
1202 a.py,
1203 b.py,
1204 [file a.py]
1205 x: str = 'x' # ok
1206 [file b.py]
1207 y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1208 [file c.py]
1209 # This should not trigger any errors, because it is not included:
1210 z: int = 'z'
1211 [out]
1212
1213 [case testCmdlineCfgEnableErrorCodeTrailingComma]
1214 # cmd: mypy .
1215 [file mypy.ini]
1216 \[mypy]
1217 enable_error_code =
1218 truthy-bool,
1219 redundant-expr,
1220 [out]
1221
1222 [case testCmdlineCfgDisableErrorCodeTrailingComma]
1223 # cmd: mypy .
1224 [file mypy.ini]
1225 \[mypy]
1226 disable_error_code =
1227 misc,
1228 override,
1229 [out]
1230
1231 [case testCmdlineCfgAlwaysTrueTrailingComma]
1232 # cmd: mypy .
1233 [file mypy.ini]
1234 \[mypy]
1235 always_true =
1236 MY_VAR,
1237 [out]
1238
1239 [case testCmdlineCfgModulesTrailingComma]
1240 # cmd: mypy
1241 [file mypy.ini]
1242 \[mypy]
1243 modules =
1244 a,
1245 b,
1246 [file a.py]
1247 x: str = 'x' # ok
1248 [file b.py]
1249 y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1250 [file c.py]
1251 # This should not trigger any errors, because it is not included:
1252 z: int = 'z'
1253 [out]
1254
1255 [case testCmdlineCfgPackagesTrailingComma]
1256 # cmd: mypy
1257 [file mypy.ini]
1258 \[mypy]
1259 packages =
1260 a,
1261 b,
1262 [file a/__init__.py]
1263 x: str = 'x' # ok
1264 [file b/__init__.py]
1265 y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1266 [file c/__init__.py]
1267 # This should not trigger any errors, because it is not included:
1268 z: int = 'z'
1269 [out]
1270
1271 [case testTypeVarTupleUnpackEnabled]
1272 # cmd: mypy --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack a.py
1273 [file a.py]
1274 from typing_extensions import TypeVarTuple
1275 Ts = TypeVarTuple("Ts")
1276 [out]
1277 Warning: TypeVarTuple is already enabled by default
1278 Warning: Unpack is already enabled by default
1279 == Return code: 0
1280
1281 [case testImportlibImportCannotBeResolved]
1282 # cmd: mypy a.py
1283 # This test is here because it needs use_builtins_fixtures off.
1284
1285 [file a.py]
1286 from typing import NamedTuple
1287
1288 class CodecKey(NamedTuple):
1289 def foo(self) -> "CodecKey":
1290 ...
1291
1292 [file mypy.ini]
1293 \[mypy]
1294
1295 \[mypy-importlib.*]
1296 follow_imports = skip
1297 follow_imports_for_stubs = True
1298
1299 [case testParallelRunWithSyntaxError]
1300 # cmd: mypy a.py --num-workers=2 --pretty
1301 [file a.py]
1302 1 2
1303 [out]
1304 a.py:1: error: Simple statements must be separated by newlines or semicolons
1305 1 2
1306 ^
1307 == Return code: 2
1308
1309 [case testCannotDisableCacheInParallelMode]
1310 # cmd: mypy --cache-dir=/dev/null --num-workers=2 a.py
1311 [file a.py]
1312 pass
1313 [out]
1314 error: Cache must be enabled in parallel mode
1315 == Return code: 2
1316
1317 [case testCheckingStubPackagesWorksInParallelMode]
1318 # cmd: mypy foo-stubs --num-workers=4
1319 [file foo-stubs/__init__.pyi]
1320 [file foo-stubs/api/__init__.pyi]
1321 from foo.api import bar as bar
1322 [file foo-stubs/api/bar.pyi]
1323 [out]