Refactor code block syntax checker for maintainability and clarity #6589
theotrosman
started this conversation in
Feature suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This script (check_code_blocks.py) is useful for validating embedded Python code in Markdown files using pyright, but it could benefit from a few improvements in structure and clarity to support better maintainability and robustness.
Observations:
Code block extraction logic is coupled to Python-only blocks.
The script hardcodes ```python as the start delimiter. If in the future we want to support other languages or extensions, this makes it harder to scale.
The ignore-check logic is verbose and nested.
The nested all(all(...)) logic for checking imports from specific modules (autogen_*) is functional but hard to read and could be abstracted into a helper function.
Subprocess call to pyright is inline.
This can be abstracted for better testability and reusability.
Temporary files are written and not cleaned up.
Currently, delete=False is used in NamedTemporaryFile, but there's no explicit cleanup. This may leave behind orphaned .py files after execution.
No test mode or dry-run flag.
Could be useful for CI pipelines to add a non-blocking mode that just reports without throwing RuntimeError.
Hardcoded dependency on pyright.
There's no fallback or clear error message if pyright is not installed.
Suggested improvements:
Abstract extract_python_code_blocks() into a more generic extract_code_blocks(language: str)
Refactor the should_ignore_block() logic into its own function for readability
Wrap the subprocess call in a run_pyright(file_path: str) -> CompletedProcess utility
Add optional cleanup for temporary files
Add a --dry-run CLI option that doesn’t raise errors (useful for CI)
Check for pyright presence at startup and fail fast with a helpful message if not found
Beta Was this translation helpful? Give feedback.
All reactions