Skip to content

Conversation

@strawgate
Copy link
Contributor

@strawgate strawgate commented Dec 19, 2025

Require formatted and runnable examples in docstrings and documentation, and bring the documentation and code up to compliance with these new requirements. Closes #1587

Uses pytest-examples to ensure that examples in docstrings and documentation are properly formatted and runnable with assertions on any print statements in the example. Adds pytest-timeout to the dev dependencies which is used to prevent example tests from running forever if they hit blocking code.

There are zero lines of actual code changed outside the test_docs.py. The majority of the PR is imports organization, linting, formatting, and ensuring that examples are runnable or ensuring we skip examples that should not be linted or that should not be run (either because they are incomplete examples, require internet access or an external connection, or are blocking).

There are ~300 newly passing tests and ~140 skipped tests (examples that are not checked for formatting or if they are runnable).

@codecov
Copy link

codecov bot commented Dec 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@strawgate strawgate changed the title [Draft] Add pytest-examples [Draft] Require formatted and runable examples in docstrings and documentation Dec 20, 2025
@strawgate strawgate changed the title [Draft] Require formatted and runable examples in docstrings and documentation Require formatted and runable examples in docstrings and documentation Dec 20, 2025
@strawgate strawgate changed the title Require formatted and runable examples in docstrings and documentation Require formatted and runnable examples in docstrings and documentation Dec 20, 2025
@strawgate strawgate changed the title Require formatted and runnable examples in docstrings and documentation Enforce working examples in docstrings and documentation Dec 20, 2025
counter = logfire.metric_counter(
'exceptions',
unit='1', # (1)!
description='Number of exceptions caught'
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comma at the end here to keep the args on separate lines, especially so that # (1)! stays on the unit arg. same for others below.

Comment on lines +211 to +213
#> Failed to execute query. Status code: 401
print(response.text)
#> {"detail":"Invalid token"}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#> Failed to execute query. Status code: 401
print(response.text)
#> {"detail":"Invalid token"}
print(response.text)

this is being skipped, right?



def capture_response_headers(span: Span, response: Union[TraceRequestEndParams, TraceRequestExceptionParams]):
def capture_response_headers(span: Span, response: TraceRequestEndParams | TraceRequestExceptionParams):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def capture_response_headers(span: Span, response: TraceRequestEndParams | TraceRequestExceptionParams):
def capture_response_headers(span: Span, response: 'TraceRequestEndParams | TraceRequestExceptionParams'):

then skip the future import?

#> 019b38289f5dfbf64cd7de68d37c6a04
```

will print something like:
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not have both this and the comments above

Comment on lines +36 to +37
print('Serving on port 8000...')
#> Serving on port 8000...
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
print('Serving on port 8000...')
#> Serving on port 8000...


# Override the autouse fixtures from conftest.py to prevent them from
# interfering with doc examples. Doc examples call their own logfire.configure()
# and we don't want them to pollute the test exporter state.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see how the test exporter state would get polluted. are you sure this fixture is needed?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add pytest-examples to verify documentation examples

2 participants