Repository navigation
Open
Labels
buildThe build process and cross-buildThe build process and cross-builddocsDocumentation in the Doc dirDocumentation in the Doc dirtestsTests in the Lib/test dirTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Initial PR and discussion: #111682
So, here's the problem:
Example 1::
>>> 1 + 2
3
Example 2:
.. doctest::
>>> 1 + 2
3We have two tools: doctest (builtin module) and sphinx (3rd party) that can run these samples as doctests. The problem is that they define "doctest" term differently:
doctestmodule says that both of these examples are doctestssphinxsays that only example with.. doctest::directive is a doctest
This is problematic, because not all example are actually executed in CI, right now we have a CI job that uses Sphinx. So, in the result of this we have broken doctest (1st meaning) examples in the docs.
Examples:
- gh-54434: Make difflib.rst doctests pass. #111677
- gh-111181: Fix enum doctests #111180
- gh-111282: Fix NamedTemporaryFile example code #111283
What can we do?
Options:
- Add
.. doctest::directives everywhere, but I think it is too much work and people will forget to do this - Try to use
doctest_test_doctest_blocks, see docs https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html This is what I plan to do locally and I will send a PR with the results. I fear that quite a lot of doctests might be broken - Add doctests to
test_*files using something like
def load_tests(loader, tests, pattern):
tests.addTests(doctest.DocFileSuite(
os.path.join(REPO_ROOT, 'Doc/library/LIBNAME.rst'),
module_relative=False,
))
return testsThe last option is also problematic, because right now we can only run doctests in CI, when PR only touches Doc/ folder, I am not sure that it will be easy with this setup.
Linked PRs
Reacted by Alex Waygood
Metadata
Metadata
Assignees
Labels
buildThe build process and cross-buildThe build process and cross-builddocsDocumentation in the Doc dirDocumentation in the Doc dirtestsTests in the Lib/test dirTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
[-]Decide what should we do with doctests[/-][+]Decide what should we do with doctests in CI[/+]sobolevn commentedon Nov 3, 2023
I hope that I will send the first PR today / tomorrow, but I am a bit sick :(
AlexWaygood commentedon Nov 3, 2023
Option (2) is definitely attractive. Just two concerns from me:
doctest_test_doctest_blocksonly to certain files? (Cc. @AA-Turner again as the Sphinx expert here!)AlexWaygood commentedon Nov 3, 2023
Also, rest up and get well soon!
hugovk commentedon Nov 3, 2023
Option 4: Modify Sphinx to run both types?
But I'm also concerned about the doctest taking longer. Of the three docs jobs, doctest is the longest at 10 minutes (= 3m setup/building + 7m doctest) compared to 2.5 minutes each for the other two:
Can we do them in parallel instead of sequential? Can the doctest module run them quicker?
sobolevn commentedon Nov 3, 2023
This is option 2 :)
pythongh-111704: Add `doctest_test_doctest_blocks` option to Sphinx
sobolevn commentedon Nov 7, 2023
After working with
make doctestfor a while, here are my thoughts:-m doctest, liketestsetup.. testcleanup:: import os os.remove("tutorial.db")conf.pyand add a test logic there:skipIf:TO ALL DOCTESTS in a file (seeDoc/library/turtle.rstdoctests are failing whentkinteris not present #111735)I didn't like this tool at all.
It might be useful for small projects, but not for big ones.
AlexWaygood commentedon Nov 7, 2023
Can't you just not add
.. doctestto the REPL snippets in the documentation that you don't want to be treated as doctests? (Though I know we'd need to change that if we were to switch to a system where sphinx automatically treated all REPL snippets as doctests, regardless of whether they have the.. doctestdirective above them.)sobolevn commentedon Nov 7, 2023
Turns out that the answer is "no".
See #111735
It is a bit of a mess.
sobolevn commentedon Jul 10, 2025
I propose adding more text files to doctests, we can start with important ones and move slowly to increase the coverage.
There are two options:
.. doctest::directive in the file itself. This allows us to add only some parts of the file to the doctest. Like we do here:cpython/Doc/library/typing.rst
Lines 1473 to 1481 in c176543
def load_testin some related test file with passing filename to load. Like we do here:cpython/Lib/test/test_enum.py
Lines 28 to 45 in c176543
Important things to keep in mind:
floatformatting optionspythongh-111704: Add `stdtypes.rst` to doctests