@@ -8,6 +8,8 @@ with default options. See :ref:`error-codes` for general documentation
8
8
about error codes. :ref: `error-codes-optional ` documents additional
9
9
error codes that you can enable.
10
10
11
+ .. _code-attr-defined :
12
+
11
13
Check that attribute exists [attr-defined]
12
14
------------------------------------------
13
15
@@ -43,6 +45,8 @@ A reference to a missing attribute is given the ``Any`` type. In the
43
45
above example, the type of ``non_existent `` will be ``Any ``, which can
44
46
be important if you silence the error.
45
47
48
+ .. _code-union-attr :
49
+
46
50
Check that attribute exists in each union item [union-attr]
47
51
-----------------------------------------------------------
48
52
@@ -75,6 +79,8 @@ You can often work around these errors by using ``assert isinstance(obj, ClassNa
75
79
or ``assert obj is not None `` to tell mypy that you know that the type is more specific
76
80
than what mypy thinks.
77
81
82
+ .. _code-name-defined :
83
+
78
84
Check that name is defined [name-defined]
79
85
-----------------------------------------
80
86
@@ -89,6 +95,7 @@ This example accidentally calls ``sort()`` instead of :py:func:`sorted`:
89
95
90
96
x = sort([3 , 2 , 4 ]) # Error: Name "sort" is not defined [name-defined]
91
97
98
+ .. _code-used-before-def :
92
99
93
100
Check that a variable is not used before it's defined [used-before-def]
94
101
-----------------------------------------------------------------------
@@ -105,6 +112,7 @@ Example:
105
112
print (x) # Error: Name "x" is used before definition [used-before-def]
106
113
x = 123
107
114
115
+ .. _code-call-arg :
108
116
109
117
Check arguments in calls [call-arg]
110
118
-----------------------------------
@@ -124,6 +132,8 @@ Example:
124
132
greet(' jack' ) # OK
125
133
greet(' jill' , ' jack' ) # Error: Too many arguments for "greet" [call-arg]
126
134
135
+ .. _code-arg-type :
136
+
127
137
Check argument types [arg-type]
128
138
-------------------------------
129
139
@@ -144,6 +154,8 @@ Example:
144
154
# expected "list[int]" [arg-type]
145
155
print (first(t))
146
156
157
+ .. _code-call-overload :
158
+
147
159
Check calls to overloaded functions [call-overload]
148
160
---------------------------------------------------
149
161
@@ -175,6 +187,8 @@ Example:
175
187
# Error: No overload variant of "inc_maybe" matches argument type "float" [call-overload]
176
188
inc_maybe(1.2 )
177
189
190
+ .. _code-valid-type :
191
+
178
192
Check validity of types [valid-type]
179
193
------------------------------------
180
194
@@ -207,6 +221,8 @@ You can use :py:data:`~typing.Callable` as the type for callable objects:
207
221
for x in objs:
208
222
f(x)
209
223
224
+ .. _code-var-annotated :
225
+
210
226
Require annotation if variable type is unclear [var-annotated]
211
227
--------------------------------------------------------------
212
228
@@ -239,6 +255,8 @@ To address this, we add an explicit annotation:
239
255
240
256
reveal_type(Bundle().items) # list[str]
241
257
258
+ .. _code-override :
259
+
242
260
Check validity of overrides [override]
243
261
--------------------------------------
244
262
@@ -275,6 +293,8 @@ Example:
275
293
arg : bool ) -> int :
276
294
...
277
295
296
+ .. _code-return :
297
+
278
298
Check that function returns a value [return]
279
299
--------------------------------------------
280
300
@@ -303,6 +323,8 @@ Example:
303
323
else :
304
324
raise ValueError (' not defined for zero' )
305
325
326
+ .. _code-return-value :
327
+
306
328
Check that return value is compatible [return-value]
307
329
----------------------------------------------------
308
330
@@ -317,6 +339,8 @@ Example:
317
339
# Error: Incompatible return value type (got "int", expected "str") [return-value]
318
340
return x + 1
319
341
342
+ .. _code-assignment :
343
+
320
344
Check types in assignment statement [assignment]
321
345
------------------------------------------------
322
346
@@ -339,6 +363,8 @@ Example:
339
363
# variable has type "str") [assignment]
340
364
r.name = 5
341
365
366
+ .. _code-method-assign :
367
+
342
368
Check that assignment target is not a method [method-assign]
343
369
------------------------------------------------------------
344
370
@@ -368,6 +394,8 @@ so only the second assignment will still generate an error.
368
394
369
395
This error code is a subcode of the more general ``[assignment] `` code.
370
396
397
+ .. _code-type-var :
398
+
371
399
Check type variable values [type-var]
372
400
-------------------------------------
373
401
@@ -390,6 +418,8 @@ Example:
390
418
# Error: Value of type variable "T1" of "add" cannot be "str" [type-var]
391
419
add(' x' , ' y' )
392
420
421
+ .. _code-operator :
422
+
393
423
Check uses of various operators [operator]
394
424
------------------------------------------
395
425
@@ -404,6 +434,8 @@ Example:
404
434
# Error: Unsupported operand types for + ("int" and "str") [operator]
405
435
1 + ' x'
406
436
437
+ .. _code-index :
438
+
407
439
Check indexing operations [index]
408
440
---------------------------------
409
441
@@ -425,6 +457,8 @@ Example:
425
457
# Error: Invalid index type "bytes" for "dict[str, int]"; expected type "str" [index]
426
458
a[b ' x' ] = 4
427
459
460
+ .. _code-list-item :
461
+
428
462
Check list items [list-item]
429
463
----------------------------
430
464
@@ -439,6 +473,8 @@ Example:
439
473
# Error: List item 0 has incompatible type "int"; expected "str" [list-item]
440
474
a: list[str ] = [0 ]
441
475
476
+ .. _code-dict-item :
477
+
442
478
Check dict items [dict-item]
443
479
----------------------------
444
480
@@ -453,6 +489,8 @@ Example:
453
489
# Error: Dict entry 0 has incompatible type "str": "str"; expected "str": "int" [dict-item]
454
490
d: dict[str , int ] = {' key' : ' value' }
455
491
492
+ .. _code-typeddict-item :
493
+
456
494
Check TypedDict items [typeddict-item]
457
495
--------------------------------------
458
496
@@ -477,6 +515,8 @@ Example:
477
515
# TypedDict item "x" has type "int") [typeddict-item]
478
516
p: Point = {' x' : 1.2 , ' y' : 4 }
479
517
518
+ .. _code-typeddict-unknown-key :
519
+
480
520
Check TypedDict Keys [typeddict-unknown-key]
481
521
--------------------------------------------
482
522
@@ -533,6 +573,8 @@ runtime:
533
573
534
574
This error code is a subcode of the wider ``[typeddict-item] `` code.
535
575
576
+ .. _code-has-type :
577
+
536
578
Check that type of target is known [has-type]
537
579
---------------------------------------------
538
580
@@ -572,6 +614,8 @@ the issue:
572
614
def set_y (self ) -> None :
573
615
self .y: int = self .x # Added annotation here
574
616
617
+ .. _code-import :
618
+
575
619
Check that import target can be found [import]
576
620
----------------------------------------------
577
621
@@ -587,6 +631,8 @@ Example:
587
631
588
632
See :ref: `ignore-missing-imports ` for how to work around these errors.
589
633
634
+ .. _code-no-redef :
635
+
590
636
Check that each name is defined once [no-redef]
591
637
-----------------------------------------------
592
638
@@ -613,6 +659,8 @@ Example:
613
659
# (the first definition wins!)
614
660
A(' x' )
615
661
662
+ .. _code-func-returns-value :
663
+
616
664
Check that called function returns a value [func-returns-value]
617
665
---------------------------------------------------------------
618
666
@@ -635,6 +683,8 @@ returns ``None``:
635
683
if f():
636
684
print (" not false" )
637
685
686
+ .. _code-abstract :
687
+
638
688
Check instantiation of abstract classes [abstract]
639
689
--------------------------------------------------
640
690
@@ -666,6 +716,8 @@ Example:
666
716
# Error: Cannot instantiate abstract class "Thing" with abstract attribute "save" [abstract]
667
717
t = Thing()
668
718
719
+ .. _code-type-abstract :
720
+
669
721
Safe handling of abstract type object types [type-abstract]
670
722
-----------------------------------------------------------
671
723
@@ -692,6 +744,8 @@ Example:
692
744
# Error: Only concrete class can be given where "Type[Config]" is expected [type-abstract]
693
745
make_many(Config, 5 )
694
746
747
+ .. _code-safe-super :
748
+
695
749
Check that call to an abstract method via super is valid [safe-super]
696
750
---------------------------------------------------------------------
697
751
@@ -714,6 +768,8 @@ will cause runtime errors, so mypy prevents you from doing so:
714
768
Mypy considers the following as trivial bodies: a ``pass `` statement, a literal
715
769
ellipsis ``... ``, a docstring, and a ``raise NotImplementedError `` statement.
716
770
771
+ .. _code-valid-newtype :
772
+
717
773
Check the target of NewType [valid-newtype]
718
774
-------------------------------------------
719
775
@@ -738,6 +794,8 @@ To work around the issue, you can either give mypy access to the sources
738
794
for ``acme `` or create a stub file for the module. See :ref: `ignore-missing-imports `
739
795
for more information.
740
796
797
+ .. _code-exit-return :
798
+
741
799
Check the return type of __exit__ [exit-return]
742
800
-----------------------------------------------
743
801
@@ -794,6 +852,8 @@ You can also use ``None``:
794
852
def __exit__ (self , exc , value , tb ) -> None : # Also OK
795
853
print (' exit' )
796
854
855
+ .. _code-name-match :
856
+
797
857
Check that naming is consistent [name-match]
798
858
--------------------------------------------
799
859
@@ -807,6 +867,8 @@ consistently when using the call-based syntax. Example:
807
867
# Error: First argument to namedtuple() should be "Point2D", not "Point"
808
868
Point2D = NamedTuple(" Point" , [(" x" , int ), (" y" , int )])
809
869
870
+ .. _code-literal-required :
871
+
810
872
Check that literal is used where expected [literal-required]
811
873
------------------------------------------------------------
812
874
@@ -836,6 +898,8 @@ or ``Literal`` variables. Example:
836
898
# expected one of ("x", "y") [literal-required]
837
899
p[key]
838
900
901
+ .. _code-no-overload-impl :
902
+
839
903
Check that overloaded functions have an implementation [no-overload-impl]
840
904
-------------------------------------------------------------------------
841
905
@@ -858,6 +922,8 @@ implementation.
858
922
def func (value ):
859
923
pass # actual implementation
860
924
925
+ .. _code-unused-coroutine :
926
+
861
927
Check that coroutine return value is used [unused-coroutine]
862
928
------------------------------------------------------------
863
929
@@ -881,6 +947,8 @@ otherwise unused variable:
881
947
882
948
_ = f() # No error
883
949
950
+ .. _code-assert-type :
951
+
884
952
Check types in assert_type [assert-type]
885
953
----------------------------------------
886
954
@@ -895,6 +963,8 @@ the provided type.
895
963
896
964
assert_type([1 ], list[str ]) # Error
897
965
966
+ .. _code-truthy-function :
967
+
898
968
Check that function isn't used in boolean context [truthy-function]
899
969
-------------------------------------------------------------------
900
970
@@ -908,6 +978,8 @@ Functions will always evaluate to true in boolean contexts.
908
978
if f: # Error: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
909
979
pass
910
980
981
+ .. _code-str-bytes-safe :
982
+
911
983
Check for implicit bytes coercions [str-bytes-safe]
912
984
-------------------------------------------------------------------
913
985
@@ -926,13 +998,17 @@ Warn about cases where a bytes object may be converted to a string in an unexpec
926
998
print (f " The alphabet starts with { b!r } " ) # The alphabet starts with b'abc'
927
999
print (f " The alphabet starts with { b.decode(' utf-8' )} " ) # The alphabet starts with abc
928
1000
1001
+ .. _code-syntax :
1002
+
929
1003
Report syntax errors [syntax]
930
1004
-----------------------------
931
1005
932
1006
If the code being checked is not syntactically valid, mypy issues a
933
1007
syntax error. Most, but not all, syntax errors are *blocking errors *:
934
1008
they can't be ignored with a ``# type: ignore `` comment.
935
1009
1010
+ .. _code-misc :
1011
+
936
1012
Miscellaneous checks [misc]
937
1013
---------------------------
938
1014
0 commit comments