Skip to content

Commit 218afdf

Browse files
committed
docs: add executable doctest examples and validation
1 parent 8c852bd commit 218afdf

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎conf.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from urllib.request import urlopen
44

55
extensions = [
6+
'sphinx.ext.doctest',
67
'notfound.extension',
78
'sphinx.ext.extlinks',
89
'sphinx.ext.intersphinx',
@@ -15,6 +16,7 @@
1516
'sphinxext.opengraph',
1617
'sphinxext.rediraffe',
1718
]
19+
doctest_test_doctest_blocks = ''
1820

1921
html_last_updated_fmt = '%b %d, %Y'
2022

‎documentation/style-guide.rst‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,40 @@ When writing interactive examples, keep prompt style consistent and copy-friendl
364364
For the reST markup used to present interactive sessions, see
365365
:ref:`showing-code-examples`.
366366

367+
Testing interactive examples
368+
----------------------------
369+
370+
Interactive Python examples should produce the output shown in the
371+
documentation. When possible, use :mod:`doctest` to verify that the
372+
examples continue to work as Python evolves.
373+
374+
Prefer examples with predictable results. For instance, the following
375+
example can be checked automatically:
376+
377+
.. doctest::
378+
379+
>>> numbers = [2, 3, 5]
380+
>>> sum(numbers)
381+
10
382+
>>> sorted(numbers, reverse=True)
383+
[5, 3, 2]
384+
385+
Some results contain values that vary between executions, such as
386+
memory addresses. The ``ELLIPSIS`` option allows variable portions
387+
of the expected output to be matched without requiring an exact value:
388+
389+
.. doctest::
390+
391+
>>> object() # doctest: +ELLIPSIS
392+
<object object at 0x...>
393+
394+
Use ``ELLIPSIS`` only when the output contains genuinely variable
395+
information. Prefer exact expected results whenever possible.
396+
397+
The Sphinx doctest builder executes explicitly marked examples and
398+
compares their output with the expected results. This helps identify
399+
examples that have become incorrect or outdated.
400+
367401

368402
Code equivalents
369403
================

0 commit comments

Comments
 (0)