[issue36675] Doctest directives and comments not visible or missing from code samples
New submission from Steven D'Aprano
Change by Karthikeyan Singaravelan
Terry J. Reedy
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.
----------
nosy: +eric.araujo, ezio.melotti, terry.reedy, willingc
stage: -> needs patch
title: Doctest directives and comments not visible or missing from code samples -> Doctest directives and comments missing from code samples
versions: +Python 2.7, Python 3.7, Python 3.8
_______________________________________
Python tracker
Change by Roundup Robot
Jeroen Demeyer
Éric Araujo
Terry J. Reedy
Steven D'Aprano
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.
Thanks for the patch, and the extra information, but I disagree with the decision to suppress the directives. The reason I found this problem in the first case was that I started with the ctypes documentation, where it says: "Since some code samples behave differently under Linux, Windows, or Mac OS X, they contain doctest directives in comments." and I was very keen to see those directives so I could learn the right way to deal with platform-dependent doctests. I was very confused that they weren't visible. https://docs.python.org/3/library/ctypes.html I think that doctest directives are as much a part of documenting correct usage as any other part of the example code, and they are (semi-)human readable and (almost) self-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 surpressed, I don't know how to interpret that. Is the output some arbitrarily chosen value that doctest ought to skip? Or is that the actual output that c_wchar_p("Hello, World") will return every single time without fail? If I was a ctypes expert, it might be blindingly obvious to me, but I'm not, so I'm left in the dark. I don't know whether I should expect that precise output each and every time, or something platform and implementation specific. If the directive #doctest:+SKIP was visible, I would know that it was an arbitrarily chosen example. My preference would be: - keep the doctest directives visible, everywhere; - make them a clickable link to the appropriate section in the doctest documentation; - and, if possible, on mouse-over, they should display a tooltip with a message like "The output of this example is arbitrary." Or similar wording.
They really should not be in the dir example code that I linked to. https://docs.python.org/3/library/functions.html#dir
On the contrary: I think that the presence of the +SKIP directive helps
demonstrate that the output shown is a made-up example, not normative.
(Of course it helps that I know doctest, but even if I didn't, the
tooltip message would help.)
----------
_______________________________________
Python tracker
Jeroen Demeyer
Doctest directives in code examples should be suppressed everywhere *except* in the doctest.html examples showing how to use directives.
Thanks for clarifying. I missed that.
----------
_______________________________________
Python tracker
Cheryl Sabella
Julien Palard
Change by Julien Palard
Gregory P. Smith
Change by miss-islington
miss-islington
Jim DeLaHunt
Change by Julien Palard
Julien Palard
Julien Palard
Anthony Sottile
Julien Palard
Jim DeLaHunt
participants (12)
-
Anthony Sottile
-
Cheryl Sabella
-
Gregory P. Smith
-
Jeroen Demeyer
-
Jim DeLaHunt
-
Julien Palard
-
Karthikeyan Singaravelan
-
miss-islington
-
Roundup Robot
-
Steven D'Aprano
-
Terry J. Reedy
-
Éric Araujo