Repository navigation
Split examples as doctest reads them - #167
Merged
Merged
Conversation
An example is now the text that introduces it and a snippet holding the >>> lines, their ... continuations and the expected output, up to a blank line or the next >>> line. Text after the last snippet is an example of its own, with no snippet. Numpydoc paired a snippet with the text after it and moved continuation lines out of the snippet, and Google kept a whole section as one example with no snippet. Both styles compose examples so that they read back the same: the text, a blank line, then the snippet. Google no longer writes trailing spaces on the blank lines of a section such as Examples.
An example with no snippet is the text after its section's last snippet. Composing every example into one section joined such text with the next examples, which read back as fewer examples. End a section after it, and give an empty example a section of its own.
rr-
force-pushed
the
rework/31-doctest-examples
branch
from
November 6, 2026 17:40
3eefc88 to
0fdd784
Compare
Python 3.8 evaluates a nested function's annotations each time it is defined, which made composing Google docstrings 13% slower. Write the sections with a function at module level.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Numpydoc and Google now split an examples section the way doctest reads it. Each example holds the text that introduces it as
description, and asnippetwith the>>>lines, their...continuations and the expected output, up to a blank line or the next>>>line. Text after the last snippet becomes an example with no snippet.Before, Numpydoc gave
snippet=">>> for i in range(2):"and put the...line and the output into the description, together with the text after it. Google kept the whole section as one example with no snippet. Now both styles give two examples:description="To print the numbers:", with the snippet holding theforloop, itsprint(i)continuation and the output0and1description="Then more text.",snippet=NoneThis is breaking for users who read
example.description: it used to be the text after a snippet in Numpydoc, and is now the text before it.Compose writes each example's text, a blank line, then its snippet, so the result reads back the same in both styles. A snippet with no text follows the previous one directly. A Google section with nothing in it still gives one empty example, as before, so auto-detection doesn't change for docstrings whose examples aren't indented under the title. Google compose also stops writing trailing spaces on the blank lines inside a section like Examples.
Parity: 794 of 29,123 docstrings differ from main.
145 case files change for these reasons, plus two Numpydoc tests that pinned the old pairing.
Bench on 3.8: parse is 0.99–1.02x of main, compose 0.98–1.02x.