Skip to content

Show error codes by default #13542

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

Merged
merged 11 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,9 @@ in error messages.
``file:line:column:end_line:end_column``. This option implies
``--show-column-numbers``.

.. option:: --show-error-codes
.. option:: --hide-error-codes

This flag will add an error code ``[<code>]`` to error messages. The error
This flag will hide the error code ``[<code>]`` from error messages. By default, the error
code is shown after each error message::

prog.py:1: error: "str" has no attribute "trim" [attr-defined]
Expand Down
4 changes: 2 additions & 2 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,12 @@ These options may only be set in the global section (``[mypy]``).

Shows column numbers in error messages.

.. confval:: show_error_codes
.. confval:: hide_error_codes

:type: boolean
:default: False

Shows error codes in error messages. See :ref:`error-codes` for more information.
Hide error codes in error messages. See :ref:`error-codes` for more information.

.. confval:: pretty

Expand Down
6 changes: 3 additions & 3 deletions docs/source/error_codes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Error codes may change in future mypy releases.
Displaying error codes
----------------------

Error codes are not displayed by default. Use :option:`--show-error-codes <mypy --show-error-codes>`
or config ``show_error_codes = True`` to display error codes. Error codes are shown inside square brackets:
Error codes are displayed by default. Use :option:`--hide-error-codes <mypy --hide-error-codes>`
or config ``hide_error_codes = True`` to hide error codes. Error codes are shown inside square brackets:

.. code-block:: text

$ mypy --show-error-codes prog.py
$ mypy prog.py
prog.py:1: error: "str" has no attribute "trim" [attr-defined]

It's also possible to require error codes for ``type: ignore`` comments.
Expand Down
3 changes: 1 addition & 2 deletions docs/source/type_inference_and_annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ short explanation of the bug. To do that, use this format:
app.run(8000) # type: ignore # `run()` now accepts an `int`, as a port


Mypy displays an error code for each error if you use
:option:`--show-error-codes <mypy --show-error-codes>`:
By default, mypy displays an error code for each error:

.. code-block:: text

