Example 1::
>>> 1+23
Example 2:
.. doctest::
>>> 1+23
We 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:
doctest module says that both of these examples are doctests
sphinx says 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.
The 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.
Option (2) is definitely attractive. Just two concerns from me:
How long will this make the Doctest CI job? It already takes 10 minutes to run all CPython's doctests via Sphinx; I worry that this option could make it significantly slower, by dramatically increasing the number of doctests run in CI. This is hypothetical, though; I might be wrong!
I agree with you that there'll probably be quite a few that fail at first. If we could figure out a way to tackle the errors incrementally, like what we've been doing with the Sphinx nitpicks, that would be ideal. Would it be possible to apply doctest_test_doctest_blocks only to certain files? (Cc. @AA-Turner again as the Sphinx expert here!)
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?
Can't you just not add .. doctest to 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 .. doctest directive above them.)
Activity
sobolevn commented on Nov 3, 2023
I hope that I will send the first PR today / tomorrow, but I am a bit sick :(
AlexWaygood commented on Nov 3, 2023
Option (2) is definitely attractive. Just two concerns from me:
AlexWaygood commented on Nov 3, 2023
Also, rest up and get well soon!
hugovk commented on 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 commented on Nov 3, 2023
This is option 2 :)
pythongh-111704: Add `doctest_test_doctest_blocks` option to Sphinx
sobolevn commented on Nov 7, 2023
After working with make doctest for a while, here are my thoughts:
.. testcleanup:: import os os.remove("tutorial.db")I didn't like this tool at all.
It might be useful for small projects, but not for big ones.
AlexWaygood commented on Nov 7, 2023
Can't you just not add .. doctest to 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 .. doctest directive above them.)
sobolevn commented on Nov 7, 2023
Turns out that the answer is "no".
See #111735
It is a bit of a mess.
sobolevn commented on 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:
cpython/Doc/library/typing.rst
Lines 1473 to 1481 in c176543
cpython/Lib/test/test_enum.py
Lines 28 to 45 in c176543
Important things to keep in mind:
pythongh-111704: Add `stdtypes.rst` to doctests