-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support silencing only some error codes #8102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -593,6 +593,41 @@ in error messages. | |
|
||
Show absolute paths to files. | ||
|
||
.. option:: --ignore-error-codes CODES | ||
|
||
This flag makes mypy ignore all errors with given error codes. Flag accepts | ||
error codes as a comma separated list (there should be no spaces after commas). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better for usability if spaces around commas were ignored. Error codes can't have spaces. Grammar nit: "comma separated" -> "comma-separated" (we use the latter spelling elsewhere) |
||
For example, by default mypy would produce the following errors: | ||
|
||
.. code-block:: python | ||
|
||
class Dynamic: | ||
def __init__(self, attr: str, value: object) -> None: | ||
setattr(self, attr, value) | ||
|
||
magic_builtin # error: Name "magic_builtin" is not defined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comments make this example look kind of busy, since some of the messages are long. Also, it would be nice to include the error codes in the message, as now it's unclear where the error codes come from ( Here's a suggested change:
|
||
Dynamic("test", 0).test # error: "Dynamic" has no attribute "test" | ||
x: int = "no" # error: Incompatible types in assignment | ||
# (expression has type "str", variable has type "int") | ||
|
||
But when used as ``mypy --ignore-error-codes=attr-defined,name-defined test.py`` | ||
it will produce the following errors: | ||
|
||
.. code-block:: python | ||
|
||
class Dynamic: | ||
def __init__(self, attr: str, value: object) -> None: | ||
setattr(self, attr, value) | ||
|
||
magic_builtin # No error | ||
Dynamic("test", 0).test # No error | ||
x: int = "no" # error: Incompatible types in assignment | ||
# (expression has type "str", variable has type "int") | ||
|
||
To make mypy show error codes in error messages use :option:`--show-error-codes`. | ||
See also the lists of :ref:`default error codes <error-code-list>` and | ||
:ref:`optional error codes <error-codes-optional>`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we should have a warning here, as ignoring some errors could be quite confusing and dangerous, especially |
||
|
||
|
||
.. _incremental: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -439,6 +439,11 @@ These options may only be set in the global section (``[mypy]``). | |
``show_absolute_path`` (bool, default False) | ||
Show absolute paths to files. | ||
|
||
``ignore_error_codes`` (comma-separated list of strings) | ||
Ignore errors with these error codes in given files or directories. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The part "in given files or directories" seems redundant. It's not included in other descriptions, and so it raises the question why this option applies to given files or directories, while other options don't apply to given files or directories? |
||
See :option:`--ignore-error-codes <mypy --ignore-error-codes>` for | ||
more details. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really tiny consistency nit: we use "for more information" about and "more details here". Maybe use "for more information" here as well. |
||
|
||
|
||
Incremental mode | ||
**************** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,8 @@ Silencing errors based on error codes | |
You can use a special comment ``# type: ignore[code, ...]`` to only | ||
ignore errors with a specific error code (or codes) on a particular | ||
line. This can be used even if you have not configured mypy to show | ||
error codes. Currently it's only possible to disable arbitrary error | ||
codes on individual lines using this comment. | ||
error codes. Also you can use :option:`--ignore-error-codes <mypy --ignore-error-codes>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nit: "Also" seems redundant. |
||
for more coarse-grained (per file or per directory) silencing of errors. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual command line flag only allows global silencing of errors, but this implies that it's per file or per directory. Either leave the parenthetical remark out, or somehow make it clear that per-file/directory options are only available through the config file or |
||
|
||
.. note:: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
from mypy.options import Options, BuildType | ||
from mypy.config_parser import parse_version, parse_config_file | ||
from mypy.split_namespace import SplitNamespace | ||
from mypy.util import split_commas | ||
|
||
from mypy.version import __version__ | ||
|
||
|
@@ -640,6 +641,9 @@ def add_invertible_flag(flag: str, | |
add_invertible_flag('--show-absolute-path', default=False, | ||
help="Show absolute paths to files", | ||
group=error_group) | ||
error_group.add_argument( | ||
'--ignore-error-codes', metavar='CODES', type=split_commas, | ||
help="Ignore errors with these error codes (comma separated)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spelling nit: use "comma-separated" (similar to above) |
||
|
||
incremental_group = parser.add_argument_group( | ||
title='Incremental mode', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ class BuildType: | |
"mypyc", | ||
"no_implicit_optional", | ||
"implicit_reexport", | ||
"ignore_error_codes", | ||
"show_none_errors", | ||
"strict_optional", | ||
"strict_optional_whitelist", | ||
|
@@ -132,6 +133,9 @@ def __init__(self) -> None: | |
# Files in which to ignore all non-fatal errors | ||
self.ignore_errors = False | ||
|
||
# Ignore errors with these error codes in a given file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: "in a given files." seems redunant. Also omit period at the end for consistency? |
||
self.ignore_error_codes = [] # type: List[str] | ||
|
||
# Apply strict None checking | ||
self.strict_optional = True | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1232,3 +1232,37 @@ A = List # OK | |
B = List[A] # E:10: Missing type parameters for generic type "A" | ||
x: A # E:4: Missing type parameters for generic type "A" | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testIgnoreErrorCodesGlobal] | ||
# flags: --ignore-error-codes=attr-defined,arg-type | ||
|
||
not_defined # E: Name 'not_defined' is not defined | ||
x: int = 'no' # E: Incompatible types in assignment (expression has type "str", variable has type "int") | ||
'no'.no_way | ||
def test(x: str) -> None: ... | ||
test(0) | ||
|
||
[case testIgnoreErrorCodesPerFile] | ||
# flags: --config-file tmp/mypy.ini | ||
import b | ||
not_defined | ||
x: int = 'no' | ||
'no'.no_way | ||
def test(x: str) -> None: ... | ||
test(0) | ||
[file b.py] | ||
not_defined | ||
x: int = 'no' | ||
'no'.no_way | ||
def test(x: str) -> None: ... | ||
test(0) | ||
[file mypy.ini] | ||
\[mypy] | ||
ignore_error_codes = attr-defined, arg-type | ||
\[mypy-b] | ||
ignore_error_codes = name-defined, assignment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to eventually move various strictness flags to be enabled/disabled through error codes. To do this, it seems that per-file options need to be a delta over global settings, instead of replacing them. Also, command-line options probably should be a delta over settings in the ini file. I.e., the ignored error codes would be the union of error codes ignored in the config file, on the command line, and in per-file options. The logic gets a bit more involved once we allow enabling error codes as well, but at least we should be able to design the semantic for ignoring error codes in a way that we don't need to introduce a compatibility break. |
||
[out] | ||
tmp/b.py:3: error: "str" has no attribute "no_way" | ||
tmp/b.py:5: error: Argument 1 to "test" has incompatible type "int"; expected "str" | ||
main:3: error: Name 'not_defined' is not defined | ||
main:4: error: Incompatible types in assignment (expression has type "str", variable has type "int") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,3 +157,12 @@ main:4: error: Unterminated quote in configuration comment | |
# mypy: skip-file | ||
[out] | ||
main:1: error: Unrecognized option: skip_file = True | ||
|
||
[case testIgnoreErrorCodesInline] | ||
# mypy: ignore-error-codes="attr-defined,arg-type" | ||
|
||
not_defined # E: Name 'not_defined' is not defined | ||
x: int = 'no' # E: Incompatible types in assignment (expression has type "str", variable has type "int") | ||
'no'.no_way | ||
def test(x: str) -> None: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add daemon test case (to make sure the daemon applies these after updates). |
||
test(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we should include a link to documentatino about error codes here instead of later. The text "error codes" could link to the relevant topic.
Grammar nit: "Flag" -> "This flag"