Skip to content

Commit 9b327d1

Browse files
ilevkivskyihauntsaninja
authored andcommitted
Use consistent anchors for error codes (#15435)
Fixes #15431 After this PR one will be able to easily find documentation for given error code using `https://mypy.readthedocs.io/en/stable/_refs.html#code-{code as reported by mypy}`, for example `https://mypy.readthedocs.io/en/stable/_refs.html#code-attr-defined`.
1 parent 32abe02 commit 9b327d1

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

docs/source/error_code_list.rst

+76
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ with default options. See :ref:`error-codes` for general documentation
88
about error codes. :ref:`error-codes-optional` documents additional
99
error codes that you can enable.
1010

11+
.. _code-attr-defined:
12+
1113
Check that attribute exists [attr-defined]
1214
------------------------------------------
1315

@@ -43,6 +45,8 @@ A reference to a missing attribute is given the ``Any`` type. In the
4345
above example, the type of ``non_existent`` will be ``Any``, which can
4446
be important if you silence the error.
4547

48+
.. _code-union-attr:
49+
4650
Check that attribute exists in each union item [union-attr]
4751
-----------------------------------------------------------
4852

@@ -75,6 +79,8 @@ You can often work around these errors by using ``assert isinstance(obj, ClassNa
7579
or ``assert obj is not None`` to tell mypy that you know that the type is more specific
7680
than what mypy thinks.
7781

82+
.. _code-name-defined:
83+
7884
Check that name is defined [name-defined]
7985
-----------------------------------------
8086

@@ -89,6 +95,7 @@ This example accidentally calls ``sort()`` instead of :py:func:`sorted`:
8995
9096
x = sort([3, 2, 4]) # Error: Name "sort" is not defined [name-defined]
9197
98+
.. _code-used-before-def:
9299

93100
Check that a variable is not used before it's defined [used-before-def]
94101
-----------------------------------------------------------------------
@@ -105,6 +112,7 @@ Example:
105112
print(x) # Error: Name "x" is used before definition [used-before-def]
106113
x = 123
107114
115+
.. _code-call-arg:
108116

109117
Check arguments in calls [call-arg]
110118
-----------------------------------
@@ -124,6 +132,8 @@ Example:
124132
greet('jack') # OK
125133
greet('jill', 'jack') # Error: Too many arguments for "greet" [call-arg]
126134
135+
.. _code-arg-type:
136+
127137
Check argument types [arg-type]
128138
-------------------------------
129139

@@ -144,6 +154,8 @@ Example:
144154
# expected "list[int]" [arg-type]
145155
print(first(t))
146156
157+
.. _code-call-overload:
158+
147159
Check calls to overloaded functions [call-overload]
148160
---------------------------------------------------
149161

@@ -175,6 +187,8 @@ Example:
175187
# Error: No overload variant of "inc_maybe" matches argument type "float" [call-overload]
176188
inc_maybe(1.2)
177189
190+
.. _code-valid-type:
191+
178192
Check validity of types [valid-type]
179193
------------------------------------
180194

@@ -207,6 +221,8 @@ You can use :py:data:`~typing.Callable` as the type for callable objects:
207221
for x in objs:
208222
f(x)
209223
224+
.. _code-var-annotated:
225+
210226
Require annotation if variable type is unclear [var-annotated]
211227
--------------------------------------------------------------
212228

@@ -239,6 +255,8 @@ To address this, we add an explicit annotation:
239255
240256
reveal_type(Bundle().items) # list[str]
241257
258+
.. _code-override:
259+
242260
Check validity of overrides [override]
243261
--------------------------------------
244262

@@ -275,6 +293,8 @@ Example:
275293
arg: bool) -> int:
276294
...
277295
296+
.. _code-return:
297+
278298
Check that function returns a value [return]
279299
--------------------------------------------
280300

@@ -303,6 +323,8 @@ Example:
303323
else:
304324
raise ValueError('not defined for zero')
305325
326+
.. _code-return-value:
327+
306328
Check that return value is compatible [return-value]
307329
----------------------------------------------------
308330

@@ -317,6 +339,8 @@ Example:
317339
# Error: Incompatible return value type (got "int", expected "str") [return-value]
318340
return x + 1
319341
342+
.. _code-assignment:
343+
320344
Check types in assignment statement [assignment]
321345
------------------------------------------------
322346

@@ -339,6 +363,8 @@ Example:
339363
# variable has type "str") [assignment]
340364
r.name = 5
341365
366+
.. _code-method-assign:
367+
342368
Check that assignment target is not a method [method-assign]
343369
------------------------------------------------------------
344370

@@ -368,6 +394,8 @@ so only the second assignment will still generate an error.
368394

369395
This error code is a subcode of the more general ``[assignment]`` code.
370396

397+
.. _code-type-var:
398+
371399
Check type variable values [type-var]
372400
-------------------------------------
373401

@@ -390,6 +418,8 @@ Example:
390418
# Error: Value of type variable "T1" of "add" cannot be "str" [type-var]
391419
add('x', 'y')
392420
421+
.. _code-operator:
422+
393423
Check uses of various operators [operator]
394424
------------------------------------------
395425

@@ -404,6 +434,8 @@ Example:
404434
# Error: Unsupported operand types for + ("int" and "str") [operator]
405435
1 + 'x'
406436
437+
.. _code-index:
438+
407439
Check indexing operations [index]
408440
---------------------------------
409441

@@ -425,6 +457,8 @@ Example:
425457
# Error: Invalid index type "bytes" for "dict[str, int]"; expected type "str" [index]
426458
a[b'x'] = 4
427459
460+
.. _code-list-item:
461+
428462
Check list items [list-item]
429463
----------------------------
430464

@@ -439,6 +473,8 @@ Example:
439473
# Error: List item 0 has incompatible type "int"; expected "str" [list-item]
440474
a: list[str] = [0]
441475
476+
.. _code-dict-item:
477+
442478
Check dict items [dict-item]
443479
----------------------------
444480

@@ -453,6 +489,8 @@ Example:
453489
# Error: Dict entry 0 has incompatible type "str": "str"; expected "str": "int" [dict-item]
454490
d: dict[str, int] = {'key': 'value'}
455491
492+
.. _code-typeddict-item:
493+
456494
Check TypedDict items [typeddict-item]
457495
--------------------------------------
458496

@@ -477,6 +515,8 @@ Example:
477515
# TypedDict item "x" has type "int") [typeddict-item]
478516
p: Point = {'x': 1.2, 'y': 4}
479517
518+
.. _code-typeddict-unknown-key:
519+
480520
Check TypedDict Keys [typeddict-unknown-key]
481521
--------------------------------------------
482522

@@ -533,6 +573,8 @@ runtime:
533573

534574
This error code is a subcode of the wider ``[typeddict-item]`` code.
535575

576+
.. _code-has-type:
577+
536578
Check that type of target is known [has-type]
537579
---------------------------------------------
538580

@@ -572,6 +614,8 @@ the issue:
572614
def set_y(self) -> None:
573615
self.y: int = self.x # Added annotation here
574616
617+
.. _code-import:
618+
575619
Check that import target can be found [import]
576620
----------------------------------------------
577621

@@ -587,6 +631,8 @@ Example:
587631
588632
See :ref:`ignore-missing-imports` for how to work around these errors.
589633

634+
.. _code-no-redef:
635+
590636
Check that each name is defined once [no-redef]
591637
-----------------------------------------------
592638

@@ -613,6 +659,8 @@ Example:
613659
# (the first definition wins!)
614660
A('x')
615661
662+
.. _code-func-returns-value:
663+
616664
Check that called function returns a value [func-returns-value]
617665
---------------------------------------------------------------
618666

@@ -635,6 +683,8 @@ returns ``None``:
635683
if f():
636684
print("not false")
637685
686+
.. _code-abstract:
687+
638688
Check instantiation of abstract classes [abstract]
639689
--------------------------------------------------
640690

@@ -666,6 +716,8 @@ Example:
666716
# Error: Cannot instantiate abstract class "Thing" with abstract attribute "save" [abstract]
667717
t = Thing()
668718
719+
.. _code-type-abstract:
720+
669721
Safe handling of abstract type object types [type-abstract]
670722
-----------------------------------------------------------
671723

@@ -692,6 +744,8 @@ Example:
692744
# Error: Only concrete class can be given where "Type[Config]" is expected [type-abstract]
693745
make_many(Config, 5)
694746
747+
.. _code-safe-super:
748+
695749
Check that call to an abstract method via super is valid [safe-super]
696750
---------------------------------------------------------------------
697751

@@ -714,6 +768,8 @@ will cause runtime errors, so mypy prevents you from doing so:
714768
Mypy considers the following as trivial bodies: a ``pass`` statement, a literal
715769
ellipsis ``...``, a docstring, and a ``raise NotImplementedError`` statement.
716770

771+
.. _code-valid-newtype:
772+
717773
Check the target of NewType [valid-newtype]
718774
-------------------------------------------
719775

@@ -738,6 +794,8 @@ To work around the issue, you can either give mypy access to the sources
738794
for ``acme`` or create a stub file for the module. See :ref:`ignore-missing-imports`
739795
for more information.
740796

797+
.. _code-exit-return:
798+
741799
Check the return type of __exit__ [exit-return]
742800
-----------------------------------------------
743801

@@ -794,6 +852,8 @@ You can also use ``None``:
794852
def __exit__(self, exc, value, tb) -> None: # Also OK
795853
print('exit')
796854
855+
.. _code-name-match:
856+
797857
Check that naming is consistent [name-match]
798858
--------------------------------------------
799859

@@ -807,6 +867,8 @@ consistently when using the call-based syntax. Example:
807867
# Error: First argument to namedtuple() should be "Point2D", not "Point"
808868
Point2D = NamedTuple("Point", [("x", int), ("y", int)])
809869
870+
.. _code-literal-required:
871+
810872
Check that literal is used where expected [literal-required]
811873
------------------------------------------------------------
812874

@@ -836,6 +898,8 @@ or ``Literal`` variables. Example:
836898
# expected one of ("x", "y") [literal-required]
837899
p[key]
838900
901+
.. _code-no-overload-impl:
902+
839903
Check that overloaded functions have an implementation [no-overload-impl]
840904
-------------------------------------------------------------------------
841905

@@ -858,6 +922,8 @@ implementation.
858922
def func(value):
859923
pass # actual implementation
860924
925+
.. _code-unused-coroutine:
926+
861927
Check that coroutine return value is used [unused-coroutine]
862928
------------------------------------------------------------
863929

@@ -881,6 +947,8 @@ otherwise unused variable:
881947
882948
_ = f() # No error
883949
950+
.. _code-assert-type:
951+
884952
Check types in assert_type [assert-type]
885953
----------------------------------------
886954

@@ -895,6 +963,8 @@ the provided type.
895963
896964
assert_type([1], list[str]) # Error
897965
966+
.. _code-truthy-function:
967+
898968
Check that function isn't used in boolean context [truthy-function]
899969
-------------------------------------------------------------------
900970

@@ -908,6 +978,8 @@ Functions will always evaluate to true in boolean contexts.
908978
if f: # Error: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
909979
pass
910980
981+
.. _code-str-bytes-safe:
982+
911983
Check for implicit bytes coercions [str-bytes-safe]
912984
-------------------------------------------------------------------
913985

@@ -926,13 +998,17 @@ Warn about cases where a bytes object may be converted to a string in an unexpec
926998
print(f"The alphabet starts with {b!r}") # The alphabet starts with b'abc'
927999
print(f"The alphabet starts with {b.decode('utf-8')}") # The alphabet starts with abc
9281000
1001+
.. _code-syntax:
1002+
9291003
Report syntax errors [syntax]
9301004
-----------------------------
9311005

9321006
If the code being checked is not syntactically valid, mypy issues a
9331007
syntax error. Most, but not all, syntax errors are *blocking errors*:
9341008
they can't be ignored with a ``# type: ignore`` comment.
9351009

1010+
.. _code-misc:
1011+
9361012
Miscellaneous checks [misc]
9371013
---------------------------
9381014

0 commit comments

Comments
 (0)