Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1da0ebe

Browse files
authored
Various docs improvements (#16836)
Recommend `--disable-error-code=import-untyped`. It's probably strictly better than `--ignore-missing-imports` for most users. Remove the displaying error codes section, since it's on by default. Some more advice and discoverability for "using mypy with an existing codebase".
1 parent 09490c8 commit 1da0ebe

4 files changed

Lines changed: 36 additions & 30 deletions

File tree

docs/source/config_file.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
The mypy configuration file
44
===========================
55

6+
Mypy is very configurable. This is most useful when introducing typing to
7+
an existing codebase. See :ref:`existing-code` for concrete advice for
8+
that situation.
9+
610
Mypy supports reading configuration settings from a file with the following precedence order:
711

812
1. ``./mypy.ini``

docs/source/error_codes.rst

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,6 @@ Most error codes are shared between multiple related error messages.
1919
Error codes may change in future mypy releases.
2020

2121

22-
23-
Displaying error codes
24-
----------------------
25-
26-
Error codes are displayed by default. Use :option:`--hide-error-codes <mypy --hide-error-codes>`
27-
or config ``hide_error_codes = True`` to hide error codes. Error codes are shown inside square brackets:
28-
29-
.. code-block:: text
30-
31-
$ mypy prog.py
32-
prog.py:1: error: "str" has no attribute "trim" [attr-defined]
33-
34-
It's also possible to require error codes for ``type: ignore`` comments.
35-
See :ref:`ignore-without-code<code-ignore-without-code>` for more information.
36-
37-
3822
.. _silence-error-codes:
3923

4024
Silencing errors based on error codes
@@ -121,3 +105,10 @@ Similar logic works for disabling error codes globally. If a given error code
121105
is a subcode of another one, it will be mentioned in the documentation for the narrower
122106
code. This hierarchy is not nested: there cannot be subcodes of other
123107
subcodes.
108+
109+
110+
Requiring error codes
111+
---------------------
112+
113+
It's possible to require error codes be specified in ``type: ignore`` comments.
114+
See :ref:`ignore-without-code<code-ignore-without-code>` for more information.

docs/source/existing_code.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ invocation to your codebase, or adding your mypy invocation to
3131
existing tools you use to run tests, like ``tox``.
3232

3333
* Make sure everyone runs mypy with the same options. Checking a mypy
34-
:ref:`configuration file <config-file>` into your codebase can help
35-
with this.
34+
:ref:`configuration file <config-file>` into your codebase is the
35+
easiest way to do this.
3636

3737
* Make sure everyone type checks the same set of files. See
3838
:ref:`specifying-code-to-be-checked` for details.
@@ -48,7 +48,7 @@ A simple CI script could look something like this:
4848

4949
.. code-block:: text
5050
51-
python3 -m pip install mypy==0.971
51+
python3 -m pip install mypy==1.8
5252
# Run your standardised mypy invocation, e.g.
5353
mypy my_project
5454
# This could also look like `scripts/run_mypy.sh`, `tox run -e mypy`, `make mypy`, etc
@@ -74,6 +74,11 @@ You could even invert this, by setting ``ignore_errors = True`` in your global
7474
config section and only enabling error reporting with ``ignore_errors = False``
7575
for the set of modules you are ready to type check.
7676

77+
The per-module configuration that mypy's configuration file allows can be
78+
extremely useful. Many configuration options can be enabled or disabled
79+
only for specific modules. In particular, you can also enable or disable
80+
various error codes on a per-module basis, see :ref:`error-codes`.
81+
7782
Fixing errors related to imports
7883
--------------------------------
7984

@@ -89,7 +94,7 @@ that it can't find, that don't have types, or don't have stub files:
8994
Sometimes these can be fixed by installing the relevant packages or
9095
stub libraries in the environment you're running ``mypy`` in.
9196

92-
See :ref:`ignore-missing-imports` for a complete reference on these errors
97+
See :ref:`fix-missing-imports` for a complete reference on these errors
9398
and the ways in which you can fix them.
9499

95100
You'll likely find that you want to suppress all errors from importing
@@ -118,13 +123,15 @@ codebase, use a config like this:
118123
ignore_missing_imports = True
119124
120125
If you get a large number of errors, you may want to ignore all errors
121-
about missing imports, for instance by setting :confval:`ignore_missing_imports`
122-
to true globally. This can hide errors later on, so we recommend avoiding this
126+
about missing imports, for instance by setting
127+
:option:`--disable-error-code=import-untyped <mypy --ignore-missing-imports>`.
128+
or setting :confval:`ignore_missing_imports` to true globally.
129+
This can hide errors later on, so we recommend avoiding this
123130
if possible.
124131

125132
Finally, mypy allows fine-grained control over specific import following
126133
behaviour. It's very easy to silently shoot yourself in the foot when playing
127-
around with these, so it's mostly recommended as a last resort. For more
134+
around with these, so this should be a last resort. For more
128135
details, look :ref:`here <follow-imports>`.
129136

130137
Prioritise annotating widely imported modules

docs/source/running_mypy.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,20 @@ not catch errors in its use.
305305
The ``.*`` after ``foobar`` will ignore imports of ``foobar`` modules
306306
and subpackages in addition to the ``foobar`` top-level package namespace.
307307

308-
3. To suppress *all* missing import errors for *all* libraries in your codebase,
309-
invoke mypy with the :option:`--ignore-missing-imports <mypy --ignore-missing-imports>` command line flag or set
310-
the :confval:`ignore_missing_imports`
311-
config file option to True
312-
in the *global* section of your mypy config file::
308+
3. To suppress *all* missing import errors for *all* untyped libraries
309+
in your codebase, use :option:`--disable-error-code=import-untyped <mypy --ignore-missing-imports>`.
310+
See :ref:`code-import-untyped` for more details on this error code.
311+
312+
You can also set :confval:`disable_error_code`, like so::
313313

314314
[mypy]
315-
ignore_missing_imports = True
315+
disable_error_code = import-untyped
316+
316317

317-
We recommend using this approach only as a last resort: it's equivalent
318+
You can also set the :option:`--ignore-missing-imports <mypy --ignore-missing-imports>`
319+
command line flag or set the :confval:`ignore_missing_imports` config file
320+
option to True in the *global* section of your mypy config file. We
321+
recommend avoiding ``--ignore-missing-imports`` if possible: it's equivalent
318322
to adding a ``# type: ignore`` to all unresolved imports in your codebase.
319323

320324

0 commit comments

Comments
 (0)