"""Enforce the repository's v0.2.1 Ruff debt as a decreasing baseline.""" from __future__ import annotations import argparse import json from collections import Counter from pathlib import Path from typing import Any BASELINE_VERSION = "v0.2.1" BASELINE_TOTAL = 576 BASELINE_BY_RULE = { "B008": 117, "B017": 1, "B018": 1, "BLE001": 19, "C408": 7, "DTZ901": 1, "F401": 65, "F541": 6, "F811": 16, "F841": 20, "FURB157": 2, "I001": 90, "RET501": 2, "RUF015": 2, "RUF059": 8, "RUF100": 1, "SIM102": 1, "SIM103": 1, "SIM114": 2, "SIM115": 8, "SIM117": 13, "TRY004": 1, "TRY203": 9, "UP017": 166, "UP035": 5, "UP037": 4, "UP045": 8, } def baseline_regressions(diagnostics: list[dict[str, Any]]) -> list[str]: """Return rule-count or total-count increases above the frozen baseline.""" counts = Counter(item["code"] for item in diagnostics) regressions = [ f"{code}: {count} > {BASELINE_BY_RULE.get(code, 0)}" for code, count in sorted(counts.items()) if count > BASELINE_BY_RULE.get(code, 0) ] if len(diagnostics) > BASELINE_TOTAL: regressions.append(f"total: {len(diagnostics)} > {BASELINE_TOTAL}") return regressions def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("diagnostics", type=Path) args = parser.parse_args() diagnostics = json.loads(args.diagnostics.read_text(encoding="utf-8")) regressions = baseline_regressions(diagnostics) if regressions: print(f"Ruff debt increased above {BASELINE_VERSION}:") for regression in regressions: print(f"- {regression}") return 1 print(f"Ruff debt ratchet passed: {len(diagnostics)} <= {BASELINE_TOTAL} ({BASELINE_VERSION})") return 0 if __name__ == "__main__": raise SystemExit(main())