Skip to content

Commit c03e979

Browse files
authored
Stubtest: Link directly to line (#14437)
This format allows editors and terminals that support linking to specific lines in files to go directly to the right line.
1 parent 8a487ff commit c03e979

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

docs/source/stubtest.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test Python's official collection of library stubs,
4242
`typeshed <https://github.com/python/typeshed>`_.
4343

4444
.. warning::
45-
45+
4646
stubtest will import and execute Python code from the packages it checks.
4747

4848
Example
@@ -69,7 +69,7 @@ Here's a quick example of what stubtest can do:
6969
error: library.foo is inconsistent, runtime argument "x" has a default value but stub argument does not
7070
Stub: at line 3
7171
def (x: builtins.int)
72-
Runtime: at line 3 in file ~/library.py
72+
Runtime: in file ~/library.py:3
7373
def (x=None)
7474
7575
error: library.x variable differs from runtime type Literal['hello, stubtest']

mypy/stubtest.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ def get_description(self, concise: bool = False) -> str:
129129
stub_file = stub_node.path or None
130130

131131
stub_loc_str = ""
132-
if stub_line:
133-
stub_loc_str += f" at line {stub_line}"
134132
if stub_file:
135133
stub_loc_str += f" in file {Path(stub_file)}"
134+
if stub_line:
135+
stub_loc_str += f"{':' if stub_file else ' at line '}{stub_line}"
136136

137137
runtime_line = None
138138
runtime_file = None
@@ -147,10 +147,10 @@ def get_description(self, concise: bool = False) -> str:
147147
pass
148148

149149
runtime_loc_str = ""
150-
if runtime_line:
151-
runtime_loc_str += f" at line {runtime_line}"
152150
if runtime_file:
153151
runtime_loc_str += f" in file {Path(runtime_file)}"
152+
if runtime_line:
153+
runtime_loc_str += f"{':' if runtime_file else ' at line '}{runtime_line}"
154154

155155
output = [
156156
_style("error: ", color="red", bold=True),

mypy/test/data.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ def parse_test_case(case: DataDrivenTestCase) -> None:
169169
elif item.id == "triggered" and item.arg is None:
170170
triggered = item.data
171171
else:
172-
raise ValueError(
173-
f"Invalid section header {item.id} in {case.file} at line {item.line}"
174-
)
172+
raise ValueError(f"Invalid section header {item.id} in {case.file}:{item.line}")
175173

176174
if out_section_missing:
177175
raise ValueError(f"{case.file}, line {first_item.line}: Required output section not found")

mypy/test/teststubtest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1561,9 +1561,9 @@ def test_output(self) -> None:
15611561
expected = (
15621562
f'error: {TEST_MODULE_NAME}.bad is inconsistent, stub argument "number" differs '
15631563
'from runtime argument "num"\n'
1564-
f"Stub: at line 1 in file {TEST_MODULE_NAME}.pyi\n"
1564+
f"Stub: in file {TEST_MODULE_NAME}.pyi:1\n"
15651565
"def (number: builtins.int, text: builtins.str)\n"
1566-
f"Runtime: at line 1 in file {TEST_MODULE_NAME}.py\ndef (num, text)\n\n"
1566+
f"Runtime: in file {TEST_MODULE_NAME}.py:1\ndef (num, text)\n\n"
15671567
"Found 1 error (checked 1 module)\n"
15681568
)
15691569
assert remove_color_code(output) == expected
@@ -1721,7 +1721,7 @@ def test_config_file(self) -> None:
17211721
output = run_stubtest(stub=stub, runtime=runtime, options=[])
17221722
assert remove_color_code(output) == (
17231723
f"error: {TEST_MODULE_NAME}.temp variable differs from runtime type Literal[5]\n"
1724-
f"Stub: at line 2 in file {TEST_MODULE_NAME}.pyi\n_decimal.Decimal\nRuntime:\n5\n\n"
1724+
f"Stub: in file {TEST_MODULE_NAME}.pyi:2\n_decimal.Decimal\nRuntime:\n5\n\n"
17251725
"Found 1 error (checked 1 module)\n"
17261726
)
17271727
output = run_stubtest(stub=stub, runtime=runtime, options=[], config_file=config_file)

0 commit comments

Comments
 (0)