-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doctest directives and comments missing from code samples #80856
Comments
(Apologies if this is the wrong place for reporting website bugs.) The website is not rendering doctest directives or comments, either that or the comments have been stripped from the examples. On the doctest page itself, all the comments are missing: https://docs.python.org/3/library/doctest.html#directives The first example says: >>> print(list(range(20)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19] but without the directive, the test would fail. Screen shot attached. Doctest directives are also missing from here: https://docs.python.org/3/library/ctypes.html My browser: Firefox 45.1.1 Also checked with text browser "lynx". |
I verified that the line in Doc/library/doctest.rst has the comment. >>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
"
However, the comment is omitted from the .html built on Windows by Sphinx 1.8.1.
"
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span>
***html for comment should be here***
<span class="go">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,</span>
<span class="go">10, 11, 12, 13, 14, 15, 16, 17, 18, 19]</span>
</pre></div>
</div>
"
To me, this is a bug with building the .html for doctest.rst. Comments are preserved elsewhere. For instance, the code example for https://docs.python.org/3/library/functions.html#dir. The .rst file has
"
The resulting list is sorted alphabetically. For example:
>>> import struct
>>> dir() # show the names in the module namespace # doctest: +SKIP
"
Both use 3-space colon + indents to mean 'example block'. The only difference is '::' versus ':'. https://devguide.python.org/documenting/#source-code says :: is required. idle.rst also has a code example with comments displayed (I just submitted a PR to not suppress color highlighting.) I leave it to the doc experts to discover why the comments are not included in doctest.html. |
Isn't it a *feature* that those doctest directives are not shown? Those directives are meant for the doctest module only, not for the reader of the rendered documentation. |
OP is about the documentation page for doctest itself! |
Doctest directives in code examples should be suppressed everywhere *except* in the doctest.html examples showing how to use directives. The patch only exposes them for doctest.html and not for ctypes or anywhere else. They really should not be in the dir example code that I linked to. https://devguide.python.org/documenting/#source-code does not say anything about '::' causing suppression of comments and ':' leaving them. It is misleading in implying the '::' is required for a code block. |
Thanks for clarifying. I missed that. |
Thanks for the patch, and the extra information, but I disagree with the The reason I found this problem in the first case was that I started "Since some code samples behave differently under Linux, Windows, or Mac and I was very keen to see those directives so I could learn the right https://docs.python.org/3/library/ctypes.html I think that doctest directives are as much a part of documenting Consider this example from ctypes: >>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392) In the absence of a directive, but knowing that it may have been If I was a ctypes expert, it might be blindingly obvious to me, but I'm If the directive #doctest:+SKIP was visible, I would know that it was an My preference would be:
Or similar wording.
On the contrary: I think that the presence of the +SKIP directive helps (Of course it helps that I know doctest, but even if I didn't, the |
;tldr; There is a global configuration flag to show all the doctest directives for all the docs that are built. The default is to suppress the doctest directives. If a global setting isn't desired, then each doctest example will have to be changed individually. I've looked at the Sphinx documentation for this and there are a few points to reference.
>>> datetime.date.now() # doctest: +SKIP
datetime.date(2008, 1, 1)
Note that in the Sphinx doctest [4] documentation, there is a config option The nice thing about the doc build recognizing a section as a doctest is that it adds the toggle in the upper right of the block to 'Hide the toggle and output' so that the example can be copy and pasted more easily. Changing it into something other than a doctest (like using a Options:
[1] http://www.sphinx-doc.org/en/stable/usage/restructuredtext/basics.html#literal-blocks |
I opened an issue on the sphinx-doc repo 1 to check if it would be possible to have an option in doctest blocks to not trim them. We previously had a hack in Doc/tools/extensions/pyspecific.py to patch sphinx to not trim them for the doctest.rst file. But sphinx deprecated the hack we used :( |
We discovered and fixed this same problem back in 2011-2012 with bpo-12947 . That was apparently the source of the monkeypatch that was removed as "obselete code" on 2019-09-12. That old issue commentary has some suggestions about other workarounds. This includes adding explanatory text about the fact that doctest directives are missing from the examples which should be showing them. Maybe some of those workarounds would be effective for us now. |
Happy Christmas, everybody involved in this issue! I'm happy to announce this issue is resolved since a few days \o/ |
should the minimum sphinx version be bumped and/or this reverted: Line 48 in f7f0ed5
this change breaks, for example, sphinx 1.8.5 while building for ubuntu 20.04 The directive used here requires sphinx>=3.2.0 I notice some other attempts have been made to make the docs more compatible with sphinx 1.x in this release as well so there might be conflicting directions here: b63a620 |
My goodness, things get complex sometimes. If we cannot make Sphinx preserve doctest directives and comments, perhaps we should go back to the historical bug discussion to look at workarounds which we considered earlier. For instance, maybe we should modify the text surrounding the examples to explain that doctests directives should appear there, but that our tool chain currently removes them. At the moment, I see doctest directives in the doctest source code at: How about rewording the text before each of those examples? Or maybe finding some way to show those examples as literal text which Sphinx won't modify, even if it is not formatted like Python code examples? By the way, the discussion of this same problem back in 2011-2012 is at bpo-12947 . At that time, we applied a "monkey patch" to the Sphinx code. I haven't read the discussion closely enough to figure out if such a patch would help now. |
Since docs are now built on Sphinx 4.5.0, it might be a good time to re-fix this. I applied a cherry-picked eb7fcb8 locally and it seems to work fine. Made this into a new PR #92318 |
* Doc: Reveal doctest directives. * Fix whitespace. Co-authored-by: Julien Palard <[email protected]> Co-authored-by: Ezio Melotti <[email protected]>
* Doc: Reveal doctest directives. * Fix whitespace. Co-authored-by: Julien Palard <[email protected]> Co-authored-by: Ezio Melotti <[email protected]> (cherry picked from commit 7b024e3) Co-authored-by: Davide Rizzo <[email protected]>
* Doc: Reveal doctest directives. * Fix whitespace. Co-authored-by: Julien Palard <[email protected]> Co-authored-by: Ezio Melotti <[email protected]> (cherry picked from commit 7b024e3) Co-authored-by: Davide Rizzo <[email protected]>
* Doc: Reveal doctest directives. * Fix whitespace. Co-authored-by: Julien Palard <[email protected]> Co-authored-by: Ezio Melotti <[email protected]> (cherry picked from commit 7b024e3) Co-authored-by: Davide Rizzo <[email protected]>
* Doc: Reveal doctest directives. * Fix whitespace. Co-authored-by: Julien Palard <[email protected]> Co-authored-by: Ezio Melotti <[email protected]> (cherry picked from commit 7b024e3) Co-authored-by: Davide Rizzo <[email protected]>
This is now fixed on 3.10/3.11/main. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: