Skip to content

Allow doctest to find line number of __test__ strings if formatted as a triple quoted string. #69113

Closed

Description

@bitdancer
BPO 24925
Nosy @jneb, @bitdancer, @jayvdb, @CuriousLearner, @nasirhjafri
PRs
  • bpo-24925: _find_lineno now finds doctest __test__ string line numbers #17553
  • Files
  • doctest_find__test__.patch
  • doctestfix.py: Simple addition for doctest
  • 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/CuriousLearner'
    closed_at = None
    created_at = <Date 2015-08-24.16:20:04.640>
    labels = ['easy', '3.8', 'type-bug', '3.7']
    title = 'Allow doctest to find line number of __test__ strings if formatted as a triple quoted string.'
    updated_at = <Date 2019-12-31.13:04:09.450>
    user = 'https://github.com/bitdancer'

    bugs.python.org fields:

    activity = <Date 2019-12-31.13:04:09.450>
    actor = 'jneb'
    assignee = 'CuriousLearner'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2015-08-24.16:20:04.640>
    creator = 'r.david.murray'
    dependencies = []
    files = ['40242', '47482']
    hgrepos = []
    issue_num = 24925
    keywords = ['patch', 'easy']
    message_count = 12.0
    messages = ['249060', '253317', '253319', '313757', '313758', '314454', '330253', '330460', '334200', '334211', '358193', '359114']
    nosy_count = 5.0
    nosy_names = ['jneb', 'r.david.murray', 'jayvdb', 'CuriousLearner', 'nasirhjafri']
    pr_nums = ['17553']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue24925'
    versions = ['Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    Linked PRs

    Activity

    1. bitdancer commented on Aug 24, 2015

      @bitdancer
      MemberAuthor

      Attached is a patch that enhances doctest discovery so that it can correctly report the line number for doctest errors in tests that are string values of the __test__ dictionary. It only works if the strings are formatted as triple quoted strings, but since that is the most common case I think it is worth doing.

      I don't have a test for this...I think it will require a unit test rather than adding to the doctests, since it needs to check the reported line number and other changes to test_doctest.py would change the line numbers.

    2. jneb commented on Oct 22, 2015

      jnebmannequin
      Mannequin

      I am not as good in making nice patches, but that code can be improved upon a bit as follows:
      <insert patch intro blurb here, same as original>

      + if isinstance(obj, str) and source_lines is not None:
      + # This will find __test__ string doctests if and only if the string
      + # contains any unique line.
      + for offset,line in enumerate(obj.splitlines(keepends=True):
      + if source_lines.count(line)==1:
      + lineno = source_lines.index(line)-offset
      + break

               # We couldn't find the line number.
               return None

      I thing this will improve legibility and probably also speed under most circumstances.

    3. jneb commented on Oct 22, 2015

      jnebmannequin
      Mannequin

      Oops. Lousy editing, I should have used return instead of break.

      These 6 lines can be inserted in lib/doctest at line 1100:
      + if isinstance(obj, str) and source_lines is not None:
      + # This will find __test__ string doctests if and only if the string
      + # contains any unique line.
      + for offset,line in enumerate(obj.splitlines(keepends=True):
      + if source_lines.count(line)==1:
      + return source_lines.index(line)-offset

      And it works fine for me; the code is quite general and has predictable behaviour: any test string with a unique line in it will work.

    4. jneb commented on Mar 13, 2018

      jnebmannequin
      Mannequin

      I always use the following piece of code that can be trivially used to patch the source.
      It basically looks for a using line in the test string and finds it in the source file. If there is a match, we know where we are. Otherwise it falls back to the "normal" way.

    5. jneb commented on Mar 13, 2018

      jnebmannequin
      Mannequin

      Oh wait, I already posted that so long ago I forgot about it.
      Oops.
      Sorry about the repetition, folks.

      • Jurjen
    6. CuriousLearner commented on Mar 26, 2018

      @CuriousLearner
      Member

      Hey Jurjen,

      Do you mind converting your patch in a Pull request please?

    7. CuriousLearner commented on Nov 22, 2018

      @CuriousLearner
      Member

      Hey R. David Murray,

      Where do you think the unit test for doctest should be placed?

      Should it be in test_doctest.py?

    8. bitdancer commented on Nov 26, 2018

      @bitdancer
      MemberAuthor

      Without looking at doctest.py, yes. I believe the doctests in that file should be already plugged in to the unittest framework, so adding new testcase containing a non-doctest unit test should work fine.

    9. nasirhjafri commented on Jan 22, 2019

      nasirhjafrimannequin
      Mannequin

      Hi, Should I convert patch into PR?
      Thanks

    10. jneb commented on Jan 22, 2019

      jnebmannequin
      Mannequin

      Yes. That would make me happy. In the meantime I learned how to use git, so maybe I'll do the pull request myself next time. Thanks for the work.

    11. 2 remaining items

    12. transferred this issue fromon Apr 10, 2022
    13. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Nov 23, 2023
    14. added a commit that references this issue on Nov 16, 2025
    15. added 3 commits that reference this issue on Nov 30, 2025
    16. added a commit that references this issue on Dec 6, 2025
    17. bitdancer commented on Dec 6, 2025

      @bitdancer
      MemberAuthor

      Thanks to everyone who worked on this. It's nice to close an old issue as a success.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions