Files
rookiestar28andClaude Opus 5 ac9fbeec8d refactor(quality): modernize annotations in the three best-covered packages
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
2026-09-06 15:59:43 +08:00

129 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""
R114 implementation-record lint for defect-first regression evidence.
"""
from __future__ import annotations
import argparse
import re
import sys
from collections.abc import Sequence
from pathlib import Path
DEFAULT_KEYWORD_PATTERN = r"(bug|regression|security[ _-]?fix|hotfix|漏洞|瑕疵|修補)"
def _discover_record_files(paths: Sequence[str]) -> list[Path]:
files: list[Path] = []
for raw in paths:
p = Path(raw)
if p.is_dir():
files.extend(sorted(p.glob("*_IMPLEMENTATION_RECORD.md")))
elif p.is_file():
files.append(p)
dedup: list[Path] = []
seen = set()
for f in files:
resolved = f.resolve()
if resolved in seen:
continue
seen.add(resolved)
dedup.append(f)
return dedup
def lint_record_text(
text: str,
*,
strict: bool,
keyword_pattern: str = DEFAULT_KEYWORD_PATTERN,
) -> tuple[bool, list[str]]:
enforce = strict
if not enforce:
enforce = re.search(keyword_pattern, text, re.IGNORECASE) is not None
if not enforce:
return True, []
issues: list[str] = []
if not re.search(r"(?im)^##\s+Regression Evidence\b", text):
issues.append("missing section: '## Regression Evidence'")
required_fields = (
r"(?im)^\s*[-*]\s*Defect(?:\s+ID)?\s*:\s*\S+",
r"(?im)^\s*[-*]\s*Regression Test(?:\s+ID)?\s*:\s*\S+",
r"(?im)^\s*[-*]\s*Failing Evidence\s*:\s*\S+",
r"(?im)^\s*[-*]\s*Passing Evidence\s*:\s*\S+",
)
for pattern in required_fields:
if not re.search(pattern, text):
issues.append(f"missing required field matching: {pattern}")
return len(issues) == 0, issues
def lint_records(
paths: Sequence[str],
*,
strict: bool,
keyword_pattern: str = DEFAULT_KEYWORD_PATTERN,
) -> tuple[bool, list[str]]:
files = _discover_record_files(paths)
if not files:
return True, []
errors: list[str] = []
for file_path in files:
text = file_path.read_text(encoding="utf-8", errors="replace")
ok, issues = lint_record_text(
text,
strict=strict,
keyword_pattern=keyword_pattern,
)
if ok:
continue
for issue in issues:
errors.append(f"{file_path}: {issue}")
return len(errors) == 0, errors
def main() -> int:
parser = argparse.ArgumentParser(
description="Lint implementation records for defect-first regression evidence."
)
parser.add_argument(
"--path",
action="append",
default=None,
help="Record file or directory. Repeatable. Defaults to .planning.",
)
parser.add_argument(
"--strict",
action="store_true",
help="Force regression-evidence lint on all implementation records.",
)
parser.add_argument(
"--keyword-pattern",
default=DEFAULT_KEYWORD_PATTERN,
help="Regex used to trigger lint in non-strict mode.",
)
args = parser.parse_args()
paths = args.path or [".planning"]
ok, errors = lint_records(
paths,
strict=args.strict,
keyword_pattern=args.keyword_pattern,
)
if ok:
print("IMPLEMENTATION_RECORD_LINT: PASS")
return 0
for err in errors:
print(f"IMPLEMENTATION_RECORD_LINT: FAIL: {err}", file=sys.stdout)
return 1
if __name__ == "__main__":
raise SystemExit(main())