Debian Codegraph @

docs/source/error_codes.rst

raw
1 .. _error-codes:
2
3 Error codes
4 ===========
5
6 Mypy can optionally display an error code such as ``[attr-defined]``
7 after each error message. Error codes serve two purposes:
8
9 1. 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
13 2. 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
18 Most error codes are shared between multiple related error messages.
19 Error codes may change in future mypy releases.
20
21
22 .. _silence-error-codes:
23
24 Silencing errors based on error codes
25 -------------------------------------
26
27 You can use a special comment ``# type: ignore[code, ...]`` to only
28 ignore errors with a specific error code (or codes) on a particular
29 line. This can be used even if you have not configured mypy to show
30 error codes.
31
32 This example shows how to ignore an error about an imported name mypy
33 thinks is undefined:
34
35 .. code-block:: python
36
37 # 'foo' is defined in 'foolib', even though mypy can't see the
38 # definition.
39 from foolib import foo # type: ignore[attr-defined]
40
41 Enabling/disabling specific error codes globally
42 ------------------------------------------------
43
44 There are command-line flags and config file settings for enabling
45 certain optional error codes, such as :option:`--disallow-untyped-defs <mypy --disallow-untyped-defs>`,
46 which enables the ``no-untyped-def`` error code.
47
48 You can use :option:`--enable-error-code <mypy --enable-error-code>`
49 and :option:`--disable-error-code <mypy --disable-error-code>`
50 to enable or disable specific error codes that don't have a dedicated
51 command-line flag or config file setting.
52
53 Per-module enabling/disabling error codes
54 -----------------------------------------
55
56 You can use :ref:`configuration file <config-file>` sections to enable or
57 disable specific error codes only in some modules. For example, this ``mypy.ini``
58 config will enable non-annotated empty containers in tests, while keeping
59 other parts of code checked in strict mode:
60
61 .. code-block:: ini
62
63 [mypy]
64 strict = True
65
66 [mypy-tests.*]
67 allow_untyped_defs = True
68 allow_untyped_calls = True
69 disable_error_code = var-annotated, has-type
70
71 Note that per-module enabling/disabling acts as override over the global
72 options. So that you don't need to repeat the error code lists for each
73 module if you have them in global config section. For example:
74
75 .. code-block:: ini
76
77 [mypy]
78 enable_error_code = truthy-bool, ignore-without-code, unused-awaitable
79
80 [mypy-extensions.*]
81 disable_error_code = unused-awaitable
82
83 The above config will allow unused awaitables in extension modules, but will
84 still keep the other two error codes enabled. The overall logic is following:
85
86 * Command line and/or config main section set global error codes
87
88 * Individual config sections *adjust* them per glob/module
89
90 * Inline ``# mypy: disable-error-code="..."`` and ``# mypy: enable-error-code="..."``
91 comments can further *adjust* them for a specific file.
92 For example:
93
94 .. code-block:: python
95
96 # mypy: enable-error-code="truthy-bool, ignore-without-code"
97
98 So one can e.g. enable some code globally, disable it for all tests in
99 the corresponding config section, and then re-enable it with an inline
100 comment in some specific test.
101
102 Subcodes of error codes
103 -----------------------
104
105 In some cases, mostly for backwards compatibility reasons, an error
106 code may be covered also by another, wider error code. For example, an error with
107 code ``[method-assign]`` can be ignored by ``# type: ignore[assignment]``.
108 Similar logic works for disabling error codes globally. If a given error code
109 is a subcode of another one, it will be mentioned in the documentation for the narrower
110 code. This hierarchy is not nested: there cannot be subcodes of other
111 subcodes.
112
113
114 Requiring error codes
115 ---------------------
116
117 It's possible to require error codes be specified in ``type: ignore`` comments.
118 See :ref:`ignore-without-code<code-ignore-without-code>` for more information.