Skip to content

Commit 3aa9296

Browse files
authored
stubtest: show path to stub file (#13342)
1 parent 11fe165 commit 3aa9296

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

mypy/stubtest.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def __init__(
9090
:param runtime_desc: Specialised description for the runtime object, should you wish
9191
9292
"""
93+
self.object_path = object_path
9394
self.object_desc = ".".join(object_path)
9495
self.message = message
9596
self.stub_object = stub_object
@@ -116,10 +117,12 @@ def get_description(self, concise: bool = False) -> str:
116117
return _style(self.object_desc, bold=True) + " " + self.message
117118

118119
stub_line = None
119-
stub_file: None = None
120+
stub_file = None
120121
if not isinstance(self.stub_object, Missing):
121122
stub_line = self.stub_object.line
122-
# TODO: Find a way of getting the stub file
123+
stub_node = get_stub(self.object_path[0])
124+
if stub_node is not None:
125+
stub_file = stub_node.path or None
123126

124127
stub_loc_str = ""
125128
if stub_line:

mypy/test/teststubtest.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ def run_stubtest(
121121
with contextlib.redirect_stdout(output):
122122
test_stubs(parse_options([TEST_MODULE_NAME] + options), use_builtins_fixtures=True)
123123
# remove cwd as it's not available from outside
124-
return output.getvalue().replace(tmp_dir + os.sep, "")
124+
return (
125+
output.getvalue()
126+
.replace(os.path.realpath(tmp_dir) + os.sep, "")
127+
.replace(tmp_dir + os.sep, "")
128+
)
125129

126130

127131
class Case:
@@ -1279,7 +1283,8 @@ def test_output(self) -> None:
12791283
expected = (
12801284
f'error: {TEST_MODULE_NAME}.bad is inconsistent, stub argument "number" differs '
12811285
'from runtime argument "num"\n'
1282-
"Stub: at line 1\ndef (number: builtins.int, text: builtins.str)\n"
1286+
f"Stub: at line 1 in file {TEST_MODULE_NAME}.pyi\n"
1287+
"def (number: builtins.int, text: builtins.str)\n"
12831288
f"Runtime: at line 1 in file {TEST_MODULE_NAME}.py\ndef (num, text)\n\n"
12841289
"Found 1 error (checked 1 module)\n"
12851290
)
@@ -1437,7 +1442,7 @@ def test_config_file(self) -> None:
14371442
output = run_stubtest(stub=stub, runtime=runtime, options=[])
14381443
assert remove_color_code(output) == (
14391444
f"error: {TEST_MODULE_NAME}.temp variable differs from runtime type Literal[5]\n"
1440-
"Stub: at line 2\n_decimal.Decimal\nRuntime:\n5\n\n"
1445+
f"Stub: at line 2 in file {TEST_MODULE_NAME}.pyi\n_decimal.Decimal\nRuntime:\n5\n\n"
14411446
"Found 1 error (checked 1 module)\n"
14421447
)
14431448
output = run_stubtest(stub=stub, runtime=runtime, options=[], config_file=config_file)

0 commit comments

Comments
 (0)