Expand Down
2 changes: 1 addition & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def _build(
errors = Errors(
options.show_error_context,
options.show_column_numbers,
options.show_error_codes,
options.hide_error_codes,
options.pretty,
options.show_error_end,
lambda path: read_py_file(path, cached_read),
Expand Down
3 changes: 3 additions & 0 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ def parse_section(
elif key.startswith("disallow") and hasattr(template, key[3:]):
options_key = key[3:]
invert = True
elif key.startswith("show_") and hasattr(template, "hide_" + key[5:]):
options_key = "hide_" + key[5:]
invert = True
elif key == "strict":
pass # Special handling below
else:
Expand Down
2 changes: 1 addition & 1 deletion mypy/dmypy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def __init__(self, options: Options, status_file: str, timeout: int | None = Non

# Since the object is created in the parent process we can check
# the output terminal options here.
self.formatter = FancyFormatter(sys.stdout, sys.stderr, options.show_error_codes)
self.formatter = FancyFormatter(sys.stdout, sys.stderr, options.hide_error_codes)

def _response_metadata(self) -> dict[str, str]:
py_version = f"{self.options.python_version[0]}_{self.options.python_version[1]}"
Expand Down
6 changes: 3 additions & 3 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def __init__(
self,
show_error_context: bool = False,
show_column_numbers: bool = False,
show_error_codes: bool = False,
hide_error_codes: bool = False,
pretty: bool = False,
show_error_end: bool = False,
read_source: Callable[[str], list[str] | None] | None = None,
Expand All @@ -267,7 +267,7 @@ def __init__(
) -> None:
self.show_error_context = show_error_context
self.show_column_numbers = show_column_numbers
self.show_error_codes = show_error_codes
self.hide_error_codes = hide_error_codes
self.show_absolute_path = show_absolute_path
self.pretty = pretty
self.show_error_end = show_error_end
Expand Down Expand Up @@ -776,7 +776,7 @@ def format_messages(
s = f"{srcloc}: {severity}: {message}"
else:
s = message
if self.show_error_codes and code and severity != "note":
if not self.hide_error_codes and code and severity != "note":
# If note has an error code, it is related to a previous error. Avoid
# displaying duplicate error codes.
s = f"{s} [{code.code}]"
Expand Down
8 changes: 4 additions & 4 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def main(
if clean_exit:
options.fast_exit = False

formatter = util.FancyFormatter(stdout, stderr, options.show_error_codes)
formatter = util.FancyFormatter(stdout, stderr, options.hide_error_codes)

if options.install_types and (stdout is not sys.stdout or stderr is not sys.stderr):
# Since --install-types performs user input, we want regular stdout and stderr.
Expand Down Expand Up @@ -151,7 +151,7 @@ def run_build(
stdout: TextIO,
stderr: TextIO,
) -> tuple[build.BuildResult | None, list[str], bool]:
formatter = util.FancyFormatter(stdout, stderr, options.show_error_codes)
formatter = util.FancyFormatter(stdout, stderr, options.hide_error_codes)

messages = []

Expand Down Expand Up @@ -871,9 +871,9 @@ def add_invertible_flag(
group=error_group,
)
add_invertible_flag(
"--show-error-codes",
"--hide-error-codes",
default=False,
help="Show error codes in error messages",
help="Hide error codes in error messages",
group=error_group,
)
add_invertible_flag(
Expand Down
2 changes: 1 addition & 1 deletion mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def __init__(self) -> None:
self.shadow_file: list[list[str]] | None = None
self.show_column_numbers: bool = False
self.show_error_end: bool = False
self.show_error_codes = False
self.hide_error_codes = False
# Use soft word wrap and show trimmed source snippets with error location markers.
self.pretty = False
self.dump_graph = False
Expand Down
3 changes: 2 additions & 1 deletion mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def parse_options(
flags = flags2

if flags:
flag_list = flags.group(1).split()
flag_list = ["--hide-error-codes", *flags.group(1).split()]
flag_list.append("--no-site-packages") # the tests shouldn't need an installed Python
targets, options = process_options(flag_list, require_targets=False)
if targets:
Expand All @@ -375,6 +375,7 @@ def parse_options(
# TODO: Enable strict optional in test cases by default (requires *many* test case changes)
options.strict_optional = False
options.error_summary = False
options.hide_error_codes = True

# Allow custom python version to override testfile_pyversion.
if all(flag.split("=")[0] not in ["--python-version", "-2", "--py2"] for flag in flag_list):
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def run_case_once(
if "columns" in testcase.file:
options.show_column_numbers = True
if "errorcodes" in testcase.file:
options.show_error_codes = True
options.hide_error_codes = False

if incremental_step and options.incremental:
# Don't overwrite # flags: --no-incremental in incremental test cases
Expand Down
6 changes: 3 additions & 3 deletions mypy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ class FancyFormatter:
This currently only works on Linux and Mac.
"""

def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> None:
self.show_error_codes = show_error_codes
def __init__(self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool) -> None:
self.hide_error_codes = hide_error_codes
# Check if we are in a human-facing terminal on a supported platform.
if sys.platform not in ("linux", "darwin", "win32"):
self.dummy_term = True
Expand Down Expand Up @@ -681,7 +681,7 @@ def colorize(self, error: str) -> str:
"""Colorize an output line by highlighting the status and error code."""
if ": error:" in error:
loc, msg = error.split("error:", maxsplit=1)
if not self.show_error_codes:
if self.hide_error_codes:
return (
loc + self.style("error:", "red", bold=True) + self.highlight_quote_groups(msg)
)
Expand Down
1 change: 0 additions & 1 deletion mypy_self_check.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ warn_no_return = True
strict_optional = True
disallow_any_unimported = True
show_traceback = True
show_error_codes = True
pretty = True
always_false = MYPYC
plugins = misc/proper_plugin.py
Expand Down