1.. _error-codes:
2
3Error codes
4===========
5
6Mypy can optionally display an error code such as ``[attr-defined]``
7after each error message. Error codes serve two purposes:
8
91. It's possible to silence specific error codes on a line using ``#
10   type: ignore[code]``. This way you won't accidentally ignore other,
11   potentially more serious errors.
12
132. The error code can be used to find documentation about the error.
14   The next two topics (:ref:`error-code-list` and
15   :ref:`error-codes-optional`) document the various error codes
16   mypy can report.
17
18Most error codes are shared between multiple related error messages.
19Error codes may change in future mypy releases.
20
21
22
23Displaying error codes
24----------------------
25
26Error codes are not displayed by default.  Use :option:`--show-error-codes <mypy --show-error-codes>`
27or config `show_error_codes = True` to display error codes. Error codes are shown inside square brackets:
28
29.. code-block:: text
30
31   $ mypy --show-error-codes prog.py
32   prog.py:1: error: "str" has no attribute "trim"  [attr-defined]
33
34Silencing errors based on error codes
35-------------------------------------
36
37You can use a special comment ``# type: ignore[code, ...]`` to only
38ignore errors with a specific error code (or codes) on a particular
39line.  This can be used even if you have not configured mypy to show
40error codes. Currently it's only possible to disable arbitrary error
41codes on individual lines using this comment.
42
43.. note::
44
45  There are command-line flags and config file settings for enabling
46  certain optional error codes, such as :option:`--disallow-untyped-defs <mypy --disallow-untyped-defs>`,
47  which enables the ``no-untyped-def`` error code.
48
49This example shows how to ignore an error about an imported name mypy
50thinks is undefined:
51
52.. code-block:: python
53
54   # 'foo' is defined in 'foolib', even though mypy can't see the
55   # definition.
56   from foolib import foo  # type: ignore[attr-defined]
57