Skip to content

Test readme#390

Open
tschm wants to merge 14 commits intoplotly:masterfrom
tschm:test_readme
Open

Test readme#390
tschm wants to merge 14 commits intoplotly:masterfrom
tschm:test_readme

Conversation

@tschm
Copy link
Copy Markdown
Contributor

@tschm tschm commented Aug 16, 2025

No description provided.

@gvwilson gvwilson added feature something new community community contribution P3 not needed for current cycle labels Aug 22, 2025
Copilot AI review requested due to automatic review settings May 2, 2026 18:22
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds automated validation for Python examples in the documentation by converting README code fences to doctest-compatible snippets and introducing a pytest that executes them.

Changes:

  • Added a pytest that extracts ```python blocks from README.md and runs them via doctest.
  • Updated src/py/README.md Python examples to doctest prompt style (>>>, ...) so they can be executed as tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/py/tests/test_readme.py New test to extract README Python blocks and execute them via doctest.
src/py/README.md Updates Python code fences to doctest format to support execution/validation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +124 to +135
try:
# Run the code examples through doctest
doctest.run_docstring_examples(docstring, globals())
except doctest.DocTestFailure as e:
# If a DocTestFailure occurs, capture it and manually fail the test
pytest.fail(f"Doctests failed: {e}")

# Capture the output after running doctests
captured = capfd.readouterr()

# If there is any output (error message), fail the test
if captured.out:
Comment on lines +90 to +102
# Extract Python code blocks (assuming they are in triple backticks)
blocks = re.findall(r"```python(.*?)```", content, re.DOTALL)

code = "\n".join(blocks).strip()

# Add a docstring wrapper for doctest to process the code
docstring = f"\n{code}\n"

return docstring

except FileNotFoundError:
warnings.warn("README.md file not found", stacklevel=2)
return ""
Comment on lines +131 to +136
# Capture the output after running doctests
captured = capfd.readouterr()

# If there is any output (error message), fail the test
if captured.out:
pytest.fail(f"Doctests failed with:\n{captured.out} and \n{docstring}")
Comment on lines +67 to +92
return find_project_root(Path(__file__).parent)


@pytest.fixture
def docstring(project_root: Path) -> str:
"""Extract Python code blocks from README.md and prepare them for doctest.

This fixture reads the README.md file, extracts all Python code blocks
(enclosed in triple backticks with 'python' language identifier), and
combines them into a single docstring that can be processed by doctest.

Args:
project_root: Path to the project root directory

Returns:
str: A docstring containing all Python code examples from README.md

"""
# Read the README.md file
try:
with Path.open(project_root / "README.md", encoding="utf-8") as f:
content = f.read()

# Extract Python code blocks (assuming they are in triple backticks)
blocks = re.findall(r"```python(.*?)```", content, re.DOTALL)

Comment on lines +121 to +123
# Change to the root directory to ensure imports work correctly
os.chdir(project_root)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community community contribution feature something new P3 not needed for current cycle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants