Python 3.8 compatibility, routines.py --mark YAML spacing and comments, OKF --out and Starlight image paths confined to their folders, script counts in AGENTS.md/READMEs/manuals, and the tests/unit/ suite. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
4 KiB
Python
93 lines
4 KiB
Python
"""Every script the template ships must run on Python 3.8 (AGENTS.md §3).
|
|
|
|
No 3.8 interpreter is needed: this parses each script and rejects syntax and
|
|
standard-library APIs that arrived later. The mcp server is exempt — its SDK
|
|
dependency requires 3.10, and it runs in its own container.
|
|
"""
|
|
import ast
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
EXEMPT = {Path(".agents/modules/mcp/server")}
|
|
|
|
# Methods and functions added after 3.8, by name.
|
|
LATER_ATTRS = {
|
|
"removeprefix", "removesuffix", # str/bytes, 3.9
|
|
"is_relative_to", "with_stem", # pathlib, 3.9
|
|
"readlink", # Path.readlink, 3.9
|
|
"lcm", # math, 3.9
|
|
"bit_count", # int, 3.10
|
|
"pairwise", # itertools, 3.10
|
|
"aiter", "anext", # builtins, 3.10
|
|
"batched", # itertools, 3.12
|
|
"walk", # Path.walk, 3.12 (os.walk is fine; see below)
|
|
}
|
|
LATER_MODULES = {"zoneinfo", "graphlib", "tomllib"}
|
|
BUILTIN_GENERICS = {"list", "dict", "set", "frozenset", "tuple", "type"}
|
|
|
|
|
|
def scripts():
|
|
for p in sorted((ROOT / ".agents").rglob("*.py")):
|
|
rel = p.relative_to(ROOT)
|
|
if any(rel.is_relative_to(e) if hasattr(rel, "is_relative_to") else str(rel).startswith(str(e)) for e in EXEMPT):
|
|
continue
|
|
yield rel, p.read_text(encoding="utf-8")
|
|
|
|
|
|
def problems(rel, text):
|
|
out = []
|
|
try:
|
|
tree = ast.parse(text, filename=str(rel), feature_version=(3, 8))
|
|
except SyntaxError as e:
|
|
return [f"{rel}:{e.lineno}: syntax newer than 3.8: {e.msg}"]
|
|
future_annotations = any(
|
|
isinstance(n, ast.ImportFrom) and n.module == "__future__"
|
|
and any(a.name == "annotations" for a in n.names)
|
|
for n in tree.body)
|
|
|
|
def annotation_issue(node):
|
|
for sub in ast.walk(node):
|
|
if isinstance(sub, ast.BinOp) and isinstance(sub.op, ast.BitOr):
|
|
return "`X | Y` annotation (3.10)"
|
|
if isinstance(sub, ast.Subscript) and isinstance(sub.value, ast.Name) \
|
|
and sub.value.id in BUILTIN_GENERICS:
|
|
return f"`{sub.value.id}[...]` annotation (3.9)"
|
|
return None
|
|
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Attribute) and node.attr in LATER_ATTRS:
|
|
# os.walk predates everything; only flag `.walk` on non-`os` objects
|
|
if node.attr == "walk" and isinstance(node.value, ast.Name) and node.value.id == "os":
|
|
continue
|
|
out.append(f"{rel}:{node.lineno}: `.{node.attr}` is newer than 3.8")
|
|
elif isinstance(node, ast.Import):
|
|
out += [f"{rel}:{node.lineno}: module `{a.name}` is newer than 3.8"
|
|
for a in node.names if a.name.split(".")[0] in LATER_MODULES]
|
|
elif isinstance(node, ast.ImportFrom) and node.module and node.module.split(".")[0] in LATER_MODULES:
|
|
out.append(f"{rel}:{node.lineno}: module `{node.module}` is newer than 3.8")
|
|
elif not future_annotations and isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
|
anns = [a.annotation for a in node.args.args + node.args.kwonlyargs if a.annotation]
|
|
anns += [node.returns] if node.returns else []
|
|
for ann in anns:
|
|
issue = annotation_issue(ann)
|
|
if issue:
|
|
out.append(f"{rel}:{ann.lineno}: {issue}")
|
|
return out
|
|
|
|
|
|
class Py38Compat(unittest.TestCase):
|
|
def test_scripts_parse_and_use_only_38_apis(self):
|
|
found = []
|
|
for rel, text in scripts():
|
|
found += problems(rel, text)
|
|
self.assertEqual(found, [], "\n" + "\n".join(found))
|
|
|
|
def test_guard_catches_known_bad_code(self):
|
|
bad = "def f(x: list[str]) -> str | None:\n return x[0].removeprefix('a')\nimport zoneinfo\n"
|
|
self.assertEqual(len(problems(Path("bad.py"), bad)), 4)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|