From 738a86a64b32889a254e1ede605a01c756f4c87d Mon Sep 17 00:00:00 2001 From: Chinh Nguyen Date: Mon, 8 May 2023 23:58:37 +0900 Subject: [PATCH 1/3] add not documented errors code add document for error codes: - str-format - empty-body - top-level-await - annotation-unchecked - possibly-undefined --- docs/source/error_code_list2.rst | 92 ++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 27 deletions(-) diff --git a/docs/source/error_code_list2.rst b/docs/source/error_code_list2.rst index 8be2ac0b1d73..93aba10e4295 100644 --- a/docs/source/error_code_list2.rst +++ b/docs/source/error_code_list2.rst @@ -347,47 +347,85 @@ silence the error: async def g() -> None: _ = asyncio.create_task(f()) # No error +Check that variables are defined only in some execution paths [possibly-undefined] +---------------------------------------------------------------------------------- +If you use :option:`--enable-error-code possibly-undefined `, +mypy generates an error if you don't define a variable in all execution paths. -Check that ``# type: ignore`` comment is used [unused-ignore] -------------------------------------------------------------- +Example: + +.. code-block:: python + + # Use "mypy --enable-error-code possibly-undefined ..." -If you use :option:`--enable-error-code unused-ignore `, -or :option:`--warn-unused-ignores ` -mypy generates an error if you don't use a ``# type: ignore`` comment, i.e. if -there is a comment, but there would be no error generated by mypy on this line -anyway. + def f(x: int) -> None: + if x > 0: + y = 1 + # Error: Possibly undefined variable "y" + print(y) + +Check that functions have empty body [empty-body] +------------------------------------------------------------------- +mypy generates an error if you have a function that has an empty body. Example: .. code-block:: python - # Use "mypy --warn-unused-ignores ..." + # Use "mypy --enable-error-code empty-body ..." - def add(a: int, b: int) -> int: - # Error: unused "type: ignore" comment - return a + b # type: ignore + def f(x: int) -> str: + # Error: Function is missing a body + pass -Note that due to a specific nature of this comment, the only way to selectively -silence it, is to include the error code explicitly. Also note that this error is -not shown if the ``# type: ignore`` is not used due to code being statically -unreachable (e.g. due to platform or version checks). + +Check that your code have top-level await statements [top-level-await] +--------------------------------------------------------------------- +mypy generates an error if you have a top-level await statement. Example: .. code-block:: python - # Use "mypy --warn-unused-ignores ..." - import sys + import asyncio + + async def f() -> int: + return 1 - try: - # The "[unused-ignore]" is needed to get a clean mypy run - # on both Python 3.8, and 3.9 where this module was added - import graphlib # type: ignore[import,unused-ignore] - except ImportError: - pass + # Error: "await" used at the top level + await f() + + +Check that strings have wrong format parameters [str-format] +--------------------------------------------------------------------- +mypy generates an error if you have a string with wrong format parameters. + +Example: + +.. code-block:: python + + # Error: More parameters than arguments + print("Hello {0} {1}".format("World")) + + # Error: More arguments than parameters + print("Hello {0} {1}".format(1, 2, 3)) + + # Error: format argument is not type-safe + print("%d %d" % ("Hello", "World")) + +Check that annotations are inside an unannotated function [annotation-unchecked] +------------------------------------------------------------------------------- +mypy generates an error to notify that you have annotations which are not checked in an unannotated function. + +Consider using --check-untyped-defs option if you want to check unannotated functions as well. + +Example: + +.. code-block:: python + + def f(): + x: int = 1 + # Error: Annotation type "int" is not checked + return x - if sys.version_info >= (3, 9): - # The following will not generate an error on either - # Python 3.8, or Python 3.9 - 42 + "testing..." # type: ignore From e7bfb4ee1042ca5b5e9a95dd98f0e758cd3efdbe Mon Sep 17 00:00:00 2001 From: Chinh Nguyen Date: Tue, 9 May 2023 00:06:13 +0900 Subject: [PATCH 2/3] format markup --- docs/source/error_code_list2.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/source/error_code_list2.rst b/docs/source/error_code_list2.rst index 93aba10e4295..bd0963db8bc7 100644 --- a/docs/source/error_code_list2.rst +++ b/docs/source/error_code_list2.rst @@ -347,8 +347,10 @@ silence the error: async def g() -> None: _ = asyncio.create_task(f()) # No error + Check that variables are defined only in some execution paths [possibly-undefined] ---------------------------------------------------------------------------------- + If you use :option:`--enable-error-code possibly-undefined `, mypy generates an error if you don't define a variable in all execution paths. @@ -367,6 +369,7 @@ Example: Check that functions have empty body [empty-body] ------------------------------------------------------------------- + mypy generates an error if you have a function that has an empty body. Example: @@ -378,9 +381,9 @@ Example: # Error: Function is missing a body pass - Check that your code have top-level await statements [top-level-await] --------------------------------------------------------------------- + mypy generates an error if you have a top-level await statement. Example: @@ -399,6 +402,7 @@ Example: Check that strings have wrong format parameters [str-format] --------------------------------------------------------------------- + mypy generates an error if you have a string with wrong format parameters. Example: @@ -416,6 +420,7 @@ Example: Check that annotations are inside an unannotated function [annotation-unchecked] ------------------------------------------------------------------------------- + mypy generates an error to notify that you have annotations which are not checked in an unannotated function. Consider using --check-untyped-defs option if you want to check unannotated functions as well. @@ -428,4 +433,3 @@ Example: x: int = 1 # Error: Annotation type "int" is not checked return x - From 61544700d2573dce1b4887b6fa87bdf74d5eda0f Mon Sep 17 00:00:00 2001 From: Chinh Nguyen Date: Tue, 9 May 2023 00:08:56 +0900 Subject: [PATCH 3/3] fix Title underline --- docs/source/error_code_list2.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/error_code_list2.rst b/docs/source/error_code_list2.rst index bd0963db8bc7..00ca5895a14b 100644 --- a/docs/source/error_code_list2.rst +++ b/docs/source/error_code_list2.rst @@ -382,7 +382,7 @@ Example: pass Check that your code have top-level await statements [top-level-await] ---------------------------------------------------------------------- +---------------------------------------------------------------------- mypy generates an error if you have a top-level await statement. @@ -401,7 +401,7 @@ Example: Check that strings have wrong format parameters [str-format] ---------------------------------------------------------------------- +------------------------------------------------------------ mypy generates an error if you have a string with wrong format parameters. @@ -419,7 +419,7 @@ Example: print("%d %d" % ("Hello", "World")) Check that annotations are inside an unannotated function [annotation-unchecked] -------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- mypy generates an error to notify that you have annotations which are not checked in an unannotated function.