Skip to content
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

Add option to show links to error code docs (once per code) #15449

Merged
merged 6 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
SHOW_NOTE_CODES: Final = {codes.ANNOTATION_UNCHECKED}
allowed_duplicates: Final = ["@overload", "Got:", "Expected:"]

BASE_RTD_URL: Final = "https://mypy.rtfd.io/en/stable/_refs.html#code"

# Keep track of the original error code when the error code of a message is changed.
# This is used to give notes about out-of-date "type: ignore" comments.
original_error_codes: Final = {codes.LITERAL_REQ: codes.MISC, codes.TYPE_ABSTRACT: codes.MISC}
Expand Down Expand Up @@ -434,6 +436,34 @@ def report(
target=self.current_target(),
)
self.add_error_info(info)
if (
self.options.show_error_code_links
and not self.options.hide_error_codes
and code is not None
):
message = f"See {BASE_RTD_URL}-{code.code} for information about this error"
if offset:
message = " " * offset + message
info = ErrorInfo(
self.import_context(),
file,
self.current_module(),
type,
function,
line,
column,
end_line,
end_column,
"note",
message,
code,
blocker=False,
only_once=True,
allow_dups=False,
origin=(self.file, origin_span),
target=self.current_target(),
)
self.add_error_info(info)

def _add_error_info(self, file: str, info: ErrorInfo) -> None:
assert file not in self.flushed_files
Expand Down
6 changes: 6 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,12 @@ def add_invertible_flag(
help="Hide error codes in error messages",
group=error_group,
)
add_invertible_flag(
"--show-error-code-links",
default=False,
help="Show links to error code documentation",
group=error_group,
)
add_invertible_flag(
"--pretty",
default=False,
Expand Down
1 change: 1 addition & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def __init__(self) -> None:
self.show_column_numbers: bool = False
self.show_error_end: bool = False
self.hide_error_codes = False
self.show_error_code_links = False
# Use soft word wrap and show trimmed source snippets with error location markers.
self.pretty = False
self.dump_graph = False
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -2195,3 +2195,10 @@ cb(lambda x: a) # OK

fn = lambda x: a
cb(fn)

[case testShowErrorCodeLinks]
# flags: --show-error-codes --show-error-code-links

x: int = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] \
# N: See https://mypy.rtfd.io/en/stable/_refs.html#code-assignment for information about this error
y: int = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]