test(docs): add code example extraction and syntax validation#1250
Conversation
Doc code examples had no automated syntax checking — broken Python snippets could ship to users without anyone noticing until they copy-pasted. Now `util/check_doc_code.py` extracts fenced code blocks from all MDX/MD docs and runs `compile()` on Python blocks, reporting file/line for each error. Handles MDX patterns: indented blocks inside <Tabs>/<Steps>/<Accordion>, top-level `await` (async doc examples), `return`/`yield` (method body excerpts), and `...` placeholders. Closes #244
|
Good infrastructure addition — Issues🟡 CI job will fail on every doc-touching PR after mergeThe new Three viable fixes — pick one: Option A — Exclude spec/plans directories (scope the check to user-facing docs only): (Requires adding Option B — Mark the step Option C — Fix the 104 existing issues before or alongside this PR (preferred long-term, but likely a separate PR given the scope). 🟡 "Code blocks checked" count is wrong without
|
…ntation The validator was flagging 104 blocks that are function signatures, flow diagrams, mixed-language blocks, or spec pseudo-code — not runnable Python. Now it detects and skips: bare signatures without bodies, arrow notation, TOML/shell mixed blocks, trailing-ellipsis placeholders, and indented fragments. Design docs (docs/spec/, docs/plans/) are skipped entirely. Also fixes a real indentation bug in the chat-agent playbook where the "# Use it" section was at column 0 instead of the code block's indent level. Result: 464 Python blocks validated, 0 errors — CI passes.
|
Good addition to the CI toolchain — doc-code validation closes a real gap (broken snippets shipping to users undetected). The pseudo-code heuristics cover the tricky MDX patterns well, and the test suite is thorough. Two issues worth fixing before merge. Issues🟡 Silent exception swallow in
|
- Replace silent `except Exception` with `OSError` + stderr warning (CLAUDE.md prohibits swallowing errors) - Fix statistics bug: ok_count and skipped_count now tracked independently of --verbose, so default output shows correct totals (was showing 0/0) - Return CheckSummary NamedTuple instead of bare list to carry counts - Remove dead `verbose` param from format_results() - Rename `warnings` variable to `issues` in check_imports() to avoid shadowing the stdlib module - Replace `global` anti-pattern in test fixture with module-level import - Extract duplicated `_setup_docs` helper into shared pytest fixture - Add test_ok_count_without_verbose to verify the statistics fix
|
This PR closes a real gap: broken Python snippets in MDX docs could ship to users with no automated catch. The implementation is solid across the board — clean data modelling, correct handling of real-world MDX quirks, and a thorough test suite. Two minor nits worth fixing before this becomes the pattern other utilities follow. Issues🟢 Minor —
|
itomek
left a comment
There was a problem hiding this comment.
The three blockers from the first review are resolved in head — extract_code_blocks now catches OSError and warns to stderr (no silent swallow), check_code_blocks returns a verbose-independent ok_count so the default summary is accurate, and the spec/plans pseudo-code blocks are skipped so CI won't fail the repo on merge. Approving. Minor, non-blocking: the description's "~104 genuine syntax issues" line is stale relative to the current skip behavior — worth a one-line edit so the test-plan wording matches what the tool now does.
Generated by Claude Code
Doc code examples had no automated syntax checking — broken Python snippets could ship to users without anyone noticing until they copy-pasted from the docs. Now
util/check_doc_code.pyextracts fenced code blocks from all MDX/MD files and runscompile()on Python blocks, reporting file and line number for each syntax error. Handles real-world MDX patterns: indented blocks inside<Tabs>/<Steps>/<Accordion>, top-levelawait(async doc examples),return/yield(method body excerpts), and...placeholders. Pseudo-code in design docs (docs/spec/,docs/plans/) and non-runnable patterns (function signatures, flow diagrams, mixed-language blocks) are detected and skipped. CI runs it automatically on doc changes alongside the existing link checker.Test plan
python -m pytest tests/unit/test_doc_code_validation.py -xvs— 47 tests passpython util/check_doc_code.py --lang python— 464 blocks validated, 0 errors, exits 0python util/check_doc_code.py(no filter) — shows correct OK/skipped counts even without--verboseCloses #244