Files
Pavlo Ratushnyi 334af80d3d STRA-367: limits gate for lawbook 301 (function 30 lines, complexity 10, nesting 4, width 120)
New and changed code is checked against the base (merge-base with the integration branch):
every added or touched function and every added or changed line must be within the limits,
unless limits-allowlist.json lists the function with a reason and an owner; an allowlisted
function may only shrink. The legacy that exists today is a per-crate census in
limits-baseline.json whose totals may only fall (same --update-baseline semantics as the
architecture gate). Rust is measured from the rustsplit lexer, the gate's own Python from ast.
56 self-tests, including a temporary git repository.
2026-11-06 11:26:53 +01:00

37 lines
1.6 KiB
Python

"""The committed allowlist limits-allowlist.json: functions that are allowed to miss a limit, and by how much."""
import json
from .model import KINDS, allowed
FILE = "<file>" # the entry for lines outside any function of a file (consts, attributes, imports): only "width"
COMMENT = ("Functions that may miss a limit of scripts/check-limits.py because they were moved verbatim from legacy "
"code. Every recorded number may only fall; an entry whose function is back within the limits is stale and "
"must go (scripts/check-limits.py --update-baseline). New entries: --allowlist-add --reason ... --owner ...")
def load(path):
"""{(path, name): entry}; a missing file is an empty allowlist."""
if not path.exists():
return {}
data = json.loads(path.read_text(encoding="utf-8"))
return {(entry["path"], entry["name"]): entry for entry in data["functions"]}
def save(path, entries):
rows = [entries[key] for key in sorted(entries)]
with open(path, "w", encoding="utf-8", newline="\n") as handle:
handle.write(json.dumps({"_comment": COMMENT, "functions": rows}, indent=1) + "\n")
def new_entry(rel, name, measures, reason, owner):
return {"path": rel, "name": name, **measures, "reason": reason, "owner": owner}
def missing_fields(entry):
"""Names of the mandatory fields an entry leaves empty."""
return [name for name in ("reason", "owner") if not str(entry.get(name, "")).strip()]
def recorded(entry, limits):
"""The numbers an entry allows: a kind it does not mention is allowed up to the limit only."""
return {kind: entry.get(kind, allowed(kind, limits)) for kind in KINDS}