-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
cmdline.test
1337 lines (1223 loc) · 33.7 KB
/
cmdline.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Tests for command line parsing
-- ------------------------------
--
-- The initial line specifies the command line, in the format
--
-- # cmd: mypy <options>
--
-- Note that # flags: --some-flag IS NOT SUPPORTED.
-- Use # cmd: mypy --some-flag ...
--
-- '== Return code: <value>' is added to the output when the process return code
-- is "nonobvious" -- that is, when it is something other than 0 if there are no
-- messages and 1 if there are.
-- Directories/packages on the command line
-- ----------------------------------------
[case testCmdlinePackage]
# cmd: mypy pkg
[file pkg/__init__.py]
[file pkg/a.py]
undef
[file pkg/subpkg/__init__.py]
[file pkg/subpkg/a.py]
undef
import pkg.subpkg.a
[out]
pkg/a.py:1: error: Name "undef" is not defined
pkg/subpkg/a.py:1: error: Name "undef" is not defined
[case testCmdlinePackageSlash]
# cmd: mypy pkg/
[file pkg/__init__.py]
[file pkg/a.py]
undef
[file pkg/subpkg/__init__.py]
[file pkg/subpkg/a.py]
undef
import pkg.subpkg.a
[out]
pkg/a.py:1: error: Name "undef" is not defined
pkg/subpkg/a.py:1: error: Name "undef" is not defined
[case testCmdlineNonPackage]
# cmd: mypy dir
[file dir/a.py]
undef
[file dir/subdir/b.py]
undef
[out]
dir/a.py:1: error: Name "undef" is not defined
dir/subdir/b.py:1: error: Name "undef" is not defined
[case testCmdlineNonPackageDuplicate]
# cmd: mypy dir
[file dir/a.py]
undef
[file dir/subdir/a.py]
undef
[out]
dir/a.py: error: Duplicate module named "a" (also at "dir/subdir/a.py")
dir/a.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
dir/a.py: note: Common resolutions include: a) using `--exclude` to avoid checking one of them, b) adding `__init__.py` somewhere, c) using `--explicit-package-bases` or adjusting MYPYPATH
== Return code: 2
[case testCmdlineNonPackageSlash]
# cmd: mypy dir/
[file dir/a.py]
undef
import b
[file dir/subdir/b.py]
undef
import a
[out]
dir/a.py:1: error: Name "undef" is not defined
dir/subdir/b.py:1: error: Name "undef" is not defined
[case testCmdlinePackageContainingSubdir]
# cmd: mypy pkg
[file pkg/__init__.py]
[file pkg/a.py]
undef
import pkg.a
[file pkg/subdir/a.py]
undef
import pkg.a
[out]
pkg/a.py:1: error: Name "undef" is not defined
pkg/subdir/a.py:1: error: Name "undef" is not defined
[case testCmdlineNonPackageContainingPackage]
# cmd: mypy dir
[file dir/a.py]
undef
import subpkg.a
[file dir/subpkg/__init__.py]
[file dir/subpkg/a.py]
undef
[out]
dir/subpkg/a.py:1: error: Name "undef" is not defined
dir/a.py:1: error: Name "undef" is not defined
[case testCmdlineInvalidPackageName]
# cmd: mypy dir/sub.pkg/a.py
[file dir/sub.pkg/__init__.py]
[file dir/sub.pkg/a.py]
undef
[out]
sub.pkg is not a valid Python package name
== Return code: 2
[case testBadFileEncoding]
# cmd: mypy a.py
[file a.py]
# coding: uft-8
[out]
mypy: can't decode file 'a.py': unknown encoding: uft-8
== Return code: 2
-- '
[case testCannotIgnoreDuplicateModule]
# cmd: mypy one/mod/__init__.py two/mod/__init__.py
[file one/mod/__init__.py]
# type: ignore
[file two/mod/__init__.py]
# type: ignore
[out]
two/mod/__init__.py: error: Duplicate module named "mod" (also at "one/mod/__init__.py")
two/mod/__init__.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
two/mod/__init__.py: note: Common resolutions include: a) using `--exclude` to avoid checking one of them, b) adding `__init__.py` somewhere, c) using `--explicit-package-bases` or adjusting MYPYPATH
== Return code: 2
-- Note that we use `----`, because this is how `--` is escaped while `--` is a comment starter.
[case testFlagsFile]
# cmd: mypy @flagsfile
[file flagsfile]
----always-true=FLAG
main.py
[file main.py]
x: int
FLAG = False
if not FLAG:
x = "unreachable"
[case testConfigFile]
# cmd: mypy main.py
[file mypy.ini]
\[mypy]
always_true = FLAG
[file main.py]
x: int
FLAG = False
if not FLAG:
x = "unreachable"
[case testAltConfigFile]
# cmd: mypy --config-file config.ini main.py
[file config.ini]
\[mypy]
always_true = FLAG
[file main.py]
x: int
FLAG = False
if not FLAG:
x = "unreachable"
[case testPerFileConfigSectionMultipleMatchesDisallowed]
# cmd: mypy xx.py xy.py yx.py yy.py
[file mypy.ini]
\[mypy]
\[mypy-*x*]
disallow_untyped_defs = True
\[mypy-*y*]
disallow_untyped_calls = True
[file xx.py]
def f(a): pass
def g(a: int) -> int: return f(a)
[file xy.py]
def f(a): pass
def g(a: int) -> int: return f(a)
[file yx.py]
def f(a): pass
def g(a: int) -> int: return f(a)
[file yy.py]
def f(a): pass
def g(a: int) -> int: return f(a)
[out]
mypy.ini: [mypy-*x*]: Patterns must be fully-qualified module names, optionally with '*' in some components (e.g spam.*.eggs.*)
mypy.ini: [mypy-*y*]: Patterns must be fully-qualified module names, optionally with '*' in some components (e.g spam.*.eggs.*)
== Return code: 0
[case testMultipleGlobConfigSection]
# cmd: mypy x.py y.py z.py
[file mypy.ini]
\[mypy]
\[mypy-x.*,z.*]
disallow_untyped_defs = True
[file x.py]
def f(a): pass
[file y.py]
def f(a): pass
[file z.py]
def f(a): pass
[out]
z.py:1: error: Function is missing a type annotation
x.py:1: error: Function is missing a type annotation
[case testConfigErrorNoSection]
# cmd: mypy -c pass
[file mypy.ini]
[out]
mypy.ini: No [mypy] section in config file
== Return code: 0
[case testConfigErrorUnknownFlag]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
bad = 0
[out]
mypy.ini: [mypy]: Unrecognized option: bad = 0
== Return code: 0
[case testConfigErrorBadFlag]
# cmd: mypy a.py
[file mypy.ini]
\[mypy]
disallow-untyped-defs = True
[file a.py]
def f():
pass
[out]
mypy.ini: [mypy]: Unrecognized option: disallow-untyped-defs = True
== Return code: 0
[case testConfigErrorBadBoolean]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
ignore_missing_imports = nah
[out]
mypy.ini: [mypy]: ignore_missing_imports: Not a boolean: nah
== Return code: 0
[case testConfigErrorNotPerFile]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
\[mypy-*]
python_version = 3.11
[out]
mypy.ini: [mypy-*]: Per-module sections should only specify per-module flags (python_version)
== Return code: 0
[case testConfigMypyPath]
# cmd: mypy file.py
[file mypy.ini]
\[mypy]
mypy_path =
foo_dir:bar_dir
, baz_dir
[file foo_dir/foo.pyi]
def foo(x: int) -> str: ...
[file bar_dir/bar.pyi]
def bar(x: str) -> list: ...
[file baz_dir/baz.pyi]
def baz(x: list) -> dict: ...
[file file.py]
import no_stubs
from foo import foo
from bar import bar
from baz import baz
baz(bar(foo(42)))
baz(bar(foo('oof')))
[out]
file.py:1: error: Cannot find implementation or library stub for module named "no_stubs"
file.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
file.py:6: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
[case testConfigFollowImportsSysPath]
# cmd: mypy main.py
[file main.py]
from a import x
x + 0
x + '' # E
import a
a.x + 0
a.x + '' # E
a.y # E
a + 0 # E
[file mypy.ini]
\[mypy]
follow_imports = normal
no_silence_site_packages = True
[file pypath/a/__init__.py]
x = 0
x += '' # Error reported here
[file pypath/a/py.typed]
[out]
pypath/a/__init__.py:2: error: Unsupported operand types for + ("int" and "str")
main.py:3: error: Unsupported operand types for + ("int" and "str")
main.py:6: error: Unsupported operand types for + ("int" and "str")
main.py:7: error: Module has no attribute "y"
main.py:8: error: Unsupported operand types for + (Module and "int")
[case testConfigFollowImportsInvalid]
# cmd: mypy main.py
[file mypy.ini]
\[mypy]
follow_imports =True
[file main.py]
[out]
mypy.ini: [mypy]: follow_imports: invalid choice 'True' (choose from 'normal', 'silent', 'skip', 'error')
== Return code: 0
[case testFailedImportOnWrongCWD]
# cmd: mypy main.py
# cwd: main/subdir1/subdir2
[file main/subdir1/subdir2/main.py]
import parent
import grandparent
import missing
[file main/subdir1/subdir2/__init__.py]
[file main/subdir1/parent.py]
[file main/subdir1/__init__.py]
[file main/grandparent.py]
[file main/__init__.py]
[out]
main.py:1: error: Cannot find implementation or library stub for module named "parent"
main.py:1: note: You may be running mypy in a subpackage, mypy should be run on the package root
main.py:2: error: Cannot find implementation or library stub for module named "grandparent"
main.py:3: error: Cannot find implementation or library stub for module named "missing"
main.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testImportInParentButNoInit]
# cmd: mypy main.py
# cwd: main/not_a_package
[file main/not_a_package/main.py]
import needs_init
[file main/needs_init.py]
[file main/__init__.py]
[out]
main.py:1: error: Cannot find implementation or library stub for module named "needs_init"
main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testConfigNoErrorForUnknownXFlagInSubsection]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
\[mypy-foo]
x_bad = 0
[out]
[case testDotInFilenameOKScript]
# cmd: mypy a.b.py c.d.pyi
[file a.b.py]
undef
[file c.d.pyi]
whatever
[out]
c.d.pyi:1: error: Name "whatever" is not defined
a.b.py:1: error: Name "undef" is not defined
[case testDotInFilenameOKFolder]
# cmd: mypy my.folder
[file my.folder/tst.py]
undef
[out]
my.folder/tst.py:1: error: Name "undef" is not defined
[case testDotInFilenameNoImport]
# cmd: mypy main.py
[file main.py]
import a.b
[file a.b.py]
whatever
[out]
main.py:1: error: Cannot find implementation or library stub for module named "a.b"
main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main.py:1: error: Cannot find implementation or library stub for module named "a"
[case testPythonVersionWrongFormatPyProjectTOML]
# cmd: mypy -c pass
[file pyproject.toml]
\[tool.mypy]
python_version = 3.10
[out]
pyproject.toml: [mypy]: python_version: Python 3.1 is not supported (must be 3.8 or higher). You may need to put quotes around your Python version
== Return code: 0
[case testPythonVersionTooOld10]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
python_version = 1.0
[out]
mypy.ini: [mypy]: python_version: Python major version '1' out of range (must be 3)
== Return code: 0
[case testPythonVersionTooOld37]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
python_version = 3.7
[out]
mypy.ini: [mypy]: python_version: Python 3.7 is not supported (must be 3.8 or higher)
== Return code: 0
[case testPythonVersionTooNew40]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
python_version = 4.0
[out]
mypy.ini: [mypy]: python_version: Python major version '4' out of range (must be 3)
== Return code: 0
[case testPythonVersionTooDead27]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
python_version = 2.7
[out]
usage: mypy [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Mypy no longer supports checking Python 2 code. Consider pinning to mypy<0.980 if you need to check Python 2 code.
== Return code: 2
[case testPythonVersionAccepted38]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
python_version = 3.8
[out]
[case testPythonVersionAccepted311]
# cmd: mypy -c pass
[file mypy.ini]
\[mypy]
python_version = 3.11
[out]
-- This should be a dumping ground for tests of plugins that are sensitive to
-- typeshed changes.
[case testTypeshedSensitivePlugins]
# cmd: mypy int_pow.py
[file int_pow.py]
a = 1
b = a + 2
reveal_type(a**0)
reveal_type(a**1)
reveal_type(a**2)
reveal_type(a**-0)
reveal_type(a**-1)
reveal_type(a**(-2))
reveal_type(a**b)
reveal_type(a.__pow__(2))
reveal_type(a.__pow__(a))
[out]
int_pow.py:3: note: Revealed type is "Literal[1]"
int_pow.py:4: note: Revealed type is "builtins.int"
int_pow.py:5: note: Revealed type is "builtins.int"
int_pow.py:6: note: Revealed type is "Literal[1]"
int_pow.py:7: note: Revealed type is "builtins.float"
int_pow.py:8: note: Revealed type is "builtins.float"
int_pow.py:9: note: Revealed type is "Any"
int_pow.py:10: note: Revealed type is "builtins.int"
int_pow.py:11: note: Revealed type is "Any"
== Return code: 0
[case testDisallowAnyGenericsBuiltinCollectionsPre39]
# cmd: mypy m.py
[file mypy.ini]
\[mypy]
python_version = 3.8
\[mypy-m]
disallow_any_generics = True
[file m.py]
def j(s: frozenset) -> None: pass
[out]
m.py:1: error: Implicit generic "Any". Use "typing.FrozenSet" and specify generic parameters
[case testDisallowAnyGenericsTypingCollections]
# cmd: mypy m.py
[file mypy.ini]
\[mypy]
\[mypy-m]
disallow_any_generics = True
[file m.py]
from typing import FrozenSet
def j(s: FrozenSet) -> None: pass
[out]
m.py:2: error: Missing type parameters for generic type "FrozenSet"
[case testSectionInheritance]
# cmd: mypy a
[file a/__init__.py]
0()
[file a/foo.py]
0()
[file a/b/__init__.py]
[file a/b/c/__init__.py]
0()
[file a/b/c/d/__init__.py]
[file a/b/c/d/e/__init__.py]
from typing import List
def g(x: List) -> None: pass
g(None)
[file mypy.ini]
\[mypy]
allow_any_generics = True
\[mypy-a.*]
ignore_errors = True
\[mypy-a.b.*]
disallow_any_generics = True
ignore_errors = True
\[mypy-a.b.c.*]
ignore_errors = True
\[mypy-a.b.c.d.*]
ignore_errors = True
\[mypy-a.b.c.d.e.*]
ignore_errors = True
strict_optional = True
\[mypy-a.b.c.d.e]
ignore_errors = False
[out]
a/b/c/d/e/__init__.py:2: error: Missing type parameters for generic type "List"
a/b/c/d/e/__init__.py:3: error: Argument 1 to "g" has incompatible type "None"; expected "List[Any]"
[case testMissingFile]
# cmd: mypy nope.py
[out]
mypy: can't read file 'nope.py': No such file or directory
== Return code: 2
--'
[case testModulesAndPackages]
# cmd: mypy --package p.a --package p.b --module c
[file p/__init__.py]
[file p/a.py]
def foo(x):
# type: (int) -> str
return "x"
foo("wrong")
[file p/b/__init__.py]
from ..a import foo
def bar(a):
# type: (int) -> str
return foo(a)
bar("wrong")
[file c.py]
import p.b
p.b.bar("wrong")
[out]
p/a.py:4: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
p/b/__init__.py:5: error: Argument 1 to "bar" has incompatible type "str"; expected "int"
c.py:2: error: Argument 1 to "bar" has incompatible type "str"; expected "int"
[case testSrcPEP420Packages]
# cmd: mypy -p anamespace --namespace-packages
[file mypy.ini]
\[mypy]
mypy_path = src
[file src/setup.cfg]
[file src/anamespace/foo/__init__.py]
[file src/anamespace/foo/bar.py]
def bar(a: int, b: int) -> str:
return a + b
[out]
src/anamespace/foo/bar.py:2: error: Incompatible return value type (got "int", expected "str")
[case testNestedPEP420Packages]
# cmd: mypy -p pkg --namespace-packages
[file pkg/a1/b/c/d/e.py]
x = 0 # type: str
[file pkg/a1/b/f.py]
from pkg.a1.b.c.d.e import x
x()
[file pkg/a2/__init__.py]
[file pkg/a2/b/c/d/e.py]
x = 0 # type: str
[file pkg/a2/b/f.py]
from pkg.a2.b.c.d.e import x
x()
[out]
pkg/a2/b/c/d/e.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
pkg/a1/b/c/d/e.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
pkg/a2/b/f.py:2: error: "str" not callable
pkg/a1/b/f.py:2: error: "str" not callable
[case testFollowImportStubs1]
# cmd: mypy main.py
[file mypy.ini]
\[mypy]
\[mypy-math.*]
follow_imports = error
follow_imports_for_stubs = True
[file main.py]
import math
math.frobnicate()
[out]
main.py:1: error: Import of "math" ignored
main.py:1: note: (Using --follow-imports=error, module not passed on command line)
[case testFollowImportStubs2]
# cmd: mypy main.py
[file mypy.ini]
\[mypy]
\[mypy-math.*]
follow_imports = skip
follow_imports_for_stubs = True
[file main.py]
import math
math.frobnicate()
[case testShadowFile1]
# cmd: mypy --shadow-file source.py shadow.py source.py
[file source.py]
def foo() -> str:
return "bar"
[file shadow.py]
def bar() -> str:
return 14
[out]
source.py:2: error: Incompatible return value type (got "int", expected "str")
[case testShadowFile2]
# 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
[file s1.py]
def foo() -> str:
return "bar"
[file shad1.py]
def bar() -> str:
return 14
[file s2.py]
def baz() -> str:
return 14
[file shad2.py]
def baz() -> int:
return 14
[file s3.py]
def qux() -> str:
return "bar"
[file shad3.py]
def foo() -> int:
return [42]
[file s4.py]
def foo() -> str:
return 9
[out]
s4.py:2: error: Incompatible return value type (got "int", expected "str")
s3.py:2: error: Incompatible return value type (got "List[int]", expected "int")
s1.py:2: error: Incompatible return value type (got "int", expected "str")
[case testShadowFileWithPretty]
# cmd: mypy a.py --pretty --shadow-file a.py b.py
[file a.py]
b: bytes
[file b.py]
a: int = ""
b: bytes = 1
[out]
a.py:1: error: Incompatible types in assignment (expression has type "str",
variable has type "int")
a: int = ""
^~
a.py:2: error: Incompatible types in assignment (expression has type "int",
variable has type "bytes")
b: bytes = 1
^
[case testConfigWarnUnusedSection1]
# cmd: mypy foo.py quux.py spam/eggs.py
[file mypy.ini]
\[mypy]
warn_unused_configs = True
incremental = False
\[mypy-bar]
\[mypy-foo]
\[mypy-baz.*]
\[mypy-quux.*]
\[mypy-spam.*]
\[mypy-spam.eggs]
\[mypy-emarg.*]
\[mypy-emarg.hatch]
-- Currently we don't treat an unstructured pattern like a.*.b as unused
-- if it matches another section (like a.x.b). This would be reasonable
-- to change. '
\[mypy-a.*.b]
\[mypy-a.*.c]
\[mypy-a.x.b]
[file foo.py]
[file quux.py]
[file spam/__init__.py]
[file spam/eggs.py]
[out]
Warning: unused section(s) in mypy.ini: [mypy-bar], [mypy-baz.*], [mypy-emarg.*], [mypy-emarg.hatch], [mypy-a.*.c], [mypy-a.x.b]
== Return code: 0
[case testConfigUnstructuredGlob]
# cmd: mypy emarg foo
[file mypy.ini]
\[mypy]
ignore_errors = true
\[mypy-*.lol]
ignore_errors = false
\[mypy-emarg.*]
ignore_errors = false
\[mypy-emarg.*.villip.*]
ignore_errors = true
\[mypy-emarg.hatch.villip.mankangulisk]
ignore_errors = false
[file emarg/__init__.py]
[file emarg/foo.py]
fail
[file emarg/villip.py]
fail
[file emarg/hatch/__init__.py]
[file emarg/hatch/villip/__init__.py]
[file emarg/hatch/villip/nus.py]
fail
[file emarg/hatch/villip/mankangulisk.py]
fail
[file foo/__init__.py]
[file foo/lol.py]
fail
[out]
foo/lol.py:1: error: Name "fail" is not defined
emarg/foo.py:1: error: Name "fail" is not defined
emarg/hatch/villip/mankangulisk.py:1: error: Name "fail" is not defined
[case testPackageRootEmpty]
# cmd: mypy --no-namespace-packages --package-root= a/b/c.py main.py
[file a/b/c.py]
[file main.py]
import a.b.c
[case testPackageRootEmptyNamespacePackage]
# cmd: mypy --namespace-packages --package-root= a/b/c.py main.py
[file a/b/c.py]
[file main.py]
import a.b.c
[case testPackageRootNonEmpty]
# cmd: mypy --package-root=a/ a/b/c.py main.py
[file a/b/c.py]
[file main.py]
import b.c
[case testPackageRootMultiple1]
# cmd: mypy --package-root=. --package-root=a a/b/c.py d.py main.py
[file a/b/c.py]
[file d.py]
[file main.py]
import b.c
import d
[case testPackageRootMultiple2]
# cmd: mypy --package-root=a/ --package-root=./ a/b/c.py d.py main.py
[file a/b/c.py]
[file d.py]
[file main.py]
import b.c
import d
[case testCacheMap]
-- This just checks that a valid --cache-map triple is accepted.
-- (Errors are too verbose to check.)
# cmd: mypy a.py --no-sqlite-cache --cache-map a.py a.meta.json a.data.json
[file a.py]
[out]
[case testIniFiles]
# cmd: mypy
[file mypy.ini]
\[mypy]
files = a.py, b.py
[file a.py]
fail
[file b.py]
fail
[out]
b.py:1: error: Name "fail" is not defined
a.py:1: error: Name "fail" is not defined
[case testIniFilesGlobbing]
# cmd: mypy
[file mypy.ini]
\[mypy]
files = **/*.py
[file a/b.py]
fail
[file c.py]
fail
[out]
a/b.py:1: error: Name "fail" is not defined
c.py:1: error: Name "fail" is not defined
[case testIniFilesCmdlineOverridesConfig]
# cmd: mypy override.py
[file mypy.ini]
\[mypy]
files = config.py
[out]
mypy: can't read file 'override.py': No such file or directory
== Return code: 2
[case testErrorSummaryOnSuccess]
# cmd: mypy --error-summary good.py
[file good.py]
x = 2 + 2
[out]
Success: no issues found in 1 source file
== Return code: 0
[case testErrorSummaryOnFail]
# cmd: mypy --error-summary bad.py
[file bad.py]
42 + 'no'
[out]
bad.py:1: error: Unsupported operand types for + ("int" and "str")
Found 1 error in 1 file (checked 1 source file)
[case testErrorSummaryOnFailNotes]
# cmd: mypy --error-summary bad.py
[file bad.py]
from typing import List
x = [] # type: List[float]
y = [] # type: List[int]
x = y
[out]
bad.py:4: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[float]")
bad.py:4: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
bad.py:4: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)
[case testErrorSummaryOnFailTwoErrors]
# cmd: mypy --error-summary bad.py foo.py
[file bad.py]
42 + 'no'
42 + 'no'
[file foo.py]
[out]
bad.py:1: error: Unsupported operand types for + ("int" and "str")
bad.py:2: error: Unsupported operand types for + ("int" and "str")
Found 2 errors in 1 file (checked 2 source files)
[case testErrorSummaryOnFailTwoFiles]
# cmd: mypy --error-summary bad.py bad2.py
[file bad.py]
42 + 'no'
[file bad2.py]
42 + 'no'
[out]
bad2.py:1: error: Unsupported operand types for + ("int" and "str")
bad.py:1: error: Unsupported operand types for + ("int" and "str")
Found 2 errors in 2 files (checked 2 source files)
[case testErrorSummaryOnBadUsage]
# cmd: mypy --error-summary missing.py
[out]
mypy: can't read file 'missing.py': No such file or directory
== Return code: 2
[case testShowSourceCodeSnippetsWrappedFormatting]
# cmd: mypy --pretty some_file.py
[file some_file.py]
from typing import Union
42 + 'no way'
class OneCustomClassName:
def some_interesting_method(self, arg: AnotherCustomClassDefinedBelow) -> AnotherCustomClassDefinedBelow:
...
class AnotherCustomClassDefinedBelow:
def another_even_more_interesting_method(self, arg: Union[int, str, float]) -> None:
self.very_important_attribute_with_long_name: OneCustomClassName = OneCustomClassName().some_interesting_method(arg)
[out]
some_file.py:3: error: Unsupported operand types for + ("int" and "str")
42 + 'no way'
^~~~~~~~
some_file.py:11: error: Incompatible types in assignment (expression has type
"AnotherCustomClassDefinedBelow", variable has type "OneCustomClassName")
...t_attribute_with_long_name: OneCustomClassName = OneCustomClassName()....
^~~~~~~~~~~~~~~~~~~~~...
some_file.py:11: error: Argument 1 to "some_interesting_method" of
"OneCustomClassName" has incompatible type "Union[int, str, float]"; expected
"AnotherCustomClassDefinedBelow"
...OneCustomClassName = OneCustomClassName().some_interesting_method(arg)
^~~
[case testShowSourceCodeSnippetsBlockingError]
# cmd: mypy --pretty --show-error-codes some_file.py
[file some_file.py]
it_looks_like_we_started_typing_something_but_then. = did_not_notice(an_extra_dot)
[out]
some_file.py:1: error: invalid syntax [syntax]
...ooks_like_we_started_typing_something_but_then. = did_not_notice(an_ex...
^
== Return code: 2
[case testTabRenderingUponError]
# cmd: mypy --pretty tabs.py
[file tabs.py]
def test_tabs() -> str:
return None
def test_between(x: str) -> None: ...
test_between(1 + 1)
[out]
tabs.py:2: error: Incompatible return value type (got "None", expected "str")
return None
^~~~
tabs.py:4: error: Argument 1 to "test_between" has incompatible type "int";
expected "str"
test_between(1 + 1)
^~~~~~~~~~~~
[case testErrorMessageWhenOpenPydFile]
# cmd: mypy a.pyd
[file a.pyd]
# coding: uft-8
[out]
mypy: stubgen does not support .pyd files: 'a.pyd'
== Return code: 2
[case testDuplicateModules]
# cmd: mypy src
[file mypy.ini]
\[mypy]
mypy_path = src
[file src/__init__.py]
[file src/a.py]
import foo.bar
[file src/foo/__init__.py]
[file src/foo/bar.py]
1+'x'
[out]
src/foo/bar.py: error: Source file found twice under different module names: "src.foo.bar" and "foo.bar"
src/foo/bar.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
src/foo/bar.py: note: Common resolutions include: a) adding `__init__.py` somewhere, b) using `--explicit-package-bases` or adjusting MYPYPATH
== Return code: 2
[case testEnableInvalidErrorCode]
# cmd: mypy --enable-error-code YOLO test.py
[file test.py]
x = 1
[out]
usage: mypy [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Invalid error code(s): YOLO
== Return code: 2
[case testDisableInvalidErrorCode]
# cmd: mypy --disable-error-code YOLO test.py
[file test.py]
x = 1
[out]
usage: mypy [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Invalid error code(s): YOLO
== Return code: 2
[case testEnableAndDisableInvalidErrorCode]
# cmd: mypy --disable-error-code YOLO --enable-error-code YOLO2 test.py
[file test.py]
x = 1
[out]
usage: mypy [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Invalid error code(s): YOLO, YOLO2
== Return code: 2
[case testEnableValidAndInvalidErrorCode]
# cmd: mypy --enable-error-code attr-defined --enable-error-code YOLO test.py
[file test.py]
x = 1
[out]
usage: mypy [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Invalid error code(s): YOLO
== Return code: 2
[case testDisableValidAndInvalidErrorCode]
# cmd: mypy --disable-error-code attr-defined --disable-error-code YOLO test.py
[file test.py]
x = 1
[out]
usage: mypy [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Invalid error code(s): YOLO
== Return code: 2
[case testStubsDirectory]
# cmd: mypy --error-summary pkg-stubs
[file pkg-stubs/__init__.pyi]
[file pkg-stubs/thing.pyi]
class Thing: ...
[out]