/
check-flags.test
1586 lines (1255 loc) · 40.4 KB
/
check-flags.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
[case testUnannotatedFunction]
# flags: --disallow-untyped-defs
def f(x): pass
[out]
main:2: error: Function is missing a type annotation
[case testUnannotatedArgument]
# flags: --disallow-untyped-defs
def f(x) -> int: pass
[out]
main:2: error: Function is missing a type annotation for one or more arguments
[case testNoArgumentFunction]
# flags: --disallow-untyped-defs
def f() -> int: pass
[out]
[case testUnannotatedReturn]
# flags: --disallow-untyped-defs
def f(x: int): pass
[out]
main:2: error: Function is missing a return type annotation
[case testUnannotatedReturnWithFastParser]
# flags: --disallow-untyped-defs
def f(x: int): pass
[out]
main:2: error: Function is missing a return type annotation
[case testLambda]
# flags: --disallow-untyped-defs
lambda x: x
[out]
[case testUntypedDef]
# flags: --disallow-untyped-defs
def f():
1 + "str"
[out]
main:2: error: Function is missing a return type annotation
main:2: note: Use "-> None" if function does not return a value
[case testUnannotatedReturnWithOnlySelfArgument]
# flags: --disallow-untyped-defs
def f(self): pass
[out]
main:2: error: Function is missing a return type annotation
main:2: note: Use "-> None" if function does not return a value
[case testUnannotatedReturnWithNontrivialReturn]
# flags: --disallow-untyped-defs
def f(): return 1
[out]
main:2: error: Function is missing a return type annotation
[case testUntypedAsyncDef]
# flags: --disallow-untyped-defs
async def f(): # E: Function is missing a return type annotation \
# N: Use "-> None" if function does not return a value
pass
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-medium.pyi]
[case testAsyncUnannotatedArgument]
# flags: --disallow-untyped-defs
async def f(x) -> None: # E: Function is missing a type annotation for one or more arguments
pass
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
[case testAsyncUnannotatedReturn]
# flags: --disallow-untyped-defs
from typing import Any
async def f(x: int): # E: Function is missing a return type annotation
pass
# Make sure explicit Any is allowed.
async def g(x: int) -> Any:
pass
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
[case testDisallowUntypedDefsUntypedDecorator]
# flags: --disallow-untyped-decorators
def d(p):
return p
@d # E: Untyped decorator makes function "f" untyped
def f(i: int) -> int:
return i
[case testDisallowUntypedDecoratorsUnresolvedDecorator]
# flags: --disallow-untyped-decorators --ignore-missing-imports
from nonexistent import d
@d # E: Untyped decorator makes function "f" untyped
def f(i: int) -> int:
return i
[case testDisallowUntypedDecoratorUntypedDef]
# flags: --disallow-untyped-decorators
def d(p):
return p
@d # no error
def f(): pass
[case testDisallowUntypedDecoratorsPartialFunction]
# flags: --disallow-untyped-decorators
def d(p):
return p
@d # E: Untyped decorator makes function "f" untyped
def f(x) -> None: pass
@d # E: Untyped decorator makes function "g" untyped
def g(x, y: int): pass
@d # E: Untyped decorator makes function "h" untyped
def h(x: int): pass
[case testDisallowUntypedDecoratorsImpreciseDecorator]
# flags: --disallow-untyped-decorators
from typing import Any
def d(p) -> Any:
return p
@d # no error
def f() -> None: pass
[case testDisallowUntypedDecoratorsMultipleDecorators]
# flags: --disallow-untyped-decorators
from typing import Any
def d1(p):
return p
def d2(p):
return p
def d3(p) -> Any:
return p
@d1 # E: Untyped decorator makes function "f" untyped
@d2 # E: Untyped decorator makes function "f" untyped
@d3 # no error
@d1 # E: Untyped decorator makes function "f" untyped
def f() -> None: pass
[case testDisallowUntypedDecoratorsCallableInstance]
# flags: --disallow-untyped-decorators
from typing import Callable
class TypedDecorator:
def __call__(self, c: Callable) -> Callable:
return function
class UntypedDecorator:
def __call__(self, c):
return function
@TypedDecorator()
def f() -> None: pass
@UntypedDecorator() # E: Untyped decorator makes function "g" untyped
def g() -> None: pass
@TypedDecorator()
@UntypedDecorator() # E: Untyped decorator makes function "h" untyped
def h() -> None: pass
@UntypedDecorator() # E: Untyped decorator makes function "i" untyped
@TypedDecorator()
def i() -> None: pass
reveal_type(f) # N: Revealed type is 'def (*Any, **Any) -> Any'
reveal_type(g) # N: Revealed type is 'Any'
reveal_type(h) # N: Revealed type is 'def (*Any, **Any) -> Any'
reveal_type(i) # N: Revealed type is 'Any'
[case testDisallowUntypedDecoratorsNonCallableInstance]
# flags: --disallow-untyped-decorators
class Decorator:
pass
@Decorator() # E: "Decorator" not callable
def f() -> None: pass
[case testSubclassingAny]
# flags: --disallow-subclassing-any
from typing import Any
FakeClass = None # type: Any
class Foo(FakeClass): pass # E: Class cannot subclass 'FakeClass' (has type 'Any')
[out]
[case testSubclassingAnyMultipleBaseClasses]
# flags: --disallow-subclassing-any
from typing import Any
FakeClass = None # type: Any
class ActualClass: pass
class Foo(ActualClass, FakeClass): pass # E: Class cannot subclass 'FakeClass' (has type 'Any')
[out]
[case testSubclassingAnySilentImports]
# flags: --disallow-subclassing-any --follow-imports=skip
# cmd: mypy -m main
[file main.py]
from ignored_module import BaseClass
class Foo(BaseClass): pass
[file ignored_module.py]
class BaseClass: pass
[out]
tmp/main.py:2: error: Class cannot subclass 'BaseClass' (has type 'Any')
[case testSubclassingAnySilentImports2]
# flags: --disallow-subclassing-any --follow-imports=skip
# cmd: mypy -m main
[file main.py]
import ignored_module
class Foo(ignored_module.BaseClass): pass
[file ignored_module.py]
class BaseClass: pass
[out]
tmp/main.py:2: error: Class cannot subclass 'BaseClass' (has type 'Any')
[case testWarnNoReturnIgnoresTrivialFunctions]
# flags: --warn-no-return
def f() -> int:
pass
def g() -> int:
...
def h() -> int:
"""with docstring"""
pass
def i() -> int:
"""with docstring"""
...
def j() -> int:
u"""with unicode docstring"""
pass
def k() -> int:
"""docstring only"""
[case testWarnNoReturnWorksWithAlwaysTrue]
# flags: --warn-no-return
PY3 = True
def f() -> int:
if PY3:
return 0
else:
return 0
[builtins fixtures/bool.pyi]
[case testWarnNoReturnWorksWithAlwaysFalse]
# flags: --warn-no-return
PY2 = False
def f() -> int:
if PY2:
return 0
else:
return 0
[builtins fixtures/bool.pyi]
[case testWarnNoReturnWorksWithMypyTrue]
# flags: --warn-no-return
MYPY = False
def f() -> int:
if MYPY:
return 0
else:
return 0
[builtins fixtures/bool.pyi]
[case testNoReturnDisallowsReturn]
# flags: --warn-no-return
from mypy_extensions import NoReturn
def f() -> NoReturn:
if bool():
return 5 # E: Return statement in function which does not return
else:
return # E: Return statement in function which does not return
[builtins fixtures/dict.pyi]
[case testNoReturnWithoutImplicitReturn]
# flags: --warn-no-return
from mypy_extensions import NoReturn
def no_return() -> NoReturn: pass
def f() -> NoReturn:
no_return()
[builtins fixtures/dict.pyi]
[case testNoReturnDisallowsImplicitReturn]
# flags: --warn-no-return
from mypy_extensions import NoReturn
def f() -> NoReturn: # E: Implicit return in function which does not return
non_trivial_function = 1
[builtins fixtures/dict.pyi]
[case testNoReturnNoWarnNoReturn]
# flags: --warn-no-return
from mypy_extensions import NoReturn
def no_return() -> NoReturn: pass
def f() -> int:
if bool():
return 0
else:
no_return()
[builtins fixtures/dict.pyi]
[case testNoReturnInExpr]
# flags: --warn-no-return
from mypy_extensions import NoReturn
def no_return() -> NoReturn: pass
def f() -> int:
return 0
reveal_type(f() or no_return()) # N: Revealed type is 'builtins.int'
[builtins fixtures/dict.pyi]
[case testNoReturnVariable]
# flags: --warn-no-return
from mypy_extensions import NoReturn
x = 0 # type: NoReturn # E: Incompatible types in assignment (expression has type "int", variable has type "NoReturn")
[builtins fixtures/dict.pyi]
[case testNoReturnImportFromTyping]
from typing import NoReturn
def h() -> NoReturn:
if bool():
return 5 # E: Return statement in function which does not return
else:
return # E: Return statement in function which does not return
def no_return() -> NoReturn: pass
def f() -> NoReturn:
no_return()
x: NoReturn = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "NoReturn")
[builtins fixtures/dict.pyi]
[case testShowErrorContextFunction]
# flags: --show-error-context
def f() -> None:
0 + ""
[out]
main: note: In function "f":
main:3: error: Unsupported operand types for + ("int" and "str")
[case testShowErrorContextClass]
# flags: --show-error-context
class A:
0 + ""
[out]
main: note: In class "A":
main:3: error: Unsupported operand types for + ("int" and "str")
[case testShowErrorContextMember]
# flags: --show-error-context
class A:
def f(self, x: int) -> None:
self.f("")
[out]
main: note: In member "f" of class "A":
main:4: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
[case testShowErrorContextModule]
# flags: --show-error-context
import m
[file m.py]
0 + ""
[out]
main:2: note: In module imported here:
tmp/m.py:1: error: Unsupported operand types for + ("int" and "str")
[case testShowErrorContextTopLevel]
# flags: --show-error-context
def f() -> None:
0 + ""
0 + ""
[out]
main: note: In function "f":
main:3: error: Unsupported operand types for + ("int" and "str")
main: note: At top level:
main:4: error: Unsupported operand types for + ("int" and "str")
[case testShowErrorContextFromHere]
# flags: --show-error-context
import a
[file a.py]
import b
[file b.py]
0 + ""
[out]
tmp/a.py:1: note: In module imported here,
main:2: note: ... from here:
tmp/b.py:1: error: Unsupported operand types for + ("int" and "str")
[case testFollowImportsNormal]
# flags: --follow-imports=normal
from mod import x
x + ""
[file mod.py]
1 + ""
x = 0
[out]
tmp/mod.py:1: error: Unsupported operand types for + ("int" and "str")
main:3: error: Unsupported operand types for + ("int" and "str")
[case testFollowImportsSilent]
# flags: --follow-imports=silent
from mod import x
x + "" # E: Unsupported operand types for + ("int" and "str")
[file mod.py]
1 + ""
x = 0
[case testFollowImportsSilentTypeIgnore]
# flags: --warn-unused-ignores --follow-imports=silent
import mod
[file mod.py]
x = 3 # type: ignore
[case testFollowImportsSkip]
# flags: --follow-imports=skip
from mod import x
x + ""
[file mod.py]
this deliberate syntax error will not be reported
[out]
[case testFollowImportsError]
# flags: --follow-imports=error
from mod import x
x + ""
[file mod.py]
deliberate syntax error
[out]
main:2: error: Import of 'mod' ignored
main:2: note: (Using --follow-imports=error, module not passed on command line)
[case testIgnoreMissingImportsFalse]
from mod import x
[out]
main:1: error: Cannot find implementation or library stub for module named 'mod'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
[case testIgnoreMissingImportsTrue]
# flags: --ignore-missing-imports
from mod import x
[out]
[case testPerFileIncompleteDefsBasic]
# flags: --config-file tmp/mypy.ini
import standard, incomplete
[file standard.py]
def incomplete(x) -> int:
return 0
[file incomplete.py]
def incomplete(x) -> int: # E: Function is missing a type annotation for one or more arguments
return 0
[file mypy.ini]
\[mypy]
disallow_incomplete_defs = False
\[mypy-incomplete]
disallow_incomplete_defs = True
[case testPerFileStrictOptionalBasic]
# flags: --config-file tmp/mypy.ini
import standard, optional
[file standard.py]
x = 0
if int():
x = None
[file optional.py]
x = 0
if int():
x = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[file mypy.ini]
\[mypy]
strict_optional = False
\[mypy-optional]
strict_optional = True
[case testPerFileStrictOptionalBasicImportStandard]
# flags: --config-file tmp/mypy.ini
import standard, optional
[file standard.py]
from typing import Optional
def f(x: int) -> None: pass
an_int = 0 # type: int
optional_int = None # type: Optional[int]
f(an_int) # ints can be used as ints
f(optional_int) # optional ints can be used as ints in this file
[file optional.py]
import standard
def f(x: int) -> None: pass
standard.an_int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
standard.optional_int = None # OK -- explicitly declared as optional
f(standard.an_int) # ints can be used as ints
f(standard.optional_int) # E: Argument 1 to "f" has incompatible type "None"; expected "int"
[file mypy.ini]
\[mypy]
strict_optional = False
\[mypy-optional]
strict_optional = True
[case testPerFileStrictOptionalBasicImportOptional]
# flags: --config-file tmp/mypy.ini
import standard, optional
[file standard.py]
import optional
def f(x: int) -> None: pass
f(optional.x) # OK -- in non-strict Optional context
f(optional.y) # OK -- in non-strict Optional context
[file optional.py]
from typing import Optional
def f(x: int) -> None: pass
x = 0 # type: Optional[int]
y = None # type: None
[file mypy.ini]
\[mypy]
strict_optional = False
\[mypy-optional]
strict_optional = True
[case testPerFileStrictOptionalListItemImportOptional]
# flags: --config-file tmp/mypy.ini
import standard, optional
[file standard.py]
import optional
from typing import List
def f(x: List[int]) -> None: pass
f(optional.x) # OK -- in non-strict Optional context
f(optional.y) # OK -- in non-strict Optional context
[file optional.py]
from typing import Optional, List
def f(x: List[int]) -> None: pass
x = [] # type: List[Optional[int]]
y = [] # type: List[int]
[file mypy.ini]
\[mypy]
strict_optional = False
\[mypy-optional]
strict_optional = True
[builtins fixtures/list.pyi]
[case testPerFileStrictOptionalComplicatedList]
from typing import Union, Optional, List
def f() -> None:
x = [] # type: Union[List[Optional[str]], str]
[builtins fixtures/list.pyi]
[case testPerFileStrictOptionalNoneArguments]
# flags: --config-file tmp/mypy.ini
import standard, optional
[file standard.py]
def f(x: int = None) -> None: pass
[file optional.py]
import standard
def f(x: int = None) -> None: pass
standard.f(None)
[file mypy.ini]
\[mypy]
strict_optional = False
\[mypy-optional]
strict_optional = True
[case testDisallowImplicitTypesIgnoreMissingTypes]
# flags: --ignore-missing-imports --disallow-any-unimported
from missing import MyType
def f(x: MyType) -> None: # E: Argument 1 to "f" becomes "Any" due to an unfollowed import
pass
[case testDisallowImplicitTypes]
# flags: --disallow-any-unimported
from missing import MyType
def f(x: MyType) -> None:
pass
[out]
main:2: error: Cannot find implementation or library stub for module named 'missing'
main:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: Argument 1 to "f" becomes "Any" due to an unfollowed import
[case testDisallowImplicitAnyVariableDefinition]
# flags: --ignore-missing-imports --disallow-any-unimported
from missing import Unchecked
t: Unchecked = 12 # E: Type of variable becomes "Any" due to an unfollowed import
[case testDisallowImplicitAnyGeneric]
# flags: --ignore-missing-imports --disallow-any-unimported
from missing import Unchecked
from typing import List
def foo(l: List[Unchecked]) -> List[Unchecked]:
t = [] # type: List[Unchecked]
return l
[builtins fixtures/list.pyi]
[out]
main:5: error: Return type becomes "List[Any]" due to an unfollowed import
main:5: error: Argument 1 to "foo" becomes "List[Any]" due to an unfollowed import
main:6: error: Type of variable becomes "List[Any]" due to an unfollowed import
[case testDisallowImplicitAnyInherit]
# flags: --ignore-missing-imports --disallow-any-unimported
from missing import Unchecked
from typing import List
class C(Unchecked): # E: Base type Unchecked becomes "Any" due to an unfollowed import
pass
class A(List[Unchecked]): # E: Base type becomes "List[Any]" due to an unfollowed import
pass
[builtins fixtures/list.pyi]
[case testDisallowImplicitAnyAlias]
# flags: --ignore-missing-imports --disallow-any-unimported
from missing import Unchecked
from typing import List
X = List[Unchecked]
def f(x: X) -> None: # E: Argument 1 to "f" becomes "List[Any]" due to an unfollowed import
pass
[builtins fixtures/list.pyi]
[case testDisallowImplicitAnyCast]
# flags: --ignore-missing-imports --disallow-any-unimported
from missing import Unchecked
from typing import List, cast
foo = [1, 2, 3]
cast(List[Unchecked], foo) # E: Target type of cast becomes "List[Any]" due to an unfollowed import
cast(Unchecked, foo) # E: Target type of cast becomes "Any" due to an unfollowed import
[builtins fixtures/list.pyi]
[case testDisallowImplicitAnyNamedTuple]
# flags: --ignore-missing-imports --disallow-any-unimported
from typing import List, NamedTuple
from missing import Unchecked
Point = NamedTuple('Point', [('x', List[Unchecked]),
('y', Unchecked)])
[builtins fixtures/list.pyi]
[out]
main:5: error: NamedTuple type becomes "Tuple[List[Any], Any]" due to an unfollowed import
[case testDisallowImplicitAnyTypeVarConstraints]
# flags: --ignore-missing-imports --disallow-any-unimported
from typing import List, NamedTuple, TypeVar, Any
from missing import Unchecked
T = TypeVar('T', Unchecked, List[Unchecked], str)
[builtins fixtures/list.pyi]
[out]
main:5: error: Constraint 1 becomes "Any" due to an unfollowed import
main:5: error: Constraint 2 becomes "List[Any]" due to an unfollowed import
[case testDisallowImplicitAnyNewType]
# flags: --ignore-missing-imports --disallow-any-unimported
from typing import NewType, List
from missing import Unchecked
Baz = NewType('Baz', Unchecked) # E: Argument 2 to NewType(...) must be subclassable (got "Any")
Bar = NewType('Bar', List[Unchecked]) # E: Argument 2 to NewType(...) becomes "List[Any]" due to an unfollowed import
[builtins fixtures/list.pyi]
[case testDisallowImplicitAnyCallableAndTuple]
# flags: --ignore-missing-imports --disallow-any-unimported
from typing import Callable, Tuple
from missing import Unchecked
def foo(f: Callable[[], Unchecked]) -> Tuple[Unchecked]:
return f()
[builtins fixtures/list.pyi]
[out]
main:5: error: Return type becomes "Tuple[Any]" due to an unfollowed import
main:5: error: Argument 1 to "foo" becomes "Callable[[], Any]" due to an unfollowed import
[case testDisallowImplicitAnySubclassingExplicitAny]
# flags: --ignore-missing-imports --disallow-any-unimported --disallow-subclassing-any
from typing import Any
class C(Any): # E: Class cannot subclass 'Any' (has type 'Any')
pass
[case testDisallowImplicitAnyVarDeclaration]
# flags: --ignore-missing-imports --disallow-any-unimported
from missing import Unchecked
foo: Unchecked = ""
foo = ""
x, y = 1, 2 # type: Unchecked, Unchecked
[builtins fixtures/tuple.pyi]
[out]
main:4: error: Type of variable becomes "Any" due to an unfollowed import
main:6: error: A type on this line becomes "Any" due to an unfollowed import
[case testDisallowUnimportedAnyTypedDictSimple]
# flags: --ignore-missing-imports --disallow-any-unimported
from mypy_extensions import TypedDict
from x import Unchecked
M = TypedDict('M', {'x': str, 'y': Unchecked}) # E: Type of a TypedDict key becomes "Any" due to an unfollowed import
def f(m: M) -> M: pass # no error
[builtins fixtures/dict.pyi]
[case testDisallowUnimportedAnyTypedDictGeneric]
# flags: --ignore-missing-imports --disallow-any-unimported
from mypy_extensions import TypedDict
from typing import List
from x import Unchecked
M = TypedDict('M', {'x': str, 'y': List[Unchecked]}) # E: Type of a TypedDict key becomes "List[Any]" due to an unfollowed import
def f(m: M) -> M: pass # no error
[builtins fixtures/dict.pyi]
[case testDisallowAnyDecoratedUnannotatedDecorator]
# flags: --disallow-any-decorated
from typing import Any
def d(f):
return f
@d
def f(x: Any) -> Any: # E: Function is untyped after decorator transformation
pass
@d
def h(x): # E: Function is untyped after decorator transformation
pass
[builtins fixtures/list.pyi]
[case testDisallowAnyDecoratedErrorIsReportedOnlyOnce]
# flags: --disallow-any-decorated
def d(f):
return f
def d2(f):
return f
@d
@d2
@d
def f(x: int) -> None: pass # E: Function is untyped after decorator transformation
[case testDisallowAnyDecoratedReturnAny]
# flags: --disallow-any-decorated
from typing import Any
def d(f) -> Any:
return f
@d
def f() -> None: pass # E: Function is untyped after decorator transformation
[builtins fixtures/list.pyi]
[case testDisallowAnyDecoratedReturnCallable]
# flags: --disallow-any-decorated
from typing import Any, Callable
def d(f) -> Callable[..., None]:
return f
@d
def g(i: int, s: str) -> None: pass # E: Type of decorated function contains type "Any" ("Callable[..., None]")
[builtins fixtures/list.pyi]
[case testDisallowAnyDecoratedNonexistentDecorator]
# flags: --disallow-any-decorated --ignore-missing-imports
from nonexistent import d
@d
def f() -> None: pass # E: Function is untyped after decorator transformation
[builtins fixtures/list.pyi]
[case testDisallowAnyDecoratedPartlyTypedCallable]
# flags: --disallow-any-decorated --ignore-missing-imports
from typing import Callable, Any, List
def d(f) -> Callable[[int, Any], Any]: pass
def d2(f) -> Callable[[int], List[Any]]: pass
def d3(f) -> Callable[[Any], List[str]]: pass
@d
def f(i: int, s: str) -> None: # E: Type of decorated function contains type "Any" ("Callable[[int, Any], Any]")
pass
@d2
def g(i: int) -> None: # E: Type of decorated function contains type "Any" ("Callable[[int], List[Any]]")
pass
@d3
def h(i: int) -> None: # E: Type of decorated function contains type "Any" ("Callable[[Any], List[str]]")
pass
[builtins fixtures/list.pyi]
[case testDisallowAnyDecoratedReturnsCallableNoParams]
# flags: --disallow-any-decorated
from typing import Callable
def d(p) -> Callable[[], int]:
return p
@d
def f(i):
return i
[builtins fixtures/list.pyi]
[case testDisallowAnyDecoratedDecoratorReturnsNonCallable]
# flags: --disallow-any-decorated
def d(p) -> int:
return p(0)
@d
def f(i):
return i
[case testDisallowAnyDecoratedUntypedUndecoratedFunction]
# flags: --disallow-any-decorated
from typing import Callable
def f(i): # no error
return i
[case testDisallowAnyDecoratedTwoDecorators]
# flags: --disallow-any-decorated
from typing import Callable
def typed_dec(f) -> Callable[[], int]: pass
def untyped_dec(f): pass
@typed_dec
@untyped_dec
def f(): # no error
return i
@untyped_dec
@typed_dec
def g(): # E: Function is untyped after decorator transformation
return i
[case testDisallowAnyExprSimple]
# flags: --disallow-any-expr
from typing import Any
def f(s):
yield s
x = f(0) # E: Expression has type "Any"
for x in f(0): # E: Expression has type "Any"
g(x) # E: Expression has type "Any"
def g(x) -> Any:
yield x # E: Expression has type "Any"
l = [1, 2, 3]
l[f(0)] # E: Expression has type "Any"
f(l)
f(f(0)) # E: Expression has type "Any"
[builtins fixtures/list.pyi]
[case testDisallowAnyExprUnannotatedFunction]
# flags: --disallow-any-expr
def g(s):
return s
g(0)
w: int = g(1)
[case testDisallowAnyExprExplicitAnyParam]
# flags: --disallow-any-expr
from typing import Any, List
def f(s: Any) -> None:
pass
def g(s: List[Any]) -> None:
pass
f(0)
# type of list below is inferred with expected type of "List[Any]", so that becomes it's type
# instead of List[str]
g(['']) # E: Expression type contains "Any" (has type "List[Any]")
[builtins fixtures/list.pyi]
[case testDisallowAnyExprAllowsAnyInCast]
# flags: --disallow-any-expr
from typing import Any, cast
class Foo:
g: Any = 2
z = cast(int, Foo().g)
m = cast(Any, Foo().g) # E: Expression has type "Any"
k = Foo.g # E: Expression has type "Any"
[builtins fixtures/list.pyi]
[case testDisallowAnyExprAllowsAnyInVariableAssignmentWithExplicitTypeAnnotation]
# flags: --disallow-any-expr
from typing import Any
class Foo:
g: Any = 2
z: int = Foo().g
x = Foo().g # type: int
m: Any = Foo().g # E: Expression has type "Any"
n = Foo().g # type: Any # E: Expression has type "Any"
[builtins fixtures/list.pyi]
[case testDisallowAnyExprGeneric]
# flags: --disallow-any-expr
from typing import List
l: List = []
l.append(1) # E: Expression type contains "Any" (has type "List[Any]")
k = l[0] # E: Expression type contains "Any" (has type "List[Any]") # E: Expression has type "Any"
[builtins fixtures/list.pyi]
[case testDisallowAnyExprTypeVar]
# flags: --disallow-any-expr
from typing import TypeVar
T = TypeVar('T') # no error
def f(t: T) -> T:
return t
[builtins fixtures/list.pyi]
[case testDisallowAnyExprNamedTuple]
# flags: --disallow-any-expr
from typing import NamedTuple
Point = NamedTuple('Point', [('x', int), ('y', int)]) # no error
def origin() -> Point:
return Point(x=0, y=0)
[builtins fixtures/list.pyi]
[case testDisallowAnyExprNewType]
# flags: --disallow-any-expr
from typing import NewType
NT = NewType('NT', int) # no error
def nt() -> NT:
return NT(1)
[builtins fixtures/list.pyi]
[case testDisallowAnyExprEnum]
# flags: --disallow-any-expr
from enum import Enum
E = Enum('E', '1, 2, 3') # no error
def k(s: E) -> None: pass
[builtins fixtures/list.pyi]
[case testDisallowAnyExprTypedDict]
# flags: --disallow-any-expr
from mypy_extensions import TypedDict
Movie = TypedDict('Movie', {'name': str, 'year': int})
def g(m: Movie) -> Movie:
return m
[builtins fixtures/dict.pyi]
[case testDisallowIncompleteDefs]
# flags: --disallow-incomplete-defs