Skip to content

Commit 62d8709

Browse files
authored
[mypyc] Display IR on annotate test failure (#18835)
This makes it easier to figure out why a test is failing. Example output on failure: ``` =================================== FAILURES ==================================== ________________________ testAnnotateTwoOperationsOnLine ________________________ data: /Users/jukka/src/mypy/mypyc/test-data/annotate-basic.test:18: Failed: Invalid source code output (/Users/jukka/src/mypy/mypyc/test-data/annotate-basic.test, line 18) ----------------------------- Captured stdout call ------------------------------ Generated IR: def f(x): x :: object r0 :: str r1, r2, r3 :: object L0: r0 = 'foo' r1 = CPyObject_GetAttr(x, r0) r2 = object 1 r3 = PyNumber_Add(r1, r2) return r3 ----------------------------- Captured stderr call ------------------------------ Expected: main:2: Get non-native attribute "foo". Generic "+" operation.x (diff) Actual: main:2: Get non-native attribute "foo". Generic "+" operation. (diff) Alignment of first line difference: E: ...Generic "+" operation.x A: ...Generic "+" operation. ^ Update the test output using --update-data (implies -n0; you can additionally use the -k selector to update only specific tests) ```
1 parent af96893 commit 62d8709

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Diff for: mypyc/test/test_annotate.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from mypy.test.config import test_temp_dir
99
from mypy.test.data import DataDrivenTestCase
1010
from mypyc.annotate import generate_annotations
11+
from mypyc.ir.pprint import format_func
1112
from mypyc.test.testutil import (
1213
ICODE_GEN_BUILTINS,
1314
MypycDataSuite,
@@ -39,8 +40,9 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
3940
for i, line in enumerate(testcase.input):
4041
if "# A:" in line:
4142
msg = line.rpartition("# A:")[2].strip()
42-
expected_output.append(f"{i + 1}: {msg}")
43+
expected_output.append(f"main:{i + 1}: {msg}")
4344

45+
ir = None
4446
try:
4547
ir, tree = build_ir_for_single_file2(testcase.input, options)
4648
except CompileError as e:
@@ -50,6 +52,16 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
5052
actual = []
5153
for line_num, line_anns in annotations.annotations.items():
5254
s = " ".join(line_anns)
53-
actual.append(f"{line_num}: {s}")
55+
actual.append(f"main:{line_num}: {s}")
5456

55-
assert_test_output(testcase, actual, "Invalid source code output", expected_output)
57+
try:
58+
assert_test_output(testcase, actual, "Invalid source code output", expected_output)
59+
except BaseException:
60+
if ir:
61+
print("Generated IR:\n")
62+
for fn in ir.functions:
63+
if fn.name == "__top_level__":
64+
continue
65+
for s in format_func(fn):
66+
print(s)
67+
raise

0 commit comments

Comments
 (0)