Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

pre-commit: format with ruff-unsloth instead of three passes around stock ruff (#14993)

Daniel Hanec6e383f13c6
diff --git a/.github/workflows/lint-ci.yml b/.github/workflows/lint-ci.yml
index 09fbb51aa2e..e3812e94970 100644
--- a/.github/workflows/lint-ci.yml
+++ b/.github/workflows/lint-ci.yml
@@ -515,9 +515,9 @@ jobs:
- name: ruff format drift (informational)
# The canonical formatter is scripts/run_ruff_format.py
- # = ruff format + scripts/enforce_kwargs_spacing.py, so plain
- # `ruff format --check` reports the kwarg-spacing diff as
- # drift. Surface the count for visibility but keep
+ # = ruff-unsloth format + scripts/enforce_kwargs_spacing.py
+ # --residual, so plain `ruff format --check` reports the
+ # kwarg-spacing diff as drift. Surface the count for visibility but keep
# non-blocking until the custom pipeline is wired in here.
continue-on-error: true
run: |
diff --git a/.github/workflows/studio-backend-ci.yml b/.github/workflows/studio-backend-ci.yml
index f53b3f4727e..28d0aac78a9 100644
--- a/.github/workflows/studio-backend-ci.yml
+++ b/.github/workflows/studio-backend-ci.yml
@@ -536,15 +536,16 @@ jobs:
done
pip install -e . --no-deps
- # Ruff version read from .pre-commit-config.yaml so test_formatter_fixed_point.py does not skip.
- - name: Install the pinned ruff (formatter fixed-point guard)
+ # ruff-unsloth wheels read from .pre-commit-config.yaml so test_formatter_fixed_point.py
+ # does not skip. pip keeps the one whose marker matches this runner and checks its sha256.
+ - name: Install the pinned ruff-unsloth (formatter fixed-point guard)
run: |
- pin="$(python -c 'import sys; sys.path.insert(0, "scripts"); import run_ruff_format as r; print(r.pinned_ruff_version(r.CONFIG.read_text(encoding = "utf-8")) or "")')"
- if [ -z "$pin" ]; then
- echo "::error::.pre-commit-config.yaml no longer pins a ruff for ruff-format-with-kwargs"
+ python -c 'import sys; sys.path.insert(0, "scripts"); import run_ruff_format as r; print("\n".join(r.pinned_requirements(r.CONFIG.read_text(encoding = "utf-8"))))' > "$RUNNER_TEMP/ruff-unsloth.txt"
+ if [ ! -s "$RUNNER_TEMP/ruff-unsloth.txt" ]; then
+ echo "::error::.pre-commit-config.yaml no longer pins ruff-unsloth for ruff-format-with-kwargs"
exit 1
fi
- pip install "ruff==$pin"
+ pip install -r "$RUNNER_TEMP/ruff-unsloth.txt"
- name: Repo tests (CPU, auto-discovered)
env:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 513242d4f13..7065f73d437 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -35,12 +35,26 @@ repos:
# half-process files ruff itself skips (which produced churn). vendor/ is
# repeated here because this hook does not read ruff's config.
exclude: '(chat_templates|ollama_template_mappers|_auto_install|mapper)\.py$|^studio/backend/vendor/|^unsloth/_vendor/'
+ # ruff-unsloth is ruff 0.6.9 patched to write this repo's style itself:
+ # `name = value` kwargs and defaults (measured as `=`, so lines break where
+ # stock 0.6.9 breaks them), the def-signature and short-assert magic-comma
+ # rules, and no blank line after the imports of a short nested block.
+ # Source: https://github.com/unslothai/ruff (branch unsloth-0.6.9, see
+ # UNSLOTH.md). Changing one of those rules means a new release there.
+ # One wheel per platform, picked by the marker and checked by its sha256.
+ # No marker tells musl from glibc, so on Alpine pip rejects the manylinux
+ # wheel; the release also has a musllinux one to install by hand. Other
+ # platforms install nothing and the script refuses with an install hint.
additional_dependencies:
- - ruff==0.6.9
+ - "ruff-unsloth @ https://github.com/unslothai/ruff/releases/download/v0.6.9-unsloth.1/ruff_unsloth-0.6.9.post1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl#sha256=866eb6575a00bc6ddeb37dc67ed4cd469e7250a3d118c2e4533b1d99be48fb9a ; sys_platform == 'linux' and platform_machine == 'x86_64'"
+ - "ruff-unsloth @ https://github.com/unslothai/ruff/releases/download/v0.6.9-unsloth.1/ruff_unsloth-0.6.9.post1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl#sha256=b375c841533dc0a5f8974915995970cb101ed9ce7af414c672a17c183310e3cc ; sys_platform == 'linux' and platform_machine == 'aarch64'"
+ - "ruff-unsloth @ https://github.com/unslothai/ruff/releases/download/v0.6.9-unsloth.1/ruff_unsloth-0.6.9.post1-py3-none-macosx_11_0_arm64.whl#sha256=5de72e6a4a22007a4d831785dabd6c362e30ec5ab90304e43d8831ae72770776 ; sys_platform == 'darwin' and platform_machine == 'arm64'"
+ - "ruff-unsloth @ https://github.com/unslothai/ruff/releases/download/v0.6.9-unsloth.1/ruff_unsloth-0.6.9.post1-py3-none-macosx_10_12_x86_64.whl#sha256=bd14fc7bc056a45804675b408480f0b42f8dfd4dc127213e9eb53fa42b5d7d33 ; sys_platform == 'darwin' and platform_machine == 'x86_64'"
+ - "ruff-unsloth @ https://github.com/unslothai/ruff/releases/download/v0.6.9-unsloth.1/ruff_unsloth-0.6.9.post1-py3-none-win_amd64.whl#sha256=0847fc1afffa899c918e162b93d04ff985fba4bf8fc7c5577608068d1fd95ebd ; sys_platform == 'win32' and platform_machine == 'AMD64'"
# Serial: pre-commit used to start a partition per CPU, each with its own
# multithreaded ruff, nproc x nproc threads on pre-commit.ci (#14174). A
# long file list is still split into calls that run one after another;
- # each runs one ruff, and the script spreads the Python passes around it
+ # each runs one ruff, and the script spreads the Python pass after it
# over a bounded pool.
require_serial: true
# Re-pins allowScripts entries after dependency bumps. pre-commit.ci
diff --git a/scripts/enforce_kwargs_spacing.py b/scripts/enforce_kwargs_spacing.py
index 754c7b9d106..4cf42f6690a 100755
--- a/scripts/enforce_kwargs_spacing.py
+++ b/scripts/enforce_kwargs_spacing.py
@@ -10,6 +10,11 @@ The fast engine shares one AST per file stage and tokenizes only headers,
short assert spans, and f-strings. A sparse lexer handles the rest. Compatibility
routines preserve the reference's behavior on malformed or unusual source.
There is no dependency on ruff's canonical layout and no persistent cache.
+
+The pre-commit hook formats with ruff-unsloth, which writes the spacing, the
+magic-comma rules and the import blank lines itself, and then runs only
+``--residual``: string merging and redundant-pass removal, on the files that
+could need them. The full passes stay for running this script on its own.
"""
from __future__ import annotations
@@ -44,7 +49,7 @@ def _atomic_write_text(path: Path, data: str, encoding: str) -> None:
mode = stat.S_IMODE(os.stat(path).st_mode)
except OSError:
mode = None
- fd, tmp_path = tempfile.mkstemp(prefix=".kwargs_fix.", dir=dirpath)
+ fd, tmp_path = tempfile.mkstemp(prefix = ".kwargs_fix.", dir = dirpath)
try:
# newline = "\n", not the default None. Default translates every "\n" to os.linesep,
# which on Windows is "\r\n", and the read side (tokenize.open, below) has already
@@ -53,7 +58,7 @@ def _atomic_write_text(path: Path, data: str, encoding: str) -> None:
# pre-commit hook got a whole-file diff on everything they edited. "\n" rather than ""
# because it states the intended ending rather than "do not translate", and it holds even
# if the read side stops normalising.
- with os.fdopen(fd, "w", encoding=encoding, newline="\n") as handle:
+ with os.fdopen(fd, "w", encoding = encoding, newline = "\n") as handle:
handle.write(data)
handle.flush()
os.fsync(handle.fileno())
@@ -196,7 +201,7 @@ def _fits_one_line(segment):
def _apply(lines, edits):
- for row, c0, c1, replacement in sorted(edits, key=lambda e: (e[0], e[1]), reverse=True):
+ for row, c0, c1, replacement in sorted(edits, key = lambda e: (e[0], e[1]), reverse = True):
line = lines[row - 1]
lines[row - 1] = line[:c0] + replacement + line[c1:]
@@ -260,7 +265,11 @@ def _assert_edits(lines, nodes):
return edits
-def _pre(text, definitions=True, assertions=True):
+def _pre(
+ text,
+ definitions = True,
+ assertions = True,
+):
if not _ordinary(text):
return _reference_pre(text, definitions, assertions)
if not ((definitions and "def" in text) or (assertions and "assert" in text)):
@@ -500,7 +509,7 @@ def _merge_edits(text, lines, starts, strings, insertions, nodes):
enclosing = [
n for n in nodes if isinstance(n, ast.stmt) and n.lineno <= row <= n.end_lineno
]
- stmt = min(enclosing, key=lambda n: n.end_lineno - n.lineno, default=None)
+ stmt = min(enclosing, key = lambda n: n.end_lineno - n.lineno, default = None)
if isinstance(stmt, ast.Assert) and stmt.lineno != stmt.end_lineno:
segment = lines[stmt.lineno - 1 : stmt.end_lineno]
segment[row - stmt.lineno] = lines[row - 1][:c0] + merged + lines[row - 1][c1:]
@@ -515,8 +524,8 @@ def _merge_edits(text, lines, starts, strings, insertions, nodes):
# inequality is NOT proof of file-level inequality (two changes in a
# larger concatenation may cancel). Defer that case to the
# reference's all-or-nothing global guard.
- before = ast.parse("(" + original + ")", mode="eval")
- after = ast.parse("(" + merged + ")", mode="eval")
+ before = ast.parse("(" + original + ")", mode = "eval")
+ after = ast.parse("(" + merged + ")", mode = "eval")
if ast.dump(before) != ast.dump(after):
return None
except SyntaxError:
@@ -529,7 +538,13 @@ def _merge_edits(text, lines, starts, strings, insertions, nodes):
return edits
-def _post(text, spacing=True, blanks=True, merging=True, passes=True):
+def _post(
+ text,
+ spacing = True,
+ blanks = True,
+ merging = True,
+ passes = True,
+):
if not _ordinary(text):
return _reference_post(text, spacing, blanks, merging, passes)
tree = _tree(text)
@@ -581,7 +596,7 @@ def _post(text, spacing=True, blanks=True, merging=True, passes=True):
end += merge_shift
pass_positions.append((row, start, end))
_apply(lines, edits)
- for row, start, end in sorted(pass_positions, reverse=True):
+ for row, start, end in sorted(pass_positions, reverse = True):
line = lines[row]
segment = line[:start] + line[end:]
lines[row] = segment if segment.strip() else ""
@@ -614,48 +629,136 @@ def _reference_post(text, spacing, blanks, merging, passes):
@_without_gc
def normalize_def_trailing_comma(text: str) -> tuple[str, bool]:
- return _pre(text, assertions=False)
+ return _pre(text, assertions = False)
@_without_gc
def collapse_short_asserts(text: str) -> tuple[str, bool]:
- return _pre(text, definitions=False)
+ return _pre(text, definitions = False)
@_without_gc
def enforce_spacing(text: str) -> tuple[str, bool]:
- return _post(text, blanks=False, merging=False, passes=False)
+ return _post(text, blanks = False, merging = False, passes = False)
@_without_gc
def remove_blank_after_short_import(text: str) -> tuple[str, bool]:
if "import" not in text:
return text, False
- return _post(text, spacing=False, merging=False, passes=False)
+ return _post(text, spacing = False, merging = False, passes = False)
@_without_gc
def merge_adjacent_string_literals(text: str) -> tuple[str, bool]:
- return _post(text, spacing=False, blanks=False, passes=False)
+ return _post(text, spacing = False, blanks = False, passes = False)
@_without_gc
def remove_redundant_passes(text: str) -> tuple[str, bool]:
if "pass" not in text:
return text, False
- return _post(text, spacing=False, blanks=False, merging=False)
+ return _post(text, spacing = False, blanks = False, merging = False)
+
+
+# A closing quote, blanks, then an opening one: every same-line implicit concatenation
+# matches, and so does `" ".join`. The lexer below settles which it is.
+_ADJACENT_QUOTES = re.compile(r"""['"][ \t]+(?:[rRuUbBfF]{1,2})?['"]""")
+_PASS_ROW = re.compile(r"^([ \t]*)pass[ \t]*(?:#.*)?$", re.MULTILINE)
+
+
+def _may_merge(text):
+ """Whether a same-line run of adjacent literals is one _merge_string_run would fold."""
+ if not _ADJACENT_QUOTES.search(text):
+ return False
+ try:
+ _, strings = _lex(text)
+ except Exception:
+ # The lexer assumes valid source; anything it cannot read gets the full pass.
+ return True
+ # Runs built as _merge_edits builds them: single-row pieces, blanks between.
+ run = []
+ for piece in strings + [None]:
+ if (
+ piece is not None
+ and run
+ and "\n" not in text[run[-1][1] : piece[2]]
+ and not text[run[-1][2] : piece[1]].strip(" \t")
+ ):
+ run.append(piece)
+ continue
+ if len(run) > 1 and _merge_string_run([(k, text[a:b]) for k, a, b in run]) is not None:
+ return True
+ run = [] if piece is None else [piece]
+ return False
+
+
+def _may_have_redundant_pass(text):
+ """Whether some `pass` can share its suite with another statement.
+
+ Over-approximates on formatted source. A sibling before the `pass` ends on a row
+ indented at least as deep (its last line, a closing bracket, a compound body) or on
+ the row that closes a triple-quoted string; a sibling after it starts at the same
+ indent. Comment and blank rows are skipped, and a backslash continuation anywhere
+ sends the file to the full pass.
+ """
+ if "pass" not in text:
+ return False
+ if "\\\n" in text:
+ return True
+ lines = text.split("\n")
+ rows = [m for m in _PASS_ROW.finditer(text)]
+ if not rows:
+ return False
+ starts = _line_starts([line + "\n" for line in lines])
+
+ def significant(row):
+ stripped = lines[row].strip()
+ return stripped and not stripped.startswith("#")
+
+ for match in rows:
+ row = bisect.bisect_right(starts, match.start()) - 1
+ own = len(match.group(1))
+ before = next((r for r in range(row - 1, -1, -1) if significant(r)), None)
+ if before is not None:
+ line = lines[before]
+ if len(line) - len(line.lstrip()) >= own or '"""' in line or "'''" in line:
+ return True
+ after = next((r for r in range(row + 1, len(lines)) if significant(r)), None)
+ if after is not None and len(lines[after]) - len(lines[after].lstrip()) == own:
+ return True
+ return False
@_without_gc
-def process_file(path: Path, pre: bool = False) -> bool:
+def residual(text: str) -> tuple[str, bool]:
+ """The two passes ruff-unsloth leaves to Python, run only where they could apply."""
+ if not _ordinary(text):
+ return _post(text, spacing = False, blanks = False)
+ merging = _may_merge(text)
+ passes = _may_have_redundant_pass(text)
+ if not (merging or passes):
+ return text, False
+ return _post(text, spacing = False, blanks = False, merging = merging, passes = passes)
+
+
+@_without_gc
+def process_file(
+ path: Path,
+ pre: bool = False,
+ only_residual: bool = False,
+) -> bool:
try:
with tokenize.open(path) as handle:
original = handle.read()
encoding = handle.encoding
except (OSError, SyntaxError) as exc:
- print(f"Failed to read {path}: {exc}", file=sys.stderr)
+ print(f"Failed to read {path}: {exc}", file = sys.stderr)
return False
- updated, changed = _pre(original) if pre else _post(original)
+ if only_residual:
+ updated, changed = residual(original)
+ else:
+ updated, changed = _pre(original) if pre else _post(original)
if changed:
_atomic_write_text(path, updated, encoding)
return changed
@@ -667,7 +770,7 @@ def process_file(path: Path, pre: bool = False) -> bool:
def _reference_enforce_spacing(text: str) -> tuple[str, bool]:
"""Return updated text with keyword '=' padded by spaces, plus change flag."""
- lines = text.splitlines(keepends=True)
+ lines = text.splitlines(keepends = True)
if not lines:
return text, False
@@ -745,10 +848,10 @@ def _reference_remove_redundant_passes(text: str) -> tuple[str, bool]:
if not redundant:
return text, False
- lines = text.splitlines(keepends=True)
+ lines = text.splitlines(keepends = True)
changed = False
- for node in sorted(redundant, key=lambda item: (item.lineno, item.col_offset), reverse=True):
+ for node in sorted(redundant, key = lambda item: (item.lineno, item.col_offset), reverse = True):
start = node.lineno - 1
end = (node.end_lineno or node.lineno) - 1
if start >= len(lines):
@@ -789,7 +892,7 @@ def _reference_remove_blank_after_short_import(text: str) -> tuple[str, bool]:
except SyntaxError:
return text, False
- lines = text.splitlines(keepends=True)
+ lines = text.splitlines(keepends = True)
import_types = (ast.Import, ast.ImportFrom)
drop: set[int] = set() # 1-based physical line numbers to delete
@@ -825,7 +928,7 @@ def _reference_remove_blank_after_short_import(text: str) -> tuple[str, bool]:
if not drop:
return text, False
- kept = [ln for idx, ln in enumerate(lines, start=1) if idx not in drop]
+ kept = [ln for idx, ln in enumerate(lines, start = 1) if idx not in drop]
return "".join(kept), True
@@ -908,8 +1011,8 @@ def _reference_normalize_def_trailing_comma(text: str) -> tuple[str, bool]:
if not edits:
return text, False
- lines = text.splitlines(keepends=True)
- for row, col, kind in sorted(edits, reverse=True):
+ lines = text.splitlines(keepends = True)
+ for row, col, kind in sorted(edits, reverse = True):
ln = lines[row - 1]
if kind == "del":
if col < len(ln) and ln[col] == ",":
@@ -1061,7 +1164,7 @@ def _reference_merge_adjacent_string_literals(text: str) -> tuple[str, bool]:
except (tokenize.TokenError, IndentationError, SyntaxError):
return text, False
- lines = text.splitlines(keepends=True)
+ lines = text.splitlines(keepends = True)
pieces = _string_pieces(toks, lines)
runs: list[list[tuple[str, tuple[int, int], tuple[int, int], str]]] = []
@@ -1097,7 +1200,7 @@ def _reference_merge_adjacent_string_literals(text: str) -> tuple[str, bool]:
if not edits:
return text, False
- for row, c0, c1, repl in sorted(edits, key=lambda e: (e[0], e[1]), reverse=True):
+ for row, c0, c1, repl in sorted(edits, key = lambda e: (e[0], e[1]), reverse = True):
ln = lines[row - 1]
lines[row - 1] = ln[:c0] + repl + ln[c1:]
out = "".join(lines)
@@ -1117,7 +1220,7 @@ def _reference_collapse_short_asserts(text: str) -> tuple[str, bool]:
except (tokenize.TokenError, IndentationError, SyntaxError):
return text, False
- lines = text.splitlines(keepends=True)
+ lines = text.splitlines(keepends = True)
multiline = [
(n.lineno, n.end_lineno)
for n in ast.walk(tree)
@@ -1159,7 +1262,7 @@ def _reference_collapse_short_asserts(text: str) -> tuple[str, bool]:
changed = False
for positions in by_target.values(): # apply per assert; skip any that break AST
trial = working[:]
- for row, col in sorted(positions, reverse=True):
+ for row, col in sorted(positions, reverse = True):
ln = trial[row - 1]
if col < len(ln) and ln[col] == ",":
trial[row - 1] = ln[:col] + ln[col + 1 :]
@@ -1172,12 +1275,17 @@ def _reference_collapse_short_asserts(text: str) -> tuple[str, bool]:
def main(argv: list[str]) -> int:
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument("files", nargs="+", help="Python files to fix")
+ parser = argparse.ArgumentParser(description = __doc__)
+ parser.add_argument("files", nargs = "+", help = "Python files to fix")
parser.add_argument(
"--pre",
- action="store_true",
- help="pre-ruff pass: normalize def-signature commas + collapse short multi-line asserts",
+ action = "store_true",
+ help = "pre-ruff pass: normalize def-signature commas + collapse short multi-line asserts",
+ )
+ parser.add_argument(
+ "--residual",
+ action = "store_true",
+ help = "after ruff-unsloth: only string merging and redundant-pass removal",
)
args = parser.parse_args(argv)
@@ -1191,7 +1299,7 @@ def main(argv: list[str]) -> int:
continue
if not path.exists() or path.is_dir():
continue
- if process_file(path, pre=args.pre):
+ if process_file(path, pre = args.pre, only_residual = args.residual):
touched.append(path)
if touched:
diff --git a/scripts/run_ruff_format.py b/scripts/run_ruff_format.py
index 9bf0dd57b09..43115d3464c 100755
--- a/scripts/run_ruff_format.py
+++ b/scripts/run_ruff_format.py
@@ -1,7 +1,11 @@
#!/usr/bin/env python3
-"""Run a pre-pass (normalize def-signature magic commas + collapse short
-multi-line asserts), then `ruff format`, then the kwarg-spacing / import /
-string-merge post-pass."""
+"""Run `ruff-unsloth format`, then the string-merge / redundant-pass post-pass.
+
+ruff-unsloth (https://github.com/unslothai/ruff) is ruff 0.6.9 patched to write the
+repo's kwarg spacing, def-signature and short-assert magic-comma rules and the
+nested-import blank-line rule itself, so the only Python left is
+scripts/enforce_kwargs_spacing.py --residual, which skips every file that cannot
+need it."""
from __future__ import annotations
@@ -18,60 +22,76 @@ CONFIG = HERE.parent / ".pre-commit-config.yaml"
# made under it will be reformatted by the hook and fail pre-commit.
ANY_VERSION_ENV = "UNSLOTH_RUFF_FORMAT_ANY_VERSION"
USAGE = "usage: run_ruff_format.py FILE [FILE ...] (formats in place; no options)"
-# Worker count for the Python passes either side of ruff. The hook is require_serial
-# (#14174), so these chunks are the only parallelism; ruff itself runs once.
+# Worker count for the Python pass after ruff. The hook is require_serial (#14174), so
+# these chunks are the only parallelism; ruff itself runs once.
JOBS_ENV = "UNSLOTH_RUFF_FORMAT_JOBS"
_MAX_JOBS = 8
# Below this many files per worker an extra interpreter costs more than it saves.
_FILES_PER_JOB = 32
-# `- ruff==0.6.9` under the hook's additional_dependencies. Read out of the config
-# rather than copied here, because a second copy of the pin is a second thing to
-# forget; a regex rather than yaml.safe_load because this hook installs ruff and
-# nothing else, and adding PyYAML to run a version check would be the tail wagging
-# the dog.
-_PIN_RE = re.compile(r"^\s*-\s*ruff\s*==\s*([0-9][^\s#]*)\s*(?:#.*)?$", re.MULTILINE)
-_VERSION_RE = re.compile(r"^ruff\s+([0-9][^\s]*)")
+DISTRIBUTION = "ruff-unsloth"
+MODULE = "ruff_unsloth"
+# The ruff-unsloth wheels under the hook's additional_dependencies, one per platform:
+# `- "ruff-unsloth @ https://.../ruff_unsloth-0.6.9.post1-py3-none-<tag>.whl#sha256=... ; <marker>"`.
+# `ruff-unsloth==0.6.9.post1` is read too. The version comes out of the config rather
+# than a copy here, because a second copy of the pin is a second thing to forget; a
+# regex rather than yaml.safe_load because this hook installs ruff-unsloth and nothing
+# else, and adding PyYAML to run a version check would be the tail wagging the dog.
+_REQUIREMENT_RE = re.compile(
+ r"""^\s*-\s*(["']?)(ruff[-_]unsloth\b.*?)\1\s*(?:#.*)?$""", re.MULTILINE
+)
+_WHEEL_VERSION_RE = re.compile(r"ruff_unsloth-([0-9][^-\s]*)-py3")
+_EXACT_VERSION_RE = re.compile(r"^ruff[-_]unsloth\s*==\s*([0-9][^\s;'\"]*)")
+
+
+def pinned_requirements(config_text: str) -> list[str]:
+ """The ruff-unsloth requirement lines, markers included, for installing it outside pre-commit."""
+ return [match.group(2).strip() for match in _REQUIREMENT_RE.finditer(config_text)]
def pinned_ruff_version(config_text: str) -> str | None:
- """The ruff this repo's formatting was produced with, or None if unpinned."""
- found = {match.group(1) for match in _PIN_RE.finditer(config_text)}
+ """The ruff-unsloth this repo's formatting was produced with, or None if unpinned."""
+ found = set()
+ for requirement in pinned_requirements(config_text):
+ match = _WHEEL_VERSION_RE.search(requirement) or _EXACT_VERSION_RE.match(requirement)
+ if match:
+ found.add(match.group(1))
# Two different pins is not a question this script can answer, and guessing
- # would enforce the wrong one.
+ # would enforce the wrong one. Every platform's wheel carries the same version.
return found.pop() if len(found) == 1 else None
def installed_ruff_version(python: str = sys.executable) -> str | None:
- """The ruff the format below would actually run, or None when it cannot say."""
+ """The ruff-unsloth the format below would actually run, or None when it cannot say.
+
+ Read from the distribution, not `--version`: the binary reports the ruff it is
+ built on (0.6.9), which does not change between ruff-unsloth releases.
+ """
+ probe = f"from importlib.metadata import version; print(version({DISTRIBUTION!r}))"
try:
- out = subprocess.run(
- [python, "-m", "ruff", "--version"], capture_output = True, text = True, timeout = 60
- )
+ out = subprocess.run([python, "-c", probe], capture_output = True, text = True, timeout = 60)
except (OSError, subprocess.SubprocessError):
return None
if out.returncode != 0:
return None
- match = _VERSION_RE.match(out.stdout.strip())
- return match.group(1) if match else None
+ return out.stdout.strip() or None
def ruff_unavailable_reason(python: str = sys.executable) -> str | None:
- """Why `python -m ruff` cannot run here, or None when it can.
+ """Why `python -m ruff_unsloth` cannot run here, or None when it can.
- Separate from the version question because the answers differ. A ruff that
- runs but reports a version this cannot parse is survivable; a ruff that does
- not run at all is not, and the pre-pass below has already rewritten every
- file it was given by the time `ruff format` says so.
+ Separate from the version question because the answers differ: an unreadable
+ version is survivable, a formatter that does not run at all is not.
"""
try:
out = subprocess.run(
- [python, "-m", "ruff", "--version"], capture_output = True, text = True, timeout = 60
+ [python, "-m", MODULE, "--version"], capture_output = True, text = True, timeout = 60
)
except (OSError, subprocess.SubprocessError) as exc:
return f"{type(exc).__name__}: {exc}"
if out.returncode != 0:
- return (out.stderr or out.stdout).strip() or f"`ruff --version` exited {out.returncode}"
+ message = (out.stderr or out.stdout).strip()
+ return message or f"`{MODULE} --version` exited {out.returncode}"
return None
@@ -104,8 +124,8 @@ def parse_files(argv: list[str]) -> tuple[list[str], str | None]:
message += (
"\n There is no check mode: this script always rewrites the files"
" it is given, and `ruff format --check` is not an equivalent."
- "\n It checks the middle one of three passes, so a clean ruff says"
- " nothing about the kwarg-spacing passes either side of it."
+ "\n It checks only the ruff step, so a clean ruff says nothing"
+ " about the string-merge and redundant-pass step after it."
"\n To preview a run, copy the file aside, run this script on the"
" copy, and diff the two."
)
@@ -225,60 +245,54 @@ def main(argv: list[str]) -> int:
return 2
pinned = pinned_ruff_version(CONFIG.read_text(encoding = "utf-8")) if CONFIG.exists() else None
-
- # Both checks are made before anything is rewritten, because the pre-pass is
- # itself a rewrite. Without this first one, a missing or broken ruff let the
- # pre-pass strip every magic comma it was given and only then die on `ruff
- # format`, leaving files in a shape the hook rejects -- the opposite of what
- # a full run produces, and blamed on the next person to touch them. The
- # override below is deliberately not honoured here: no ruff formats nothing.
+ install_hint = (
+ f"pip install the {DISTRIBUTION} wheel for this platform from the hook's "
+ f"additional_dependencies in {CONFIG.name} "
+ f"({DISTRIBUTION} {pinned or '<the pinned version>'}), or run the hook itself "
+ f"(pre-commit run ruff-format-with-kwargs --files ...)"
+ )
+
+ # Both checks are made before anything is rewritten. The override below is
+ # deliberately not honoured here: no ruff-unsloth formats nothing.
unavailable = ruff_unavailable_reason()
if unavailable is not None:
print(
- f"run_ruff_format: cannot run `python -m ruff` ({unavailable}).\n"
- f" Refusing before rewriting anything: the passes either side of ruff would "
- f"leave the files half-formatted.\n"
- f" Fix: pip install ruff=={pinned or '<the pin in .pre-commit-config.yaml>'}",
+ f"run_ruff_format: cannot run `python -m {MODULE}` ({unavailable}).\n"
+ f" Refusing before rewriting anything.\n"
+ f" Fix: {install_hint}",
file = sys.stderr,
)
return 1
# ruff's own formatting is not stable across releases -- 0.9 changed which
- # half of an `assert cond, "msg"` gets wrapped -- so running this with a newer
- # ruff silently produces a style the pinned hook reformats back, and the
- # commit fails pre-commit on files that are otherwise correct. It reached main
- # twice before this check existed.
+ # half of an `assert cond, "msg"` gets wrapped -- and a ruff-unsloth release can
+ # change one of the repo's rules, so running another version silently produces
+ # a style the pinned hook reformats back, and the commit fails pre-commit on
+ # files that are otherwise correct.
installed = installed_ruff_version()
if version_mismatch(pinned, installed) and not os.environ.get(ANY_VERSION_ENV):
print(
- f"run_ruff_format: this would run ruff {installed}, but the repo is formatted "
- f"with ruff {pinned} ({CONFIG.name}).\n"
+ f"run_ruff_format: this would run {DISTRIBUTION} {installed}, but the repo is "
+ f"formatted with {DISTRIBUTION} {pinned} ({CONFIG.name}).\n"
f" Their output differs, so the hook would undo this run and pre-commit would "
f"fail on files you did not break.\n"
- f" Fix: pip install ruff=={pinned}, or run the hook itself "
- f"(pre-commit run ruff-format-with-kwargs --files ...).\n"
+ f" Fix: {install_hint}.\n"
f" Override with {ANY_VERSION_ENV}=1 if you really mean it.",
file = sys.stderr,
)
return 1
- spacing_script = HERE / "enforce_kwargs_spacing.py"
- chunks = chunk_files(files, job_count(len(files)))
-
- # Pre-ruff: normalize def-signature magic commas and strip the magic comma
- # from short multi-line asserts so ruff wraps/joins accordingly.
- pre_code = run_chunks([sys.executable, str(spacing_script), "--pre"], chunks)
- if pre_code != 0:
- return pre_code
-
# One ruff over every file: it parallelises itself, and one per chunk would
# stack its thread pools the way pre-commit's partitions did (#14174).
- ruff_cmd = [sys.executable, "-m", "ruff", "format", *files]
- ruff_proc = subprocess.run(ruff_cmd)
+ ruff_proc = subprocess.run([sys.executable, "-m", MODULE, "format", *files])
if ruff_proc.returncode != 0:
return ruff_proc.returncode
- return run_chunks([sys.executable, str(spacing_script)], chunks)
+ # What ruff-unsloth does not do: merge adjacent same-line string literals and drop
+ # redundant `pass`. --residual sends only files that could need either to the AST.
+ spacing_script = HERE / "enforce_kwargs_spacing.py"
+ chunks = chunk_files(files, job_count(len(files)))
+ return run_chunks([sys.executable, str(spacing_script), "--residual"], chunks)
if __name__ == "__main__":
diff --git a/tests/studio/test_update_release_notes.py b/tests/studio/test_update_release_notes.py
index 2a85024cbd7..04da52569a4 100644
--- a/tests/studio/test_update_release_notes.py
+++ b/tests/studio/test_update_release_notes.py
@@ -1895,7 +1895,7 @@ def test_link_resolver_reads_comments_before_fences():
"""A fence delimiter inside an HTML comment is not a fence: reading it as one
left the fence open, so every link below went unresolved. The order matters
both ways, so a comment opener inside a real fence is not a comment."""
- links = LINKS.read_text(encoding="utf-8")
+ links = LINKS.read_text(encoding = "utf-8")
# Fence state is read before comments are masked, the order the collapsed preview uses.
assert "const fenceSource = inComment\n ? null\n : FENCE.exec(" in links
# Masking happens only after the in-fence early return.
@@ -1910,11 +1910,11 @@ def test_preview_heading_and_quote_markers_follow_the_backend_rule():
dropped. A blockquote marker takes at most three leading spaces, or an
indented sample holding "> - sample output" sheds its indent into the
summary."""
- src = PREVIEW.read_text(encoding="utf-8")
+ src = PREVIEW.read_text(encoding = "utf-8")
assert "const HEADING = /^#{1,6}(?:[ \\t]|$)/;" in src
assert "const HEADING_LINE = /^ {0,3}#{1,6}(?:[ \\t]|$)/;" in src
assert "const BLOCKQUOTE = /^ {0,3}>[ \\t]?/;" in src
- backend = MODULE.read_text(encoding="utf-8")
+ backend = MODULE.read_text(encoding = "utf-8")
assert "^ {0,3}(?P<hashes>#{1,6})(?:[ \\t]+(?P<title>.*?))?[ \\t]*$" in backend
@@ -1923,7 +1923,7 @@ def test_preview_collects_labels_only_from_real_definitions():
so recording its label made toPlainText strip brackets the expanded view
keeps. The pre-scan skips the same code the collector pass skips; a real
definition takes at most three spaces, so the indent test cannot reject one."""
- src = PREVIEW.read_text(encoding="utf-8")
+ src = PREVIEW.read_text(encoding = "utf-8")
scan = src.index("const labels = new Set<string>();")
collect = src.index("let deepFence: string | null = null;")
prescan = " ".join(src[scan:collect].split())
@@ -1950,15 +1950,15 @@ def test_the_download_panel_can_shrink_inside_the_capped_stack():
the fixed update card rather than the scrolling download list. Only the
shared-stack branch needs it; standalone is fixed and not a flex item."""
panel = (FRONTEND / "features/hub/download-manager/download-manager-panel.tsx").read_text(
- encoding="utf-8"
+ encoding = "utf-8"
)
assert 'positioned ? "fixed bottom-4 right-4 z-50" : "flex min-h-0 justify-end"' in panel
- provider = (FRONTEND / "app/provider.tsx").read_text(encoding="utf-8")
+ provider = (FRONTEND / "app/provider.tsx").read_text(encoding = "utf-8")
stacks = provider.count("zIndex: Z_LAYER.OVERLAY_STACK")
assert _capped_rails(provider) == stacks, "the cap this has to absorb"
[email protected](scope="module")
[email protected](scope = "module")
def run_scanner(tmp_path_factory):
"""Run the frontend's markdown scanners under node.
@@ -1970,16 +1970,16 @@ def run_scanner(tmp_path_factory):
pytest.skip("node is needed to run the TypeScript scanners")
work = tmp_path_factory.mktemp("release-notes-scanners")
for source in (PREVIEW, CODE_SPANS, LINKS, LIST_COLUMNS, INLINE_COMMENTS):
- rewritten = _TS_ALIAS.sub(r'"./\1.ts"', source.read_text(encoding="utf-8"))
- (work / source.name).write_text(rewritten, encoding="utf-8")
- (work / "run.ts").write_text(_TS_RUNNER, encoding="utf-8")
+ rewritten = _TS_ALIAS.sub(r'"./\1.ts"', source.read_text(encoding = "utf-8"))
+ (work / source.name).write_text(rewritten, encoding = "utf-8")
+ (work / "run.ts").write_text(_TS_RUNNER, encoding = "utf-8")
def run(kind: str, markdown: str):
result = subprocess.run(
[node, "--experimental-strip-types", "--no-warnings", str(work / "run.ts"), kind],
- input=markdown,
- capture_output=True,
- text=True,
+ input = markdown,
+ capture_output = True,
+ text = True,
)
if result.returncode != 0:
pytest.skip(f"node could not run the scanners: {result.stderr.strip()[:200]}")
@@ -2067,31 +2067,31 @@ def test_the_three_scanners_share_one_list_column_rule():
"""The parser and both frontend scanners must classify a line the same way;
drifting on indentation is what put a paragraph link inside a code block.
The frontend pair shares one module, ported from the backend's tracker."""
- shared = LIST_COLUMNS.read_text(encoding="utf-8")
+ shared = LIST_COLUMNS.read_text(encoding = "utf-8")
assert "export function openLists(" in shared
assert "_open_lists" in shared, "the backend function this mirrors"
for source in (PREVIEW, LINKS):
- src = source.read_text(encoding="utf-8")
+ src = source.read_text(encoding = "utf-8")
assert 'from "@/lib/markdown-list-columns"' in src
assert "openLists(" in src
- backend = MODULE.read_text(encoding="utf-8")
+ backend = MODULE.read_text(encoding = "utf-8")
assert "_indent_width(visible) - column >= 4" in backend
assert "indentWidth(structure) - column >= INDENTED_CODE_INDENT" in LINKS.read_text(
- encoding="utf-8"
+ encoding = "utf-8"
)
def test_a_failed_fetch_keeps_retry_reachable():
"""A release with no notes is ready and a failed fetch is error; only the
error is retryable, and rendering there would replace the Retry button."""
- src = " ".join(PANEL.read_text(encoding="utf-8").split())
+ src = " ".join(PANEL.read_text(encoding = "utf-8").split())
assert "const source = notes?.matched ? notes.markdown : null;" in src
# Only NotesStatus renders retry, in the else of the markdown branch: an error has no markdown.
assert "{markdown ? (" in src
assert "retry={retry}" in src
hook = " ".join(
- (FRONTEND / "hooks" / "use-release-notes.ts").read_text(encoding="utf-8").split()
+ (FRONTEND / "hooks" / "use-release-notes.ts").read_text(encoding = "utf-8").split()
)
assert (
"const failed = !next || (!next.matched && next.error !== null);" in hook
@@ -2457,7 +2457,7 @@ def test_a_comment_written_as_an_item_first_content_is_a_block(notes_module, run
],
)
def test_real_release_bodies_keep_their_announcement(notes_module, tag, kept, dropped):
- body = (BODIES / f"{tag}.md").read_text(encoding="utf-8")
+ body = (BODIES / f"{tag}.md").read_text(encoding = "utf-8")
stripped = notes_module.strip_release_body(body)
for text in kept:
assert text in stripped, f"{tag} lost {text!r}"
@@ -2573,7 +2573,7 @@ def test_a_paragraph_install_block_does_not_swallow_deeper_headings(notes_module
assert stripped == "Intro.\n\n###### Deeply nested announcement\n\n- a real change"
[email protected](autouse=True)
[email protected](autouse = True)
def github_lockout(notes_module):
from utils.prebuilt import freshness_flow
@@ -2600,17 +2600,17 @@ def test_where_the_release_notes_token_may_travel(
seen = []
- def capture(request, timeout=None):
+ def capture(request, timeout = None):
seen.append(request)
raise urllib.error.URLError("offline")
monkeypatch.setattr(notes_module.urllib.request, "urlopen", capture)
for name in ("GH_TOKEN", "GITHUB_TOKEN"):
- monkeypatch.delenv(name, raising=False)
+ monkeypatch.delenv(name, raising = False)
for name, value in env.items():
monkeypatch.setenv(name, value)
if url_override is None:
- monkeypatch.delenv(notes_module.RELEASES_URL_ENV_VAR, raising=False)
+ monkeypatch.delenv(notes_module.RELEASES_URL_ENV_VAR, raising = False)
else:
monkeypatch.setenv(notes_module.RELEASES_URL_ENV_VAR, url_override)
notes_module._fetch_latest_release()
@@ -2644,14 +2644,14 @@ def test_which_release_note_refusals_reach_the_shared_lockout(
import urllib.error
if url_override is None:
- monkeypatch.delenv(notes_module.RELEASES_URL_ENV_VAR, raising=False)
+ monkeypatch.delenv(notes_module.RELEASES_URL_ENV_VAR, raising = False)
else:
monkeypatch.setenv(notes_module.RELEASES_URL_ENV_VAR, url_override)
headers = email.message.Message()
headers["X-RateLimit-Remaining"] = remaining
headers["X-RateLimit-Reset"] = str(int(time.time() + 1800))
- def refuse(request, timeout=None):
+ def refuse(request, timeout = None):
raise urllib.error.HTTPError(request.full_url, status, "refused", headers, io.BytesIO(body))
monkeypatch.setattr(notes_module.urllib.request, "urlopen", refuse)
@@ -2667,16 +2667,16 @@ def test_the_shared_lockout_parks_only_the_github_api(
import urllib.error
if url_override is None:
- monkeypatch.delenv(notes_module.RELEASES_URL_ENV_VAR, raising=False)
+ monkeypatch.delenv(notes_module.RELEASES_URL_ENV_VAR, raising = False)
else:
monkeypatch.setenv(notes_module.RELEASES_URL_ENV_VAR, url_override)
github_lockout.hold_github_api(60)
calls = []
- def capture(request, timeout=None):
+ def capture(request, timeout = None):
calls.append(request.full_url)
raise urllib.error.URLError("offline")
monkeypatch.setattr(notes_module.urllib.request, "urlopen", capture)
- notes_module.get_latest_release(refresh=True)
+ notes_module.get_latest_release(refresh = True)
assert calls == ([] if url_override is None else [url_override])
diff --git a/tests/test_enforce_kwargs_spacing_residual.py b/tests/test_enforce_kwargs_spacing_residual.py
new file mode 100644
index 00000000000..8add5cfe5db
--- /dev/null
+++ b/tests/test_enforce_kwargs_spacing_residual.py
@@ -0,0 +1,98 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+"""--residual: the two passes ruff-unsloth leaves to Python, skipped where they cannot apply.
+
+The prefilters only decide whether a file reaches the AST; when one does, the result
+must be exactly what the full post-pass (spacing and blank lines off) would write.
+"""
+
+from __future__ import annotations
+
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+_SCRIPTS = str(Path(__file__).resolve().parent.parent / "scripts")
+if _SCRIPTS not in sys.path:
+ sys.path.insert(0, _SCRIPTS)
+
+import enforce_kwargs_spacing as K # noqa: E402
+
+# The full pass looks into a suite only when its parent suite holds two or more
+# statements, so each `pass` case sits under a module with a second statement.
+_TWO = "import os\n"
+
+# Each changes under the full post-pass, so the prefilter must let it through.
+_CHANGES = {
+ "same_line_concatenation": 'x = "a" "b"\n',
+ "plain_into_f_string": 'x = f"{y}" "tail {z}"\n',
+ "pass_after_a_statement": _TWO + "def f():\n y = 1\n pass\n",
+ "pass_before_a_statement": _TWO + "def f():\n pass\n return 1\n",
+ "pass_after_a_compound_sibling": _TWO + "def f(x):\n if x:\n y()\n pass\n",
+ "pass_after_a_closing_bracket": _TWO + "def f():\n g(\n 1,\n )\n pass\n",
+ "pass_after_a_docstring_closing_at_column_0": _TWO + 'def f():\n """doc\n"""\n pass\n',
+ "pass_after_a_comment_row": _TWO + "def f():\n y = 1\n # note\n pass\n",
+ "pass_after_a_backslash_continuation": _TWO + "def f():\n y = 1 + \\\n2\n pass\n",
+}
+
+# None of these changes, and none needs the AST to know it.
+_UNTOUCHED = {
+ "join_on_a_space": 'x = " ".join(parts)\n',
+ "bytes_stay_side_by_side": 'x = b"a" b"b"\n',
+ "f_strings_stay_side_by_side": 'x = f"{a}" f"{b}"\n',
+ "mixed_quotes": "x = \"a\" 'b'\n",
+ "lone_pass_in_a_class": "class A:\n pass\n",
+ "lone_pass_in_a_handler": "try:\n import x\nexcept ImportError:\n pass\n",
+ "no_strings_no_pass": "def f(a = 1):\n return a\n",
+}
+
+
+def _full(text: str) -> tuple[str, bool]:
+ return K._post(text, spacing = False, blanks = False)
+
+
[email protected]("name", sorted(_CHANGES))
+def test_a_file_the_full_pass_changes_gets_the_same_change(name):
+ text = _CHANGES[name]
+ expected = _full(text)
+ assert expected[1], f"{name} no longer changes under the full pass; the case is stale"
+ assert K.residual(text) == expected
+
+
[email protected]("name", sorted(_UNTOUCHED))
+def test_a_file_nothing_applies_to_never_reaches_the_ast(name, monkeypatch):
+ text = _UNTOUCHED[name]
+ assert _full(text) == (text, False)
+
+ def refuse(*_args, **_kwargs):
+ raise AssertionError(f"{name} reached the AST pass")
+
+ monkeypatch.setattr(K, "_post", refuse)
+ assert K.residual(text) == (text, False)
+
+
+def test_residual_leaves_kwarg_spacing_and_import_blank_lines_alone():
+ # ruff-unsloth owns both; doing them again here would fight it.
+ text = 'def f():\n import os\n\n return g(a=1, s="x" "y")\n'
+ updated, changed = K.residual(text)
+ assert changed
+ assert updated == 'def f():\n import os\n\n return g(a=1, s="xy")\n'
+
+
+def test_the_residual_flag_runs_only_the_residual_passes(tmp_path):
+ target = tmp_path / "sample.py"
+ target.write_text('x = f(a=1, b="p" "q")\n', encoding = "utf-8")
+ script = Path(_SCRIPTS) / "enforce_kwargs_spacing.py"
+ subprocess.run(
+ [sys.executable, str(script), "--residual", str(target)], check = True, capture_output = True
+ )
+ assert target.read_text(encoding = "utf-8") == 'x = f(a=1, b="pq")\n'
+
+
+def test_this_repos_scripts_agree_with_the_full_pass():
+ # Real files, not snippets: every one either skips the AST or matches the full pass.
+ for path in sorted(Path(_SCRIPTS).glob("*.py")):
+ text = path.read_text(encoding = "utf-8")
+ assert K.residual(text) == _full(text), path.name
diff --git a/tests/test_formatter_fixed_point.py b/tests/test_formatter_fixed_point.py
index f62b10aa430..9d02cf43d1b 100644
--- a/tests/test_formatter_fixed_point.py
+++ b/tests/test_formatter_fixed_point.py
@@ -341,7 +341,7 @@ def test_every_tracked_python_file_is_already_formatted(tmp_path):
pytest.fail(
f"this guard cannot run in CI: {_RUFF_REASON}.\n"
" The runner installs the pinned ruff in its own step (see the "
- "'Install the pinned ruff (formatter fixed-point guard)' step in "
+ "'Install the pinned ruff-unsloth (formatter fixed-point guard)' step in "
".github/workflows/studio-backend-ci.yml).\n"
" Skipping here would report a green formatting guard that checked no files."
)
diff --git a/tests/test_run_ruff_format_pin.py b/tests/test_run_ruff_format_pin.py
index fc67112a161..42998a54b11 100644
--- a/tests/test_run_ruff_format_pin.py
+++ b/tests/test_run_ruff_format_pin.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Use the ruff .pre-commit-config.yaml pins or refuse; ruff output changes between releases."""
+"""Use the ruff-unsloth .pre-commit-config.yaml pins or refuse; its output changes between releases."""
from __future__ import annotations
@@ -22,37 +22,60 @@ from run_ruff_format import ( # noqa: E402
installed_ruff_version,
main,
parse_files,
+ pinned_requirements,
pinned_ruff_version,
version_mismatch,
)
+_URL = "https://github.com/unslothai/ruff/releases/download/v0.6.9-unsloth.1/"
+_LINUX = (
+ f'"ruff-unsloth @ {_URL}ruff_unsloth-0.6.9.post1-py3-none-manylinux_2_17_x86_64.whl'
+ "#sha256=00 ; sys_platform == 'linux'\""
+)
+_MAC = (
+ f'"ruff-unsloth @ {_URL}ruff_unsloth-0.6.9.post1-py3-none-macosx_11_0_arm64.whl'
+ "#sha256=11 ; sys_platform == 'darwin'\""
+)
+
class TestReadingThePin:
def test_the_repo_pins_a_ruff_and_it_is_readable(self):
assert CONFIG.exists()
assert pinned_ruff_version(CONFIG.read_text(encoding = "utf-8"))
+ def test_the_repo_lists_a_wheel_per_platform(self):
+ requirements = pinned_requirements(CONFIG.read_text(encoding = "utf-8"))
+ assert len(requirements) >= 2
+ assert all("#sha256=" in r and " ; " in r for r in requirements), requirements
+
@pytest.mark.parametrize(
"text, expected",
[
- (" additional_dependencies:\n - ruff==0.6.9\n", "0.6.9"),
- (" - ruff == 0.6.9\n", "0.6.9"),
- (" - ruff==0.6.9 # keep in step with the formatter\n", "0.6.9"),
- (" - ruff==0.12.0rc1\n", "0.12.0rc1"),
+ (f" additional_dependencies:\n - {_LINUX}\n", "0.6.9.post1"),
+ (f" - {_LINUX}\n - {_MAC}\n", "0.6.9.post1"),
+ (f" - {_LINUX} # keep in step with the formatter\n", "0.6.9.post1"),
+ (" - ruff-unsloth==0.6.9.post2\n", "0.6.9.post2"),
+ (" - ruff-unsloth == 0.6.9.post2 ; sys_platform == 'linux'\n", "0.6.9.post2"),
],
)
def test_the_spellings_a_config_may_use(self, text, expected):
assert pinned_ruff_version(text) == expected
+ def test_the_requirement_lines_come_back_installable(self):
+ # The quotes are YAML's, not part of the requirement pip is handed.
+ assert pinned_requirements(f" - {_LINUX}\n") == [_LINUX.strip('"')]
+
+ def test_stock_ruff_is_not_the_pin(self):
+ # The lint hook's ruff is a different tool; only ruff-unsloth formats.
+ assert pinned_ruff_version(" - ruff==0.6.9\n") is None
+
def test_no_pin_is_not_a_mismatch(self):
assert pinned_ruff_version("repos:\n - repo: local\n") is None
assert version_mismatch(None, "0.16.6") is False
def test_two_different_pins_answer_nothing(self):
- assert pinned_ruff_version(" - ruff==0.6.9\n - ruff==0.9.0\n") is None
-
- def test_the_same_pin_twice_is_still_one_answer(self):
- assert pinned_ruff_version(" - ruff==0.6.9\n - ruff==0.6.9\n") == "0.6.9"
+ text = f" - {_LINUX}\n - ruff-unsloth==0.6.9.post2\n"
+ assert pinned_ruff_version(text) is None
class TestTheMismatchRule:
@@ -130,15 +153,15 @@ class TestRuffHasToBeRunnableFirst:
)
assert main([str(target)]) == 1
err = capsys.readouterr().err
- assert "pip install ruff==" in err
+ assert "ruff-unsloth" in err and "additional_dependencies" in err
assert pinned_ruff_version(CONFIG.read_text(encoding = "utf-8")) in err
class TestRefusing:
@staticmethod
def _fake_ruff(tmp_path: Path, version: str) -> str:
- """A python whose `-m ruff --version` answers `version` and formats nothing."""
- pkg = tmp_path / "ruff"
+ """A python whose `-m ruff_unsloth --version` answers `version` and formats nothing."""
+ pkg = tmp_path / "ruff_unsloth"
pkg.mkdir()
(pkg / "__init__.py").write_text("")
(pkg / "__main__.py").write_text(
@@ -149,7 +172,7 @@ class TestRefusing:
shim.write_text(
"import runpy, sys\n"
f"sys.path.insert(0, {str(tmp_path)!r})\n"
- "runpy.run_module('ruff', run_name='__main__')\n",
+ "runpy.run_module('ruff_unsloth', run_name='__main__')\n",
encoding = "utf-8",
)
return str(shim)
@@ -172,7 +195,7 @@ class TestRefusing:
@pytest.mark.skipif(
installed_ruff_version() is None,
- reason = "this one really runs the formatter, and ruff is not installed here",
+ reason = "this one really runs the formatter, and ruff-unsloth is not installed here",
)
def test_the_override_lets_it_through(self, tmp_path, monkeypatch):
# Skipped without ruff: main() forwards the formatter's exit code, unrelated to the gate.
@@ -181,7 +204,7 @@ class TestRefusing:
monkeypatch.setattr("run_ruff_format.installed_ruff_version", lambda *a, **k: "9.9.9")
monkeypatch.setenv(ANY_VERSION_ENV, "1")
assert main([str(target)]) == 0
- # The post-pass is what puts the spaces in, so this proves it ran.
+ # ruff-unsloth is what puts the spaces in, so this proves it ran.
assert target.read_text(encoding = "utf-8") == "x = f(a = 1)\n"
def test_no_files_is_rejected_before_the_version_is_consulted(self, monkeypatch):