Skip to content

Commit 4db811a

Browse files
chore: change ruff parsing to JSON + relax ruff version (#156)
1 parent b71c6c1 commit 4db811a

File tree

3 files changed

+52
-48
lines changed

3 files changed

+52
-48
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ dependencies = [
4949
"scipy>=1.11.0",
5050
"sqlfluff==3.2.0",
5151
"tiktoken>=0.8.0",
52-
"ruff==0.12.3",
52+
"ruff>=0.14.10",
5353
]
5454

5555
[project.scripts]

src/data_designer/engine/validators/python.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import ast
5+
import json
56
import logging
6-
import re
77
import subprocess
88
import tempfile
99
from collections import defaultdict
@@ -179,50 +179,53 @@ def _run_linter(codebase_path: str) -> dict[str, PythonLinterMessages]:
179179
for file in Path(codebase_path).glob("*.py"):
180180
processed[file.stem] = PythonLinterMessages()
181181

182-
# Run ruff linter
182+
# Run ruff linter with JSON output
183183
ruff_bin = find_ruff_bin()
184-
env = {"NO_COLOR": "1"}
185184

186185
ruff_exec = subprocess.run(
187186
[
188187
ruff_bin,
189188
"check",
190189
"--select",
191190
"E,F6,F7,F8,SIM,PLC,PLE,PLR,PLW",
191+
"--output-format=json",
192192
codebase_path,
193193
],
194-
env=env,
195194
text=True,
196195
capture_output=True,
197196
check=False,
198197
cwd=Path.cwd(),
199198
)
200199
ruff_output = ruff_exec.stdout
201200

202-
# Parse ruff output
203-
if "All checks passed!" in ruff_output:
204-
return processed # no errors or warnings
205-
206-
pattern = r"(.*):([0-9]*):([0-9]*): ([A-Za-z0-9]*):? (?:\[\*\] )?(.*)\n"
207-
errors = re.findall(pattern, ruff_output)
201+
# Parse JSON output
202+
try:
203+
diagnostics = json.loads(ruff_output)
204+
except json.JSONDecodeError as e:
205+
raise RuntimeError(f"Failed to parse ruff JSON output: {e}")
208206

209-
if errors == []: # output could not be parsed
210-
raise RuntimeError("ruff's output could not be parsed")
207+
if not diagnostics:
208+
return processed # no errors or warnings
211209

212-
try:
213-
for error in errors:
214-
filename, line, column, symbol, message = error
215-
processed[Path(filename).stem].add(
216-
PythonLinterMessage(
217-
type=TYPE_FROM_SYMBOL[re.sub(r"[^A-Za-z]+", "", symbol)],
218-
symbol=symbol,
219-
line=int(line),
220-
column=int(column),
221-
message=message,
222-
)
210+
for diagnostic in diagnostics:
211+
filename = diagnostic["filename"]
212+
code = diagnostic["code"]
213+
location = diagnostic["location"]
214+
message = diagnostic["message"]
215+
216+
# Extract alphabetic prefix from code for type mapping
217+
alpha_prefix = "".join(c for c in code if c.isalpha())
218+
error_type = TYPE_FROM_SYMBOL.get(alpha_prefix, "warning")
219+
220+
processed[Path(filename).stem].add(
221+
PythonLinterMessage(
222+
type=error_type,
223+
symbol=code,
224+
line=location["row"],
225+
column=location["column"],
226+
message=message,
223227
)
224-
except Exception: # output not in expected format
225-
raise RuntimeError("ruff's output not in expected format")
228+
)
226229

227230
return processed
228231

uv.lock

Lines changed: 23 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)