Skip to content

Commit 741779a

Browse files
committed
🤖 ruff format the testgen output before writing it
1 parent 63e9d30 commit 741779a

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

‎tool/_ruff.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import subprocess
2+
from pathlib import Path
3+
4+
# see https://github.com/astral-sh/ruff/pull/18224
5+
from ruff.__main__ import ( # type: ignore[import-untyped] # pyright: ignore[reportMissingTypeStubs]
6+
find_ruff_bin, # noqa: PLC2701
7+
)
8+
9+
10+
def ruff_format(source: str) -> str:
11+
result = subprocess.run(
12+
[find_ruff_bin(), "format", "-"],
13+
input=source,
14+
text=True,
15+
capture_output=True,
16+
check=True,
17+
cwd=Path.cwd(),
18+
)
19+
result.check_returncode()
20+
return result.stdout

‎tool/testgen.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
sys.path.insert(0, str(Path(__file__).parent.parent))
3838

39+
from tool._ruff import ruff_format
3940
from tool.promotion import _typename as dtype_label
4041
from tool.ufunc import _all_types as ufunc_signatures
4142

@@ -249,13 +250,7 @@ def _array_expr(
249250

250251
expr2 = _array_expr_single(*(dts[1] for dts in dtypess), ndim=ndim, npt=npt)
251252

252-
expr = f"tuple[{expr1}, {expr2}]"
253-
if "\n" in expr or "|" in expr:
254-
expr1 = textwrap.indent(expr1, TAB)
255-
expr2 = textwrap.indent(expr2, TAB)
256-
expr = f"tuple[\n{expr1},\n{expr2},\n]"
257-
258-
return expr
253+
return f"tuple[{expr1}, {expr2}]"
259254

260255

261256
def __group_types(*types: str) -> tuple[dict[str, list[str]], list[str]]:
@@ -472,11 +467,7 @@ def build(self) -> str:
472467

473468
lines.append(line)
474469

475-
# ensure trailing newline
476-
if not n_empty:
477-
lines.append("")
478-
479-
return BR.join(lines)
470+
return ruff_format(BR.join(lines))
480471

481472
@final
482473
def _read(self, /) -> str | None:

0 commit comments

Comments
 (0)