Skip to content

Commit 8052c8d

Browse files
Add documentation for type: ignore (#11358)
Adds a reference on how # type: ignore can be used. Resolves #11354
1 parent 01d6eb8 commit 8052c8d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

docs/source/error_codes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ or config `show_error_codes = True` to display error codes. Error codes are show
3131
$ mypy --show-error-codes prog.py
3232
prog.py:1: error: "str" has no attribute "trim" [attr-defined]
3333
34+
.. _silence-error-codes:
35+
3436
Silencing errors based on error codes
3537
-------------------------------------
3638

docs/source/type_inference_and_annotations.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,66 @@ to be annotated with a starred type:
216216
p, q, *rs = 1, 2 # type: int, int, List[int]
217217
218218
Here, the type of ``rs`` is set to ``List[int]``.
219+
220+
Silencing type errors
221+
*********************
222+
223+
You might want to disable type checking on specific lines, or within specific
224+
files in your codebase. To do that, you can use a ``# type: ignore`` comment.
225+
226+
For example, say that the web framework that you use now takes an integer
227+
argument to ``run()``, which starts it on localhost on that port. Liks so:
228+
229+
.. code-block:: python
230+
231+
# Starting app on http://localhost:8000
232+
app.run(8000)
233+
234+
However, the type stubs that the package uses is not up-to-date, and it still
235+
expects only `str` types for `run()`. This would give you the following error:
236+
237+
.. code-block:: text
238+
239+
error: Argument 1 to "run" of "A" has incompatible type "int"; expected "str"
240+
241+
If you cannot directly fix the type stubs yourself, you can temporarily
242+
disable type checking on that line, by adding a ``# type: ignore``:
243+
244+
.. code-block:: python
245+
246+
# Starting app on http://localhost:8000
247+
app.run(8000) # type: ignore
248+
249+
This will suppress any mypy errors that would have raised on that specific line.
250+
251+
You should probably add some more information on the ``# type: ignore`` comment,
252+
to explain why the ignore was added in the first place. This could be a link to
253+
an issue on the repository responsible for the type stubs, or it could be a
254+
short explanation of the bug. To do that, use this format:
255+
256+
.. code-block:: python
257+
258+
# Starting app on http://localhost:8000
259+
app.run(8000) # type: ignore # `run()` now accepts an `int`, as a port
260+
261+
262+
If your error displays an error code, like so:
263+
264+
.. code-block:: text
265+
266+
error: "str" has no attribute "trim" [attr-defined]
267+
268+
269+
It is possible to add a specific error-code in your message, like
270+
``# type: ignore[attr-defined]``, to clarify what's being silenced. You can
271+
find more information about error codes here: :ref:`silence-error-codes`.
272+
273+
Similarly, you can also ignore all mypy checks in a file, by adding a
274+
``# type: ignore`` on the top of the file:
275+
276+
.. code-block:: python
277+
278+
# type: ignore
279+
# This is a test file, skipping type checking in it.
280+
import unittest
281+
...

0 commit comments

Comments
 (0)