Skip to content

Commit 4f4ac27

Browse files
authored
chore(api): surface pyrefly output on type-check failures (langgenius#37934)
1 parent 3aa26fb commit 4f4ac27

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

api/libs/pyrefly_diagnostics.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from __future__ import annotations
44

5+
import argparse
56
import sys
67

7-
_DIAGNOSTIC_PREFIXES = ("ERROR ", "WARNING ")
8+
_DIAGNOSTIC_PREFIXES = ("ERROR ", "WARN ", "WARNING ")
89
_LOCATION_PREFIX = "-->"
910

1011

@@ -13,7 +14,7 @@ def extract_diagnostics(raw_output: str) -> str:
1314
1415
The full pyrefly output includes code excerpts and carets, which create noisy
1516
diffs. This helper keeps only:
16-
- diagnostic headline lines (``ERROR ...`` / ``WARNING ...``)
17+
- diagnostic headline lines (``ERROR ...`` / ``WARN ...`` / ``WARNING ...``)
1718
- the following location line (``--> path:line:column``), when present
1819
"""
1920

@@ -36,11 +37,28 @@ def extract_diagnostics(raw_output: str) -> str:
3637
return "\n".join(diagnostics) + "\n"
3738

3839

40+
def render_diagnostics(raw_output: str, exit_code: int) -> str:
41+
"""Render concise diagnostics and fall back to raw output on unmatched failures."""
42+
43+
diagnostics = extract_diagnostics(raw_output)
44+
if diagnostics:
45+
return diagnostics
46+
47+
if exit_code != 0:
48+
return raw_output
49+
50+
return ""
51+
52+
3953
def main() -> int:
4054
"""Read pyrefly output from stdin and print normalized diagnostics."""
4155

56+
parser = argparse.ArgumentParser()
57+
parser.add_argument("--status", type=int, default=0)
58+
args = parser.parse_args()
59+
4260
raw_output = sys.stdin.read()
43-
sys.stdout.write(extract_diagnostics(raw_output))
61+
sys.stdout.write(render_diagnostics(raw_output, exit_code=args.status))
4462
return 0
4563

4664

api/tests/unit_tests/libs/test_pyrefly_diagnostics.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from libs.pyrefly_diagnostics import extract_diagnostics
1+
from libs.pyrefly_diagnostics import extract_diagnostics, render_diagnostics
22

33

44
def test_extract_diagnostics_keeps_only_summary_and_location_lines() -> None:
@@ -40,6 +40,37 @@ def test_extract_diagnostics_handles_error_without_location_line() -> None:
4040
assert diagnostics == "ERROR unexpected pyrefly output format [bad-format]\n"
4141

4242

43+
def test_extract_diagnostics_keeps_warn_headlines_and_location_lines() -> None:
44+
# Arrange
45+
raw_output = """INFO Checking project configured at `/tmp/project/pyrefly.toml`
46+
WARN Skipping include pattern `/tmp/project/tests` because it is matched by `project-excludes`.
47+
--> tests/test_containers_integration_tests/pyrefly.toml:3:1
48+
"""
49+
50+
# Act
51+
diagnostics = extract_diagnostics(raw_output)
52+
53+
# Assert
54+
assert diagnostics == (
55+
"WARN Skipping include pattern `/tmp/project/tests` because it is matched by `project-excludes`.\n"
56+
" --> tests/test_containers_integration_tests/pyrefly.toml:3:1\n"
57+
)
58+
59+
60+
def test_render_diagnostics_falls_back_to_raw_output_for_nonzero_exit_without_matches() -> None:
61+
# Arrange
62+
raw_output = (
63+
"INFO Checking project configured at `/tmp/project/pyrefly.toml`\n"
64+
"No Python files matched pattern `/tmp/project/tests/test_containers_integration_tests`\n"
65+
)
66+
67+
# Act
68+
diagnostics = render_diagnostics(raw_output, exit_code=1)
69+
70+
# Assert
71+
assert diagnostics == raw_output
72+
73+
4374
def test_extract_diagnostics_returns_empty_for_non_error_output() -> None:
4475
# Arrange
4576
raw_output = "INFO Checking project configured at `/tmp/project/pyrefly.toml`\n"

dev/pyrefly-check-local

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ run_pyrefly() {
4444
local pyrefly_status=$?
4545
set -e
4646

47-
uv run --directory api python libs/pyrefly_diagnostics.py < "$tmp_output"
47+
uv run --directory api python libs/pyrefly_diagnostics.py --status "$pyrefly_status" < "$tmp_output"
4848
rm -f "$tmp_output"
4949
return "$pyrefly_status"
5050
}

0 commit comments

Comments
 (0)