Skip to content

Commit

Permalink
Stubtest: Link directly to line (#14437)
Browse files Browse the repository at this point in the history
This format allows editors and terminals that support linking to
specific lines in files to go directly to the right line.
  • Loading branch information
Avasam committed Feb 18, 2023
1 parent 8a487ff commit c03e979
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/source/stubtest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test Python's official collection of library stubs,
`typeshed <https://github.com/python/typeshed>`_.

.. warning::

stubtest will import and execute Python code from the packages it checks.

Example
Expand All @@ -69,7 +69,7 @@ Here's a quick example of what stubtest can do:
error: library.foo is inconsistent, runtime argument "x" has a default value but stub argument does not
Stub: at line 3
def (x: builtins.int)
Runtime: at line 3 in file ~/library.py
Runtime: in file ~/library.py:3
def (x=None)
error: library.x variable differs from runtime type Literal['hello, stubtest']
Expand Down
8 changes: 4 additions & 4 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ def get_description(self, concise: bool = False) -> str:
stub_file = stub_node.path or None

stub_loc_str = ""
if stub_line:
stub_loc_str += f" at line {stub_line}"
if stub_file:
stub_loc_str += f" in file {Path(stub_file)}"
if stub_line:
stub_loc_str += f"{':' if stub_file else ' at line '}{stub_line}"

runtime_line = None
runtime_file = None
Expand All @@ -147,10 +147,10 @@ def get_description(self, concise: bool = False) -> str:
pass

runtime_loc_str = ""
if runtime_line:
runtime_loc_str += f" at line {runtime_line}"
if runtime_file:
runtime_loc_str += f" in file {Path(runtime_file)}"
if runtime_line:
runtime_loc_str += f"{':' if runtime_file else ' at line '}{runtime_line}"

output = [
_style("error: ", color="red", bold=True),
Expand Down
4 changes: 1 addition & 3 deletions mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ def parse_test_case(case: DataDrivenTestCase) -> None:
elif item.id == "triggered" and item.arg is None:
triggered = item.data
else:
raise ValueError(
f"Invalid section header {item.id} in {case.file} at line {item.line}"
)
raise ValueError(f"Invalid section header {item.id} in {case.file}:{item.line}")

if out_section_missing:
raise ValueError(f"{case.file}, line {first_item.line}: Required output section not found")
Expand Down
6 changes: 3 additions & 3 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,9 +1561,9 @@ def test_output(self) -> None:
expected = (
f'error: {TEST_MODULE_NAME}.bad is inconsistent, stub argument "number" differs '
'from runtime argument "num"\n'
f"Stub: at line 1 in file {TEST_MODULE_NAME}.pyi\n"
f"Stub: in file {TEST_MODULE_NAME}.pyi:1\n"
"def (number: builtins.int, text: builtins.str)\n"
f"Runtime: at line 1 in file {TEST_MODULE_NAME}.py\ndef (num, text)\n\n"
f"Runtime: in file {TEST_MODULE_NAME}.py:1\ndef (num, text)\n\n"
"Found 1 error (checked 1 module)\n"
)
assert remove_color_code(output) == expected
Expand Down Expand Up @@ -1721,7 +1721,7 @@ def test_config_file(self) -> None:
output = run_stubtest(stub=stub, runtime=runtime, options=[])
assert remove_color_code(output) == (
f"error: {TEST_MODULE_NAME}.temp variable differs from runtime type Literal[5]\n"
f"Stub: at line 2 in file {TEST_MODULE_NAME}.pyi\n_decimal.Decimal\nRuntime:\n5\n\n"
f"Stub: in file {TEST_MODULE_NAME}.pyi:2\n_decimal.Decimal\nRuntime:\n5\n\n"
"Found 1 error (checked 1 module)\n"
)
output = run_stubtest(stub=stub, runtime=runtime, options=[], config_file=config_file)
Expand Down

0 comments on commit c03e979

Please sign in to comment.