From c03e979ca06c3bf082a4cd07458a1bc3205dc5e5 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 18 Feb 2023 12:39:25 -0500 Subject: [PATCH] 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. --- docs/source/stubtest.rst | 4 ++-- mypy/stubtest.py | 8 ++++---- mypy/test/data.py | 4 +--- mypy/test/teststubtest.py | 6 +++--- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/docs/source/stubtest.rst b/docs/source/stubtest.rst index a8279eb6c239..f3c036f56c06 100644 --- a/docs/source/stubtest.rst +++ b/docs/source/stubtest.rst @@ -42,7 +42,7 @@ test Python's official collection of library stubs, `typeshed `_. .. warning:: - + stubtest will import and execute Python code from the packages it checks. Example @@ -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'] diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 4a99c407f319..cd173f63e2a1 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -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 @@ -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), diff --git a/mypy/test/data.py b/mypy/test/data.py index 535ebf304784..6e2ad198f614 100644 --- a/mypy/test/data.py +++ b/mypy/test/data.py @@ -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") diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 42dd40d76414..6bb4dfb2c937 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -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 @@ -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)