You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow doctest to find line number of __test__ strings if formatted as a triple quoted string. #69113
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=Nonecreated_at=<Date2015-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=<Date2019-12-31.13:04:09.450>user='https://github.com/bitdancer'
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.
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.returnNone
I thing this will improve legibility and probably also speed under most circumstances.
jneb commented on Oct 22, 2015
jnebmannequin
Mannequin
More actions
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.
jneb commented on Mar 13, 2018
jnebmannequin
Mannequin
More actions
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.
jneb commented on Mar 13, 2018
jnebmannequin
Mannequin
More actions
Oh wait, I already posted that so long ago I forgot about it.
Oops.
Sorry about the repetition, folks.
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.
nasirhjafri commented on Jan 22, 2019
nasirhjafrimannequin
Mannequin
More actions
Hi, Should I convert patch into PR?
Thanks
jneb commented on Jan 22, 2019
jnebmannequin
Mannequin
More actions
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.
Activity
bitdancer commentedon Aug 24, 2015
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.
jneb commentedon Oct 22, 2015
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
I thing this will improve legibility and probably also speed under most circumstances.
jneb commentedon Oct 22, 2015
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.
jneb commentedon Mar 13, 2018
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.
jneb commentedon Mar 13, 2018
Oh wait, I already posted that so long ago I forgot about it.
Oops.
Sorry about the repetition, folks.
CuriousLearner commentedon Mar 26, 2018
Hey Jurjen,
Do you mind converting your patch in a Pull request please?
CuriousLearner commentedon Nov 22, 2018
Hey R. David Murray,
Where do you think the unit test for
doctestshould be placed?Should it be in
test_doctest.py?bitdancer commentedon Nov 26, 2018
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.
nasirhjafri commentedon Jan 22, 2019
Hi, Should I convert patch into PR?
Thanks
jneb commentedon Jan 22, 2019
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.
2 remaining items
pythongh-69113: Fix doctest to report line numbers for __test__ strings
pythongh-69113: Fix doctest to report line numbers for __test__ strings
pythongh-69113: Fix doctest to report line numbers for __test__ strings
pythongh-69113: Fix doctest to report line numbers for __test__ strings
gh-69113: Fix doctest to report line numbers for __test__ strings (#1…
bitdancer commentedon Dec 6, 2025
Thanks to everyone who worked on this. It's nice to close an old issue as a success.