R250 tranche 1. The static-analysis baseline is reduced by fixing code, not by raising pins, in the packages where a mechanical rewrite is best protected by tests: models (92.7% covered), nodes (90.1%) and scripts (80.0%). The two worst covered packages, connector (57.1%) and api (60.7%), are deferred, and no path in the tranche matches a canonical high-risk pattern. Ruff UP006/UP035/UP045 across 17 files removes 146 findings: PEP 585 builtin generics, PEP 604 unions, and the deprecated typing imports they consume. The autofix was scoped to an explicit file list and rule list, never to a directory or the repository, and the whole diff was reviewed. Those rewrites orphan 23 typing imports, so "remove no imports" was not available: leaving them would be new debt the exact-match ratchet rejects. The rule applied instead is narrow and uniformly evidenced - remove an unused import only when it is a typing-vocabulary name from typing or the collections.abc target UP035 relocates to. Neither module has import side effects and no file in the tranche declares __all__, so no such name can be a compatibility re-export. That removed 37 imports, the 23 orphans plus 14 already-dead ones. Six unused imports that need real analysis are untouched: a possible re-export in models.schemas, two bare sys/os imports, a name imported out of a test module, and unittest.mock.patch. mypy strictness advances by exactly two packages. models.* and nodes.* now set disallow_untyped_defs through a scoped override; the global default stays false. The measured cost was 11 missing annotations, all added here. models/schemas.py filters caller payloads through cls.__annotations__ and the node classes are introspected by the ComfyUI host, so the pins for both went in before the rewrite: exact annotation-key tuples for all five dataclasses, from_dict filtering, asdict/JSON round-trips, the frozen INPUT_TYPES / RETURN_TYPES / FUNCTION / CATEGORY surface, and a load check for every tranche script. All 11 passed before the rewrite and pass unchanged after it. Ruff also had to reach for builtins.dict in GenerationParams.from_dict, because that class defines a dict() method that shadows the builtin inside the class body by the time the later annotation is evaluated. That is correct and the alternative would rename a public method. Baseline: 1465 -> 1395 fingerprints, 3566 -> 3406 findings. The written diff removes UP006 93, UP035 33, UP045 20 and F401 14, adds nothing, and touches no path outside the 17 tranche files. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DJVFcBRvDopwG5qwW4ThNt
120 lines
3.4 KiB
Python
120 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import subprocess
|
|
import sys
|
|
from collections.abc import Callable, Iterable
|
|
from pathlib import Path
|
|
|
|
OPENAPI_SYNC_TRIGGER_PATHS = {
|
|
"docs/openapi.yaml",
|
|
"docs/release/api_contract.md",
|
|
"scripts/generate_openapi_spec.py",
|
|
"services/openapi_generation.py",
|
|
}
|
|
|
|
|
|
def _repo_root() -> Path:
|
|
return Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def _normalize_path(path: str) -> str:
|
|
return path.replace("\\", "/").strip()
|
|
|
|
|
|
def _get_staged_paths() -> list[str]:
|
|
result = subprocess.run(
|
|
["git", "diff", "--cached", "--name-only", "--diff-filter=ACMR"],
|
|
check=False,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
text=True,
|
|
)
|
|
if result.returncode != 0:
|
|
return []
|
|
return [
|
|
_normalize_path(line)
|
|
for line in result.stdout.splitlines()
|
|
if _normalize_path(line)
|
|
]
|
|
|
|
|
|
def should_validate_openapi(paths: Iterable[str]) -> bool:
|
|
normalized = {_normalize_path(path) for path in paths if _normalize_path(path)}
|
|
if not normalized:
|
|
return False
|
|
return any(path in OPENAPI_SYNC_TRIGGER_PATHS for path in normalized)
|
|
|
|
|
|
def _load_generate_openapi_yaml() -> Callable[[str | Path | None], str]:
|
|
root = str(_repo_root())
|
|
if root not in sys.path:
|
|
sys.path.insert(0, root)
|
|
from services.openapi_generation import generate_openapi_yaml
|
|
|
|
return generate_openapi_yaml
|
|
|
|
|
|
def validate_openapi_sync(
|
|
*,
|
|
openapi_path: str | Path | None = None,
|
|
contract_path: str | Path | None = None,
|
|
generate_openapi_yaml: Callable[[str | Path | None], str] | None = None,
|
|
) -> tuple[bool, str]:
|
|
root = _repo_root()
|
|
openapi_file = (
|
|
Path(openapi_path) if openapi_path else root / "docs" / "openapi.yaml"
|
|
)
|
|
contract_file = (
|
|
Path(contract_path)
|
|
if contract_path
|
|
else root / "docs" / "release" / "api_contract.md"
|
|
)
|
|
if not openapi_file.exists():
|
|
return False, f"[OpenClaw] OpenAPI sync check failed: missing {openapi_file}"
|
|
|
|
generator = generate_openapi_yaml or _load_generate_openapi_yaml()
|
|
expected = generator(contract_file)
|
|
actual = openapi_file.read_text(encoding="utf-8")
|
|
if actual == expected:
|
|
return True, ""
|
|
|
|
message = "\n".join(
|
|
[
|
|
"[OpenClaw] Commit/push blocked: docs/openapi.yaml is out of sync.",
|
|
"",
|
|
"Generated specs must not be edited by hand without regenerating them.",
|
|
"",
|
|
"Fix:",
|
|
" 1. Update generator/contract inputs as needed.",
|
|
" 2. Regenerate with: python scripts/generate_openapi_spec.py",
|
|
" 3. Review and stage docs/openapi.yaml together with the source change.",
|
|
]
|
|
)
|
|
return False, message
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Guard that docs/openapi.yaml stays in sync with the generator."
|
|
)
|
|
parser.add_argument(
|
|
"--staged",
|
|
action="store_true",
|
|
help="Only validate when staged changes touch OpenAPI generator/spec sources.",
|
|
)
|
|
args = parser.parse_args(argv)
|
|
|
|
if args.staged and not should_validate_openapi(_get_staged_paths()):
|
|
return 0
|
|
|
|
ok, message = validate_openapi_sync()
|
|
if ok:
|
|
return 0
|
|
print(message, file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|