check-python-snippets.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. """Check Python 3.11 syntax in lesson snippets without executing services."""
  2. import ast
  3. import json
  4. from pathlib import Path
  5. root = Path(__file__).resolve().parents[1]
  6. files = [root / "app/lessons.json", *sorted((root / "public/content").glob("*.json"))]
  7. checked = 0
  8. errors = []
  9. for path in files:
  10. lessons = json.loads(path.read_text(encoding="utf-8-sig"))
  11. for lesson in lessons:
  12. for index, step in enumerate(lesson["steps"], 1):
  13. if step.get("lang") not in {"python", "py"} or not step.get("code"):
  14. continue
  15. label = f"{path.stem}/{lesson['id']}/step-{index}"
  16. try:
  17. ast.parse(step["code"], filename=label, feature_version=(3, 11))
  18. checked += 1
  19. except SyntaxError as exc:
  20. errors.append(f"{label}: line {exc.lineno}: {exc.msg}")
  21. if errors:
  22. raise SystemExit("\n".join(errors))
  23. print(f"Python 3.11 AST: {checked} snippets passed across {len(files)} modules.")
  24. print("Syntax only; dependencies, SQL, external services and failure drills are not executed.")