-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathsemanal-errors.test
1515 lines (1301 loc) · 38.1 KB
/
semanal-errors.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 testUndefinedVariableInGlobalStatement]
import typing
x
y
[out]
main:2: error: Name "x" is not defined
main:3: error: Name "y" is not defined
[case testUndefinedVariableWithinFunctionContext]
import typing
def f() -> None:
x
y
[out]
main:3: error: Name "x" is not defined
main:4: error: Name "y" is not defined
[case testMethodScope]
import typing
class A:
def f(self): pass
f
[out]
main:4: error: Name "f" is not defined
[case testMethodScope2]
import typing
class A:
def f(self): pass
class B:
def g(self) -> None:
f # error
g # error
[out]
main:6: error: Name "f" is not defined
main:7: error: Name "g" is not defined
[case testInvalidType]
import typing
x = None # type: X
[out]
main:2: error: Name "X" is not defined
[case testInvalidGenericArg]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(Generic[t]): pass
x = 0 # type: A[y]
[out]
main:4: error: Name "y" is not defined
[case testInvalidNumberOfGenericArgsInTypeDecl]
from typing import TypeVar, Generic
t = TypeVar('t')
class A: pass
class B(Generic[t]): pass
x = 0 # type: B[A, A]
y = 0 # type: A[A]
[out]
main:5: error: "B" expects 1 type argument, but 2 given
main:6: error: "A" expects no type arguments, but 1 given
[case testInvalidNumberOfGenericArgsInUndefinedArg]
class A: pass
x = None # type: A[int] # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInNestedBlock]
class A: pass
class B:
def f(self) -> None:
while 1:
x = None # type: A[int] \
# E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInSignature]
import typing
class A: pass
def f() -> A[int]: pass # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInOverloadedSignature]
from typing import overload
class A: pass
@overload
def f(): pass
@overload
def f(x: A[int]) -> None: pass # E: "A" expects no type arguments, but 1 given
def f(*args): pass
[out]
[case testInvalidNumberOfGenericArgsInBaseType]
import typing
class A: pass
class B(A[int]): pass # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInCast]
from typing import cast
class A: pass
x = cast(A[int], 1) # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInNestedGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]): pass
class B: pass
def f() -> A[B[int]]: pass # E: "B" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInTupleType]
from typing import Tuple
class A: pass
x = None # type: Tuple[A[int]] # E: "A" expects no type arguments, but 1 given
[builtins fixtures/tuple.pyi]
[out]
[case testInvalidNumberOfGenericArgsInFunctionType]
from typing import Callable
class A: pass
x = None # type: Callable[[A[int]], int] # E: "A" expects no type arguments, but 1 given
y = None # type: Callable[[], A[int]] # E: "A" expects no type arguments, but 1 given
[out]
[case testVarOrFuncAsType]
import typing
def f(): pass
x = 1
y = 0 # type: f
z = 0 # type: x
[out]
main:5: error: Function "__main__.f" is not valid as a type
main:5: note: Perhaps you need "Callable[...]" or a callback protocol?
main:6: error: Variable "__main__.x" is not valid as a type
main:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[case testGlobalVarRedefinition]
import typing
class A: pass
x = 0 # type: A
x = 0 # type: A
[out]
main:4: error: Name "x" already defined on line 3
[case testLocalVarRedefinition]
import typing
class A: pass
def f() -> None:
x = 0 # type: A
x = 0 # type: A
[out]
main:5: error: Name "x" already defined on line 4
[case testClassVarRedefinition]
import typing
class A:
x = 0 # type: object
x = 0 # type: object
[out]
main:4: error: Name "x" already defined on line 3
[case testMultipleClassDefinitions]
import typing
class A: pass
class A: pass
[out]
main:3: error: Name "A" already defined on line 2
[case testMultipleMixedDefinitions]
import typing
x = 1
def x(): pass
class x: pass
[out]
main:3: error: Name "x" already defined on line 2
main:4: error: Name "x" already defined on line 2
[case testNameNotImported]
import typing
from m import y
x
[file m.py]
x = y = 1
[out]
main:3: error: Name "x" is not defined
[case testMissingNameInImportFrom]
import typing
from m import y
[file m.py]
x = 1
[out]
main:2: error: Module "m" has no attribute "y"
[case testMissingModule]
import typing
import m
[out]
main:2: error: Cannot find implementation or library stub for module named "m"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModule2]
import typing
from m import x
[out]
main:2: error: Cannot find implementation or library stub for module named "m"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModule3]
import typing
from m import *
[out]
main:2: error: Cannot find implementation or library stub for module named "m"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModuleRelativeImport]
import typing
import m
[file m/__init__.py]
from .x import y
[out]
tmp/m/__init__.py:1: error: Cannot find implementation or library stub for module named "m.x"
tmp/m/__init__.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModuleRelativeImport2]
import typing
import m.a
[file m/__init__.py]
[file m/a.py]
from .x import y
[out]
tmp/m/a.py:1: error: Cannot find implementation or library stub for module named "m.x"
tmp/m/a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testModuleNotImported]
import typing
import _m
_n.x
[file _m.py]
import _n
[file _n.py]
x = 1
[out]
main:3: error: Name "_n" is not defined
[case testImportAsteriskPlusUnderscore]
import typing
from _m import *
_x
__x__
[file _m.py]
_x = __x__ = 1
[out]
main:3: error: Name "_x" is not defined
main:4: error: Name "__x__" is not defined
[case testRelativeImportAtTopLevelModule]
from . import m
[out]
main:1: error: No parent module -- cannot perform relative import
[case testRelativeImportAtTopLevelModule2]
from .. import m
[out]
main:1: error: No parent module -- cannot perform relative import
[case testUndefinedTypeWithQualifiedName]
import typing
import m
def f() -> m.c: pass
def g() -> n.c: pass
[file m.py]
[out]
main:3: error: Name "m.c" is not defined
main:4: error: Name "n" is not defined
[case testMissingPackage]
import typing
import m.n
[out]
main:2: error: Cannot find implementation or library stub for module named "m.n"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Cannot find implementation or library stub for module named "m"
[case testMissingPackage2]
import typing
from m.n import x
from a.b import *
[out]
main:2: error: Cannot find implementation or library stub for module named "m.n"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:3: error: Cannot find implementation or library stub for module named "a.b"
[case testErrorInImportedModule]
import m
[file m.py]
import typing
x = y
[out]
tmp/m.py:2: error: Name "y" is not defined
[case testErrorInImportedModule2]
import m.n
[file m/__init__.py]
[file m/n.py]
import k
[file k.py]
import typing
x = y
[out]
tmp/k.py:2: error: Name "y" is not defined
[case testPackageWithoutInitFile]
# flags: --no-namespace-packages
import typing
import m.n
m.n.x
[file m/n.py]
x = 1
[out]
main:3: error: Cannot find implementation or library stub for module named "m.n"
main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:3: error: Cannot find implementation or library stub for module named "m"
[case testBreakOutsideLoop]
break
def f():
break
[out]
main:1: error: "break" outside loop
main:3: error: "break" outside loop
[case testContinueOutsideLoop]
continue
def f():
continue
[out]
main:1: error: "continue" outside loop
main:3: error: "continue" outside loop
[case testReturnOutsideFunction]
def f(): pass
return
return 1
[out]
main:2: error: "return" outside function
main:3: error: "return" outside function
[case testYieldOutsideFunction]
yield 1
yield
[out]
main:1: error: "yield" outside function
main:2: error: "yield" outside function
[case testInvalidLvalues1]
1 = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues2]
(1) = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues3]
(1, 1) = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues4]
[1, 1] = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues6]
x = y = z = 1 # ok
x, (y, 1) = 1
[out]
main:2: error: cannot assign to literal
[case testInvalidLvalues7]
x, [y, 1] = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues8]
x, [y, [z, 1]] = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues9]
x, (y) = 1 # ok
x, (y, (z, z)) = 1 # ok
x, (y, (z, 1)) = 1
[out]
main:3: error: cannot assign to literal
[case testInvalidLvalues10]
x + x = 1
[out]
main:1: error: cannot assign to operator
[out version>=3.10]
main:1: error: cannot assign to expression here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues11]
-x = 1
[out]
main:1: error: cannot assign to operator
[out version>=3.10]
main:1: error: cannot assign to expression here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues12]
1.1 = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues13]
'x' = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues14]
x() = 1
[out]
main:1: error: cannot assign to function call
[out version>=3.10]
main:1: error: cannot assign to function call here. Maybe you meant '==' instead of '='?
[case testTwoStarExpressions]
a, *b, *c = 1
*a, (*b, c) = 1
a, (*b, *c) = 1
[*a, *b] = 1
[out]
main:1: error: Two starred expressions in assignment
main:3: error: Two starred expressions in assignment
main:4: error: Two starred expressions in assignment
[case testTwoStarExpressionsInForStmt]
z = 1
for a, *b, *c in z:
pass
for *a, (*b, c) in z:
pass
for a, (*b, *c) in z:
pass
for [*a, *b] in z:
pass
[out]
main:2: error: Two starred expressions in assignment
main:6: error: Two starred expressions in assignment
main:8: error: Two starred expressions in assignment
[case testTwoStarExpressionsInGeneratorExpr]
(a for a, *b, *c in [])
(a for *a, (*b, c) in [])
(a for a, (*b, *c) in [])
[out]
main:1: error: Name "a" is not defined
main:1: error: Two starred expressions in assignment
main:3: error: Two starred expressions in assignment
[case testStarExpressionRhs]
b = 1
c = 1
d = 1
a = *b
[out]
main:4: error: can't use starred expression here
[case testStarExpressionInExp]
a = 1
*a + 1
[out]
main:2: error: can't use starred expression here
[case testInvalidDel1]
x = 1
del x(1)
[out]
main:2: error: cannot delete function call
[case testInvalidDel2]
x = 1
del x + 1
[out]
main:2: error: cannot delete operator
[out version>=3.10]
main:2: error: cannot delete expression
[case testInvalidDel3]
del z # E: Name "z" is not defined
[case testFunctionTvarScope]
from typing import TypeVar
t = TypeVar('t')
def f(x: t) -> t: pass
x = 0 # type: t
[out]
main:5: error: Type variable "__main__.t" is unbound
main:5: note: (Hint: Use "Generic[t]" or "Protocol[t]" base class to bind "t" inside a class)
main:5: note: (Hint: Use "t" in function signature to bind "t" inside a function)
[case testClassTvarScope]
from typing import Generic, TypeVar
t = TypeVar('t')
class c(Generic[t]): pass
x = 0 # type: t
[out]
main:5: error: Type variable "__main__.t" is unbound
main:5: note: (Hint: Use "Generic[t]" or "Protocol[t]" base class to bind "t" inside a class)
main:5: note: (Hint: Use "t" in function signature to bind "t" inside a function)
[case testExpressionRefersToTypeVariable]
from typing import TypeVar, Generic
t = TypeVar('t')
class c(Generic[t]):
def f(self) -> None: x = t
def f(y: t): x = t
[out]
main:4: error: "t" is a type variable and only valid in type context
main:5: error: "t" is a type variable and only valid in type context
[case testMissingSelf]
import typing
class A:
def f(): pass
[out]
main:3: error: Method must have at least one argument. Did you forget the "self" argument?
[case testInvalidBaseClass]
import typing
class A(B): pass
[out]
main:2: error: Name "B" is not defined
[case testSuperOutsideClass]
class A: pass
super().x
def f() -> None: super().y
[out]
main:2: error: "super" used outside class
main:3: error: "super" used outside class
[case testMissingSelfInMethod]
import typing
class A:
def f() -> None: pass
def g(): pass
[out]
main:3: error: Method must have at least one argument. Did you forget the "self" argument?
main:4: error: Method must have at least one argument. Did you forget the "self" argument?
[case testMultipleMethodDefinition]
import typing
class A:
def f(self) -> None: pass
def g(self) -> None: pass
def f(self, x: object) -> None: pass
[out]
main:5: error: Name "f" already defined on line 3
[case testInvalidGlobalDecl]
import typing
def f() -> None:
global x
x = None
[out]
main:4: error: Name "x" is not defined
[case testInvalidNonlocalDecl]
import typing
def f():
def g() -> None:
nonlocal x
x = None
[out]
main:4: error: No binding for nonlocal "x" found
main:5: error: Name "x" is not defined
[case testNonlocalDeclNotMatchingGlobal]
import typing
x = None
def f() -> None:
nonlocal x
x = None
[out]
main:4: error: No binding for nonlocal "x" found
main:5: error: Name "x" is not defined
[case testNonlocalDeclConflictingWithParameter]
import typing
def g():
x = None
def f(x) -> None:
nonlocal x
x = None
[out]
main:5: error: Name "x" is already defined in local scope before nonlocal declaration
[case testNonlocalDeclOutsideFunction]
x = 2
nonlocal x
[out]
main:2: error: nonlocal declaration not allowed at module level
[case testGlobalAndNonlocalDecl]
import typing
x = 1
def f():
x = 1
def g() -> None:
global x
nonlocal x
x = None
[out]
main:7: error: Name "x" is nonlocal and global
[case testNonlocalAndGlobalDecl]
import typing
x = 1
def f():
x = 1
def g() -> None:
nonlocal x
global x
x = None
[out]
main:7: error: Name "x" is nonlocal and global
[case testNestedFunctionAndScoping]
import typing
def f(x) -> None:
def g(y):
z = x
z
y
x
[out]
main:5: error: Name "z" is not defined
main:6: error: Name "y" is not defined
[case testMultipleNestedFunctionDef]
import typing
def f(x) -> None:
def g(): pass
x = 1
def g(): pass
[out]
main:5: error: Name "g" already defined on line 3
[case testRedefinedOverloadedFunction]
from typing import overload, Any
def f() -> None:
@overload
def p(o: object) -> None: pass # no error
@overload
def p(o: Any) -> None: pass # no error
x = 1
def p(): pass # fail
[out]
main:3: error: An overloaded function outside a stub file must have an implementation
main:8: error: Name "p" already defined on line 3
[case testNestedFunctionInMethod]
import typing
class A:
def f(self) -> None:
def g() -> None:
x
y
[out]
main:5: error: Name "x" is not defined
main:6: error: Name "y" is not defined
[case testImportScope]
import typing
def f() -> None:
import x
x.y # E: Name "x" is not defined
[file x.py]
y = 1
[out]
[case testImportScope2]
import typing
def f() -> None:
from x import y
y
y # E: Name "y" is not defined
[file x.py]
y = 1
[out]
[case testImportScope3]
import typing
def f() -> None:
from x import *
y
y # E: Name "y" is not defined
[file x.py]
y = 1
[out]
[case testImportScope4]
import typing
class A:
from x import *
y
y # E: Name "y" is not defined
[file x.py]
y = 1
[out]
[case testScopeOfNestedClass]
import typing
def f():
class A: pass
A
A # E: Name "A" is not defined
[out]
[case testScopeOfNestedClass2]
import typing
class A:
class B: pass
B # E: Name "B" is not defined
[out]
[case testScopeOfNestedClass3]
import typing
class A:
def f(self):
class B: pass
B # E: Name "B" is not defined
B # E: Name "B" is not defined
[out]
[case testInvalidNestedClassReferenceInDecl]
import typing
class A: pass
foo = 0 # type: A.x # E: Name "A.x" is not defined
[out]
[case testTvarScopingWithNestedClass]
from typing import TypeVar, Generic
t = TypeVar('t')
s = TypeVar('s')
class A(Generic[t]):
class B(Generic[s]):
x = 0 # type: A[s]
y = 0 # type: A[t] # E: Type variable "__main__.t" is unbound \
# N: (Hint: Use "Generic[t]" or "Protocol[t]" base class to bind "t" inside a class) \
# N: (Hint: Use "t" in function signature to bind "t" inside a function)
z = 0 # type: A[s] # E: Type variable "__main__.s" is unbound \
# N: (Hint: Use "Generic[s]" or "Protocol[s]" base class to bind "s" inside a class) \
# N: (Hint: Use "s" in function signature to bind "s" inside a function)
a = 0 # type: A[t]
[out]
[case testTestExtendPrimitives]
# Extending bool is not checked here as it should be typed
# as final meaning the type checker will detect it.
class C(bool): pass # ok
class A(int): pass # ok
class B(float): pass # ok
class D(str): pass # ok
[builtins fixtures/primitives.pyi]
[out]
[case testCyclicInheritance1]
class A(A): pass # E: Cannot resolve name "A" (possible cyclic definition)
[out]
[case testCyclicInheritance2]
class A(B): pass # E: Cannot resolve name "B" (possible cyclic definition)
class B(A): pass
[out]
[case testAssignToTypeDef]
import typing
class A: pass
A = None # E: Cannot assign to a type
[out]
[case testInvalidCastTargetSyntax]
from typing import cast, TypeVar, Generic
t = TypeVar('t')
class C(Generic[t]): pass
cast(str + str, None) # E: Cast target is not a type
cast(C[str][str], None) # E: Cast target is not a type
cast(C[str + str], None) # E: Cast target is not a type
cast([int], None) # E: Bracketed expression "[...]" is not valid as a type \
# N: Did you mean "List[...]"?
[out]
[case testInvalidCastTargetType]
from typing import cast
x = 0
cast(x, None) # E: Variable "__main__.x" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
cast(t, None) # E: Name "t" is not defined
cast(__builtins__.x, None) # E: Name "__builtins__.x" is not defined
[out]
[case testInvalidCastTargetType2]
from typing import cast
x = 0
cast(str[str], None) # E: "str" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfArgsToCast]
from typing import cast
cast(str) # E: "cast" expects 2 arguments
cast(str, None, None) # E: "cast" expects 2 arguments
[out]
[case testInvalidKindsOfArgsToCast]
from typing import cast
cast(str, *None) # E: "cast" must be called with 2 positional arguments
cast(str, target=None) # E: "cast" must be called with 2 positional arguments
[out]
[case testInvalidAssertType]
from typing import assert_type
assert_type(1, type=int) # E: "assert_type" must be called with 2 positional arguments
assert_type(1, *int) # E: "assert_type" must be called with 2 positional arguments
assert_type() # E: "assert_type" expects 2 arguments
assert_type(1, int, "hello") # E: "assert_type" expects 2 arguments
assert_type(int, 1) # E: Invalid type: try using Literal[1] instead?
assert_type(1, int[int]) # E: "int" expects no type arguments, but 1 given
[case testInvalidAnyCall]
from typing import Any
Any(str, None) # E: Any(...) is no longer supported. Use cast(Any, ...) instead
Any(arg=str) # E: Any(...) is no longer supported. Use cast(Any, ...) instead
[out]
[case testTypeListAsType]
def f(x: [int]) -> None: # E: Bracketed expression "[...]" is not valid as a type \
# N: Did you mean "List[...]"?
pass
[out]
[case testInvalidFunctionType]
from typing import Callable
x = None # type: Callable[int, str]
y = None # type: Callable[int]
z = None # type: Callable[int, int, int]
[out]
main:2: error: The first argument to Callable must be a list of types, parameter specification, or "..."
main:2: note: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas
main:3: error: Please use "Callable[[<parameters>], <return type>]" or "Callable"
main:4: error: Please use "Callable[[<parameters>], <return type>]" or "Callable"
[case testAbstractGlobalFunction]
import typing
from abc import abstractmethod
@abstractmethod
def foo(): pass
[out]
main:3: error: "abstractmethod" used with a non-method
[case testAbstractNestedFunction]
import typing
from abc import abstractmethod
def g() -> None:
@abstractmethod
def foo(): pass
[out]
main:4: error: "abstractmethod" used with a non-method
[case testInvalidTypeDeclaration]
import typing
def f(): pass
f() = 1 # type: int
[out]
main:3: error: cannot assign to function call
[out version>=3.10]
main:3: error: cannot assign to function call here. Maybe you meant '==' instead of '='?
[case testIndexedAssignmentWithTypeDeclaration]
import typing
None[1] = 1 # type: int
[out]
main:2: error: Unexpected type declaration
[case testNonSelfMemberAssignmentWithTypeDeclaration]
import typing
None.x = 1 # type: int
[out]
main:2: error: Type cannot be declared in assignment to non-self attribute
[case testNonSelfMemberAssignmentWithTypeDeclarationInMethod]
import typing
class A:
def f(self, x) -> None:
x.y = 1 # type: int
[out]
main:4: error: Type cannot be declared in assignment to non-self attribute
[case testInvalidTypeInTypeApplication]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(Generic[t]): pass
A[TypeVar] # E: Variable "typing.TypeVar" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[out]
[case testInvalidTypeInTypeApplication2]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(Generic[t]): pass
A[1] # E: Invalid type: try using Literal[1] instead?
[out]
[case testVariableDeclWithInvalidNumberOfTypes]
x, y = 1, 2 # type: int, str, int # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested]
x, (y, z) = 1, (2, 3) # type: int, (str, int, int) # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested2]
x, (y, z) = 1, (2, 3) # type: int, (str, ) # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested3]
x, (y, z) = 1, (2, 3) # type: int, str # E: Tuple type expected for multiple variables
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested4]
x, (y, z) = 1, (2, 3) # type: int, str, int # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested5]
x, (y, ) = 1, (2, ) # type: int, str # E: Tuple type expected for multiple variables
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidType]
x, y = 1, 2 # type: int # E: Tuple type expected for multiple variables
[out]
[case testInvalidLvalueWithExplicitType]
a = 1
a() = None # type: int
[out]
main:2: error: cannot assign to function call
[out version>=3.10]
main:2: error: cannot assign to function call here. Maybe you meant '==' instead of '='?
[case testInvalidLvalueWithExplicitType2]
a = 1
a[1] = None # type: int # E: Unexpected type declaration
a.x = None # type: int \
# E: Type cannot be declared in assignment to non-self attribute
[out]
[case testInvalidLvalueWithExplicitType3]
a = 1
a.y, a.x = None, None # type: int, int \
# E: Type cannot be declared in assignment to non-self attribute
a[1], a[2] = None, None # type: int, int \
# E: Unexpected type declaration
[builtins fixtures/tuple.pyi]
[out]
[case testMissingGenericImport]
from typing import TypeVar
T = TypeVar('T')