The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(ci): check_public_surface refuses unresolvable refs and does not flag methods extracted into an in-module base class
Teknium09f2a7c0369f
diff --git a/scripts/ci/check_public_surface.py b/scripts/ci/check_public_surface.pyindex f878e9dbd49..7b8f816386e 100644--- a/scripts/ci/check_public_surface.py+++ b/scripts/ci/check_public_surface.py@@ -69,20 +69,34 @@ def public_toplevel(src: str) -> set[str] | None:return {n for n in names if not n.startswith("_")}+def _is_public_method(name: str) -> bool:+ return not name.startswith("_") or name.startswith("__")++def public_methods(src: str) -> set[str]:- """``Class.method`` for public and dunder methods of top-level classes."""+ """``Class.method`` for public and dunder methods REACHABLE on each top-level class: defined on it+ or inherited from a base class defined in the same module. Extracting a method into a mixin/base+ that the class still derives from is not a removal (the attribute still resolves)."""try:tree = ast.parse(src)except SyntaxError:return set()+ classes = {n.name: n for n in tree.body if isinstance(n, ast.ClassDef)}++ def own(cls: ast.ClassDef) -> set[str]:+ return {m.name for m in cls.body if isinstance(m, (ast.FunctionDef, ast.AsyncFunctionDef)) and _is_public_method(m.name)}++ def reachable(cls: ast.ClassDef, seen: set[str]) -> set[str]:+ names = own(cls)+ for base in cls.bases:+ base_name = base.id if isinstance(base, ast.Name) else (base.attr if isinstance(base, ast.Attribute) else None)+ if base_name in classes and base_name not in seen:+ names |= reachable(classes[base_name], seen | {base_name})+ return names+out: set[str] = set()- for node in tree.body:- if isinstance(node, ast.ClassDef):- for m in node.body:- if isinstance(m, (ast.FunctionDef, ast.AsyncFunctionDef)) and (- not m.name.startswith("_") or m.name.startswith("__")- ):- out.add(f"{node.name}.{m.name}")+ for name, cls in classes.items():+ out.update(f"{name}.{m}" for m in reachable(cls, {name}))return out@@ -145,7 +159,14 @@ def main(argv: list[str] | None = None) -> int:ap.add_argument("--strict", action="store_true", help="exit 1 on any finding")ap.add_argument("--json", dest="json_out", help="also write the report as JSON")args = ap.parse_args(argv)- base = _git("merge-base", args.base, args.head).strip() or args.base+ for ref in (args.base, args.head):+ if subprocess.run(["git", "rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}"], capture_output=True).returncode != 0:+ print(f"public-surface: cannot resolve ref {ref!r} (fetch it first); refusing to report a clean diff", file=sys.stderr)+ return 2+ base = _git("merge-base", args.base, args.head).strip()+ if not base:+ print(f"public-surface: no merge-base between {args.base!r} and {args.head!r}", file=sys.stderr)+ return 2report = diff_surface(base, args.head)print(render(report))if args.json_out:diff --git a/tests/scripts/test_check_public_surface.py b/tests/scripts/test_check_public_surface.pyindex e0e5f038f2b..bd625941986 100644--- a/tests/scripts/test_check_public_surface.py+++ b/tests/scripts/test_check_public_surface.py@@ -39,6 +39,9 @@ def test_dropped_public_names_methods_and_test_defs_are_reported_and_moves_are_ndef pub(self): ...def __len__(self): return 0def _hidden(self): ...+ class Big:+ def stays(self): ...+ def extracted(self): ...'''), encoding="utf-8")(repo / "agent" / "moved.py").write_text("def relocated(): ...\n", encoding="utf-8")(repo / "tests" / "test_x.py").write_text("def test_a(): ...\ndef test_b(): ...\nasync def test_c(): ...\n", encoding="utf-8")@@ -55,6 +58,10 @@ def test_dropped_public_names_methods_and_test_defs_are_reported_and_moves_are_nLIMIT = 3def keep(): ...class K: ...+ class _BigMixin:+ def extracted(self): ... # moved into an in-module base: still reachable on Big+ class Big(_BigMixin):+ def stays(self): ...'''), encoding="utf-8")(repo / "agent" / "moved.py").write_text("def relocated(): ...\ndef relocated2(): ...\n", encoding="utf-8")(repo / "tests" / "test_x.py").write_text("def test_a(): ...\nasync def test_c(): ...\n", encoding="utf-8")@@ -70,3 +77,15 @@ def test_dropped_public_names_methods_and_test_defs_are_reported_and_moves_are_nassert mod.main(["--base", "HEAD~1", "--head", "HEAD"]) == 0 # advisoryassert mod.main(["--base", "HEAD~1", "--head", "HEAD", "--strict"]) == 1assert mod.main(["--base", "HEAD", "--head", "HEAD", "--strict"]) == 0 # nothing dropped+++def test_unresolvable_refs_are_an_error_not_a_clean_report(tmp_path, monkeypatch):+ """Independent-review witness: a nonexistent base ref under --strict reported zero drops and exited 0,+ which would make a mis-fetched CI job look clean."""+ repo = tmp_path+ (repo / "a.py").write_text("x = 1\n", encoding="utf-8")+ _git(repo, "init", "-q", "-b", "main"); _git(repo, "add", "."); _git(repo, "commit", "-qm", "base")+ monkeypatch.chdir(repo)+ mod = _load()+ assert mod.main(["--base", "origin/does-not-exist", "--head", "HEAD", "--strict"]) == 2+ assert mod.main(["--base", "origin/does-not-exist", "--head", "HEAD"]) == 2