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

Improve explanation of tab expansion in doctests #51832

Closed
techtonik mannequin opened this issue Dec 27, 2009 · 12 comments
Closed

Improve explanation of tab expansion in doctests #51832

techtonik mannequin opened this issue Dec 27, 2009 · 12 comments
Assignees
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@techtonik
Copy link
Mannequin

techtonik mannequin commented Dec 27, 2009

BPO 7583
Nosy @bitdancer
Files
  • doctabtest.py
  • issue7583.doctest.tabs.diff: patch
  • doctest-tabs-doc.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 = 'https://github.com/bitdancer'
    closed_at = <Date 2010-06-01.01:49:15.528>
    created_at = <Date 2009-12-27.16:50:30.340>
    labels = ['type-bug', 'docs']
    title = 'Improve explanation of tab expansion in doctests'
    updated_at = <Date 2010-06-01.01:49:15.526>
    user = 'https://bugs.python.org/techtonik'

    bugs.python.org fields:

    activity = <Date 2010-06-01.01:49:15.526>
    actor = 'r.david.murray'
    assignee = 'r.david.murray'
    closed = True
    closed_date = <Date 2010-06-01.01:49:15.528>
    closer = 'r.david.murray'
    components = ['Documentation']
    creation = <Date 2009-12-27.16:50:30.340>
    creator = 'techtonik'
    dependencies = []
    files = ['15679', '16721', '17232']
    hgrepos = []
    issue_num = 7583
    keywords = ['patch']
    message_count = 12.0
    messages = ['96914', '102048', '102096', '102097', '102317', '102322', '102378', '104253', '105057', '105117', '105146', '106822']
    nosy_count = 3.0
    nosy_names = ['techtonik', 'rbp', 'r.david.murray']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue7583'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented Dec 27, 2009

    Since 2.4 doctest converts all tabs to 8-space sequences in test source.
    It should do the same with output it receives for comparison. Right now
    there is no way to write a correct doctest if the output includes tab
    character. See attached doctabtest.py

    This change would be backwards compatible, because all tests with tabs are
    either fail or run with doctest.NORMALIZE_WHITESPACE flag, which is
    usually inappropriate for doctest cases that involve tab formatting.

    @techtonik techtonik mannequin added the tests Tests in the Lib/test dir label Dec 27, 2009
    @techtonik techtonik mannequin changed the title doctest should normalize tabs when comparing output [patch] doctest should normalize tabs when comparing output Mar 31, 2010
    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented Apr 1, 2010

    @bitdancer
    Copy link
    Member

    Removed [patch] from title as patch is set on the keywords. Removed 2.5 from versions because it is in security fix only mode (we use versions for where things will be fixed, not where they are broken). Changed component to Library as this is not a bug in the tests themselves but in a library package, doctest.

    I didn't set stage to patch review only because I'd like other opinions on the root issue. Normalizing tabs to 8 spaces sounds wrong to me (including in the source).

    @bitdancer bitdancer added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error and removed tests Tests in the Lib/test dir labels Apr 1, 2010
    @bitdancer bitdancer changed the title [patch] doctest should normalize tabs when comparing output doctest should normalize tabs when comparing output Apr 1, 2010
    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented Apr 1, 2010

    http://docs.python.org/library/doctest.html#how-are-docstring-examples-recognized

    "Changed in version 2.4: Expanding tabs to spaces is new; previous versions tried to preserve hard tabs, with confusing results"

    Unfortunately, no confusing results survived to see what was wrong.

    @bitdancer
    Copy link
    Member

    I think I can see how it would cause confusion, and in general it shouldn't be necessary to use real tabs in a doctest. So as you say the output should be fixed to match.

    However, I don't think the patch is quite correct. It looks to me like the expandtabs call should be made unconditionally on the output string just like it is on the string pulled from the file. The reason is that the tabs in the output should be expanded relative to the start of the output line. The indentation is meaningless in that context. The way you did it, the output in your test appears to be indented incorrectly. If the expandtabs were done on the raw output string, it would be indented eight spaces from the start of the test indent, and that would look correct.

    Your change of 'iff' to 'if' is also most likely incorrect. I'm pretty sure that 'iff' is intentional (it is an abbreviation for "if and only if".)

    @bitdancer bitdancer self-assigned this Apr 4, 2010
    @bitdancer
    Copy link
    Member

    Having thought about it some more, I see why you did the patch the way you did.

    The fact that there are two completely different ways to expand tabs in the output that are equally valid and have their advantages and disadvantages makes me wonder if this should be fixed at all. Perhaps it is better to just say that you can only handle tabs in output by ignoring whitespace.

    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented Apr 5, 2010

    Could you be more specific about why users should not be allowed to use tabs in docstrings. An example use case/user story would help me a lot.

    I've made a precondition to check tab existence before expanding tabs for performance reasons.

    The indentation of output prior to tab expansion is necessary to correctly format output and show result when test fails.

    I agree to close this bug as "won't fix" if we can clear the confusion by describing the situation in more detail and providing examples.

    @bitdancer
    Copy link
    Member

    The problem is that it would be equally reasonable for someone to want to put real tab characters in the output section (which results in strange looking doctest text) or to put expanded spaces in the doctest output section based on the assumption that output starts in the column under the first > of the >>> and that doctest will expand the tabs in the output it receives from the executed test using a tabstop of 8.  Neither of these is a good solution (the first gives you messed up looking doctests, the second means the output you document isn't really the output the test gives).  The second solution would also mean a significant rewrite of the doctest processing loop.  So the best course, since doctests are *primarily* about documentation, is to allow tabs in the output only with whitespace normalization.  (If you really want to test for the presence of tabs in the output, as opposed to just creating documentation, you can capture the output and test it using string comparison.)

    Personally I think the 'too bad' language in the docs is not really appropriate, so if you can think of a succinct way to document the above, I'll see about getting it in to the docs.

    @bitdancer bitdancer added docs Documentation in the Doc dir type-feature A feature request or enhancement and removed stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 26, 2010
    @bitdancer bitdancer changed the title doctest should normalize tabs when comparing output Improve explanation of tab expansion in doctests Apr 26, 2010
    @rbp
    Copy link
    Mannequin

    rbp mannequin commented May 5, 2010

    I've just been bitten by this, and I agree the language in the docs is very inappropriate (made me angry for a minute :)).

    One suggestion: "While not everyone might believe tabs should mean that, doctests are primarily aimed at documentation, and, since it's very hard to get tabs to look consistent, keeping hard tabs would be potentially confusing. If you absolutely need to test for the presence of tabs at the output, you can capture it and use string comparison, or write your own DocTestParser class."

    @bitdancer
    Copy link
    Member

    I tried your suggestion, but it seemed to me that it made the first paragraph of that section be all about tabs, and get even farther away from its original focus, which was introducing the example.

    I've attached a patch that instead moves the entire discussion of tabs to the 'fine print' section, where it arguably belongs anyway, and expands the discussion somewhat. Feedback welcome.

    @bitdancer bitdancer added type-bug An unexpected behavior, bug, or error and removed type-feature A feature request or enhancement labels May 6, 2010
    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented May 6, 2010

    Sorry for not being able to follow up on this issue. I believe we need to expand the problem of handling tabs in doctests with use cases and expose the problem outside the issue tracker item. I still remember that at some point I had a patch somewhere that allowed to use tabs in doctests without too much explanations. Can't find it in this tracker though.

    @bitdancer
    Copy link
    Member

    In the absence of feedback about the doc patch, I have applied it in r81634.

    @techtonic: if I recall correctly I explained in your issue that had the patch what the problem was. Short summary: there are two equally valid ways in which tabs in the output and the spacing in the sample output could be reconciled; both have their problems. Therefore it is better to have the user handle it explicitly one way or another.

    @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-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant