From 236d80b62c48cf3e5f76bfe85c18ed2f475f339f Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Tue, 19 Oct 2021 20:19:38 +0530 Subject: [PATCH 1/4] Add documentation for type: ignore --- .../source/type_inference_and_annotations.rst | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/docs/source/type_inference_and_annotations.rst b/docs/source/type_inference_and_annotations.rst index 38518a8f2c3b2..81c748e027719 100644 --- a/docs/source/type_inference_and_annotations.rst +++ b/docs/source/type_inference_and_annotations.rst @@ -216,3 +216,54 @@ to be annotated with a starred type: p, q, *rs = 1, 2 # type: int, int, List[int] Here, the type of ``rs`` is set to ``List[int]``. + +Disabling type checks +********************* + +You might want to disable type checking on specific lines, or within specific +files in your codebase. To do that, you can use a ``type: ignore`` comment. + +For example, say that the web framework that you use now takes an integer +argument to ``run()``, which starts it on localhost on that port. Liks so: + +.. code-block:: python + + # Starting app on http://localhost:8000 + app.run(8000) + +However, the type stubs that the package uses is not up-to-date, and it still +expects only `str` types for `run()`. This would give you the following error: + +.. code-block:: text + + error: Argument 1 to "run" of "A" has incompatible type "int"; expected "str" + +If you cannot directly fix the type stubs yourself, you can temporarily +disable type checking on that line, by adding a ``# type: ignore``: + +.. code-block:: python + + # Starting app on http://localhost:8000 + app.run(8000) # type: ignore + +This will suppress any mypy errors that would have raised on that specific line. + +You should probably add some more information on the ``type: ignore`` comment, +to explain why the ignore was added in the first place. This could be a link to +an issue on the repository responsible for the type stubs, or it could be a +short explanation of the bug. To do that, use this format: + +.. code-block:: python + + # Starting app on http://localhost:8000 + app.run(8000) # type: ignore # `run()` now accepts an `int`, as a port + +Similarly, you can also ignore all mypy checks in a file, by adding a +``# type: ignore`` on the top of the file: + +.. code-block:: python + + # type: ignore + # This is a test file, skipping type checking in it. + import unittest + ... From d83b3cb9c5a06ee1ba05569efa7303f9fe667041 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Wed, 20 Oct 2021 16:45:19 +0530 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Ethan Smith --- docs/source/type_inference_and_annotations.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/type_inference_and_annotations.rst b/docs/source/type_inference_and_annotations.rst index 81c748e027719..3391fa5e91c1a 100644 --- a/docs/source/type_inference_and_annotations.rst +++ b/docs/source/type_inference_and_annotations.rst @@ -217,11 +217,11 @@ to be annotated with a starred type: Here, the type of ``rs`` is set to ``List[int]``. -Disabling type checks +Silencing type errors ********************* You might want to disable type checking on specific lines, or within specific -files in your codebase. To do that, you can use a ``type: ignore`` comment. +files in your codebase. To do that, you can use a ``# type: ignore`` comment. For example, say that the web framework that you use now takes an integer argument to ``run()``, which starts it on localhost on that port. Liks so: @@ -248,7 +248,7 @@ disable type checking on that line, by adding a ``# type: ignore``: This will suppress any mypy errors that would have raised on that specific line. -You should probably add some more information on the ``type: ignore`` comment, +You should probably add some more information on the ``# type: ignore`` comment, to explain why the ignore was added in the first place. This could be a link to an issue on the repository responsible for the type stubs, or it could be a short explanation of the bug. To do that, use this format: From 760e3e4a56f822c7fa4ade5087190e3f91a86b46 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Wed, 20 Oct 2021 16:59:19 +0530 Subject: [PATCH 3/4] Add section on silencing error codes --- docs/source/error_codes.rst | 2 ++ docs/source/type_inference_and_annotations.rst | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/docs/source/error_codes.rst b/docs/source/error_codes.rst index 8a654571bc6b1..5255a6984b7b9 100644 --- a/docs/source/error_codes.rst +++ b/docs/source/error_codes.rst @@ -31,6 +31,8 @@ or config `show_error_codes = True` to display error codes. Error codes are show $ mypy --show-error-codes prog.py prog.py:1: error: "str" has no attribute "trim" [attr-defined] +.. _silence-error-codes: + Silencing errors based on error codes ------------------------------------- diff --git a/docs/source/type_inference_and_annotations.rst b/docs/source/type_inference_and_annotations.rst index 3391fa5e91c1a..eeb4b7357bf0f 100644 --- a/docs/source/type_inference_and_annotations.rst +++ b/docs/source/type_inference_and_annotations.rst @@ -258,6 +258,18 @@ short explanation of the bug. To do that, use this format: # Starting app on http://localhost:8000 app.run(8000) # type: ignore # `run()` now accepts an `int`, as a port + +If your error displays an error code, like so: + +.. code-block:: text + + error: "str" has no attribute "trim" [attr-defined] + + +It is possible to add a specific error-code in your message, like +``# type: ignore[attr-defined]``, to clarify what's being silenced. You can +find more information about error codes here: :ref:`silence-error-codes` + Similarly, you can also ignore all mypy checks in a file, by adding a ``# type: ignore`` on the top of the file: From 37b6d9f7bf6c7274c6103b37c939f79cf98ecf19 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Wed, 20 Oct 2021 17:01:04 +0530 Subject: [PATCH 4/4] Punctuation --- docs/source/type_inference_and_annotations.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/type_inference_and_annotations.rst b/docs/source/type_inference_and_annotations.rst index eeb4b7357bf0f..14aceb523c317 100644 --- a/docs/source/type_inference_and_annotations.rst +++ b/docs/source/type_inference_and_annotations.rst @@ -268,7 +268,7 @@ If your error displays an error code, like so: It is possible to add a specific error-code in your message, like ``# type: ignore[attr-defined]``, to clarify what's being silenced. You can -find more information about error codes here: :ref:`silence-error-codes` +find more information about error codes here: :ref:`silence-error-codes`. Similarly, you can also ignore all mypy checks in a file, by adding a ``# type: ignore`` on the top of the file: