Skip to content
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 directive examples in library/doctest.html lack the flags #57156

Closed
merwok opened this issue Sep 9, 2011 · 33 comments
Closed

doctest directive examples in library/doctest.html lack the flags #57156

merwok opened this issue Sep 9, 2011 · 33 comments
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@merwok
Copy link
Member

merwok commented Sep 9, 2011

BPO 12947
Nosy @birkenfeld, @terryjreedy, @ncoghlan, @ezio-melotti, @merwok, @sandrotosi, @cjerdonek, @taschini
Files
  • issue12947_v0.patch
  • 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:

    assignee = None
    closed_at = <Date 2012-10-11.04:27:19.053>
    created_at = <Date 2011-09-09.17:22:33.554>
    labels = ['type-feature', 'docs']
    title = 'doctest directive examples in library/doctest.html lack the flags'
    updated_at = <Date 2012-10-11.04:27:19.053>
    user = 'https://github.com/merwok'

    bugs.python.org fields:

    activity = <Date 2012-10-11.04:27:19.053>
    actor = 'terry.reedy'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2012-10-11.04:27:19.053>
    closer = 'terry.reedy'
    components = ['Documentation']
    creation = <Date 2011-09-09.17:22:33.554>
    creator = 'eric.araujo'
    dependencies = []
    files = ['25424']
    hgrepos = []
    issue_num = 12947
    keywords = ['patch']
    message_count = 33.0
    messages = ['143775', '143777', '143780', '158600', '158621', '158732', '159593', '159697', '161232', '170160', '170184', '171858', '171859', '171860', '171862', '171866', '171868', '171869', '171871', '171874', '172336', '172368', '172575', '172576', '172578', '172580', '172581', '172582', '172585', '172586', '172587', '172615', '172618']
    nosy_count = 10.0
    nosy_names = ['georg.brandl', 'terry.reedy', 'ncoghlan', 'ezio.melotti', 'eric.araujo', 'sandro.tosi', 'chris.jerdonek', 'docs@python', 'python-dev', 'taschini']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue12947'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    @merwok
    Copy link
    Member Author

    merwok commented Sep 9, 2011

    The docs for the doctest module contains examples that use doctest flags to control behavior, but the HTML version does not contain the flags, making the examples useless.

    I don’t know if this is a bug with the HTML builder or something we can fix in markup.

    @merwok merwok added the docs Documentation in the Doc dir label Sep 9, 2011
    @merwok
    Copy link
    Member Author

    merwok commented Sep 9, 2011

    Ezio prompted me to give precise examples, and I can’t find any in the docs online. Maybe it was only a local build with certain options that caused this problem, I’ll reopen if I find out.

    @merwok merwok closed this as completed Sep 9, 2011
    @merwok merwok added the invalid label Sep 9, 2011
    @birkenfeld
    Copy link
    Member

    Hehe... Sphinx makes a point of *removing* doctest flags, to enable doctesting of code snippets without distracting the reader with the test-internal flags.

    I think it's because you used a newer version locally.

    @taschini
    Copy link
    Mannequin

    taschini mannequin commented Apr 18, 2012

    Concrete examples can be seen in the section

    http://docs.python.org/library/doctest.html#option-flags-and-directives

    For instance at

    http://docs.python.org/library/doctest.html#doctest.IGNORE_EXCEPTION_DETAIL

    The doctest flags present in the sources in

    http://docs.python.org/_sources/library/doctest.txt

    are all stripped.

    @merwok
    Copy link
    Member Author

    merwok commented Apr 18, 2012

    Thank you. I think it’s clear that for the docs of the doctest flags we need to display snippets with the flags.

    @merwok merwok reopened this Apr 18, 2012
    @merwok merwok removed the invalid label Apr 18, 2012
    @taschini
    Copy link
    Mannequin

    taschini mannequin commented Apr 19, 2012

    As far as I can see, Sphinx has a global setting for trim_doctest_flags but lacks the possibility of locally disabling the trimming.

    A quick workaround would be to have the following sphinx extension added:

    class ProxyLexer(object):
    
        def __init__(self, underlying):
            self.__underlying = underlying
    
        def __getattr__(self, attr):
            return getattr(self.__underlying, attr)
    
    def setup(app):
        from sphinx.highlighting import lexers
        if lexers is not None:
            lexers['pycon-literal'] = ProxyLexer(lexers['pycon'])
            lexers['pycon3-literal'] = ProxyLexer(lexers['pycon3'])

    That would allow blocks marked as

    .. code-block:: pycon-literal

    or preceded by

    .. highlight:: pycon-literal

    to escape the trimming of doctest flags.

    If that's of any interest I can submit a patch.

    @ezio-melotti
    Copy link
    Member

    Is there a way to add a :keep-doctest-flags: options to literal blocks?

    @ezio-melotti ezio-melotti added the type-feature A feature request or enhancement label Apr 29, 2012
    @taschini
    Copy link
    Mannequin

    taschini mannequin commented Apr 30, 2012

    Ezio, the patch I attached goes into that direction, by adding a ":trim-doctest-flags: disable" option to the code blocks.

    I thought I had a good reason for having the option worded as ":trim-doctest-flags: disable" instead of ":keep-doctest-flags:", now I'm not so sure.

    Note: the patch is against the 2.7 branch.

    @ssbr ssbr mannequin changed the title Examples in library/doctest.html lack the flags doctest directive examples in library/doctest.html lack the flags May 20, 2012
    @terryjreedy
    Copy link
    Member

    The directives should normally be stripped, but not when they are intentionally given to teach their existence, syntax, and use, as in the doctest doc on directives.

    I opened (and closed -- am trying to anyway) a duplicate, bpo-14865. The problem of directive stripping started in 3.2.0 and subsequently 2.7.3 (after 2.7.2 in June 2011) and contiues in 3.3.0. Sandro Tosi noted

    [1] https://bitbucket.org/birkenfeld/sphinx/issue/169/strip-doctest-csomments-in-rendered-output
    [2] https://bitbucket.org/birkenfeld/sphinx/changeset/d91bf8e465ef
    Issue and commit from June 2009.

    My question is whether the added true-by-default trim_doctest_flags config value can just be added to doctest.rst? That would seem to be the point of having a settable config value, but I am ignorant of how sphinx works. I see that the patch does that, but also a lot more.

    In any case, this is a nasty regression in the docs and should be fixed somehow before the next releases. It makes the examples actively confusing.

    @cjerdonek
    Copy link
    Member

    I discovered this today as well while reading the doctest documentation.

    One thing that I never noticed before (and that doesn't seem to be reflected in the comments above) is that many of the code snippet rectangles in the doctest documentation have a small rectangle in the upper-right corner with the text ">>>".

    If you click on one of these small rectangles, most of the text inside the code snippet rectangle disappears. I haven't confirmed this, but I suspect that it is the code snippets with a doctest directive that have this clickable corner.

    Is this a feature? What is it for? :)

    @ezio-melotti
    Copy link
    Member

    Is this a feature? What is it for? :)

    The >>> is added to every example that contains an interactive session to hide the '>>>' and '...' prompts and the output and make the code copy/pastable, and it's not specific to doctests.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 3, 2012

    New changeset 662fb4bd5f84 by Nick Coghlan in branch '2.7':
    Issue bpo-12947: Add a note to doctest until the example rendering is fixed
    http://hg.python.org/cpython/rev/662fb4bd5f84

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 3, 2012

    New changeset 679b3e3aadae by Nick Coghlan in branch '3.3':
    Issue bpo-12947: Add a note to doctest until the example rendering is fixed
    http://hg.python.org/cpython/rev/679b3e3aadae

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Oct 3, 2012

    Since the status quo is thoroughly confusing for readers, I added an explicit note before the affected examples. That note should be removed once the rendering problem is fixed. (The note is there on trunk as well, I just forgot to include the issue number in the merge commit)

    @cjerdonek
    Copy link
    Member

    I thought of an easy work-around we can use after looking at the changeset Terry referenced above:

    [2] https://bitbucket.org/birkenfeld/sphinx/changeset/d91bf8e465ef

    At the expense of pretty color highlighting, we can enable Pygments' TextLexer for the affected examples (aka "null" lexer). For example--

    .. code-block:: text

       >>> raise CustomError('message') #doctest: +IGNORE_EXCEPTION_DETAIL
       Traceback (most recent call last):
       CustomError: message

    I confirmed locally that this works. I realized this might work because the Sphinx changeset referenced above has this logic:

        # trim doctest options if wanted
        if isinstance(lexer, PythonConsoleLexer) and self.trim_doctest_flags:

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 3, 2012

    New changeset 18d927fb8671 by Nick Coghlan in branch '2.7':
    Issue bpo-12947: Better workaround for the problem with doctest directives being stripped from code examples that are intended to illustrate those directives
    http://hg.python.org/cpython/rev/18d927fb8671

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 3, 2012

    New changeset d93a59a0a984 by Nick Coghlan in branch '3.3':
    Issue bpo-12947: Better workaround for the problem with doctest directives being stripped from code examples that are intended to illustrate those directives
    http://hg.python.org/cpython/rev/d93a59a0a984

    New changeset 26200f535296 by Nick Coghlan in branch 'default':
    Merge bpo-12947 workaround from 3.3
    http://hg.python.org/cpython/rev/26200f535296

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Oct 3, 2012

    Adopted Chris's workaround for now. I kept a reworded version of the preceding note (with the link to this bug), so readers know that the lack of syntax highlighting is intended-but-not-desired.

    @cjerdonek
    Copy link
    Member

    Thanks, Nick. It looks like there are a few more though? I'm counting four more in default (search for "doctest:"): three IGNORE_EXCEPTION_DETAIL and one ELLIPSIS.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Oct 3, 2012

    I only changed the ones that were specifically in the section explaining doctest directives. There are probably others, but it didn't seem necessary to change them and lose the syntax highlighting at this point.

    @terryjreedy
    Copy link
    Member

    http://docs.python.org/py3k/library/doctest.html#option-flags-and-directives

    has the improved version. Thanks. Here, the complete example text is more important than the hi-lites.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Oct 8, 2012

    Ah, I see the other examples Chris is talking about now. Yes, the workaround should be applied in those cases as well.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 10, 2012

    New changeset 00555659253d by Chris Jerdonek in branch '3.3':
    Issue bpo-12947: Divide doctest "Option Flags and Directives" section into two.
    http://hg.python.org/cpython/rev/00555659253d

    New changeset 467c9f663b77 by Chris Jerdonek in branch 'default':
    Issue bpo-12947: Merge doctest documentation improvements from 3.3.
    http://hg.python.org/cpython/rev/467c9f663b77

    New changeset 15bfa778ec21 by Chris Jerdonek in branch '2.7':
    Issue bpo-12947: Backport doctest documentation improvements from 3.3.
    http://hg.python.org/cpython/rev/15bfa778ec21

    @cjerdonek
    Copy link
    Member

    Yes, the workaround should be applied in those cases as well.

    I added the workaround to the last remaining example. Nick was right before in that three of the four examples I mentioned above were in the part about "option flags" rather than in the part about "directives". I made those two parts into their own sections for added clarity.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 10, 2012

    New changeset 9057da41cf0b by Georg Brandl in branch '3.2':
    Issue bpo-12947: revert earlier workaround and use a monkey-patch to enable showing doctest directives only in the doctest docs.
    http://hg.python.org/cpython/rev/9057da41cf0b

    New changeset 1202c1449d83 by Georg Brandl in branch '3.3':
    Issue bpo-12947: revert earlier workaround and use a monkey-patch to enable showing doctest directives only in the doctest docs.
    http://hg.python.org/cpython/rev/1202c1449d83

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 10, 2012

    New changeset ffe9f644d5af by Georg Brandl in branch '2.7':
    Issue bpo-12947: revert earlier workaround and use a monkey-patch to enable showing doctest directives only in the doctest docs.
    http://hg.python.org/cpython/rev/ffe9f644d5af

    @cjerdonek
    Copy link
    Member

    Thanks, Georg. Can this new directive be applied to individual code blocks though (or be modified to do so)?

    After a closer review (as mentioned in my comment above), it seems that the doctest directives should only be displayed in the code snippets that are illustrating the use of doctest directives (as opposed to, say, the snippets that are illustrating doctest options without using directives).

    @birkenfeld
    Copy link
    Member

    I don't see any blocks in doctest.rst where the directives should be stripped, so it's enough to do it per-file.

    @cjerdonek
    Copy link
    Member

    Like these that discuss the behavior of the doctest.IGNORE_EXCEPTION_DETAIL option (but before doctest directives have been introduced):

    http://hg.python.org/cpython/file/0d6910bef979/Doc/library/doctest.rst#l543

    The key is that you can have options take effect without using directives, so it would be good if people can see examples like that.

    @cjerdonek
    Copy link
    Member

    Georg suggested removing the doctest directives from the examples I mentioned. As currently written, they don't help them pass if doctest were run on the .rst file. That sounds fine to me for now.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 10, 2012

    New changeset 705a70b0fff4 by Chris Jerdonek in branch '3.3':
    Issue bpo-12947: Remove doctest directives from the doctest examples in the "Option Flag" section.
    http://hg.python.org/cpython/rev/705a70b0fff4

    New changeset 9475fc82768e by Chris Jerdonek in branch 'default':
    Issue bpo-12947: Merge doctest documentation change from 3.3.
    http://hg.python.org/cpython/rev/9475fc82768e

    New changeset 8704f9e7ad7c by Chris Jerdonek in branch '2.7':
    Issue bpo-12947: Backport doctest documentation change from 3.3.
    http://hg.python.org/cpython/rev/8704f9e7ad7c

    @cjerdonek
    Copy link
    Member

    This seems to look good now. Can this be closed, or is there something else that needed to be done as well?

    @terryjreedy
    Copy link
    Member

    I looked at
    http://docs.python.org/py3k/library/doctest.html#directives
    and the examples with directives looked fine to me, with directive, syntax markup, and 'hide prompt' box. I also checked the next section and saw the one example that needed to have the directive visible. Really nice.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants