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

bpo-28249: fix lineno location for empty DocTest instances #30498

Merged
merged 4 commits into from May 19, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jan 9, 2022

This patch fixes lineno for DocTest instances for objects that don't have __doc__ attribute.

In the original issue this was used as an example:

def a():
    # """A"""
    pass

def b():  # line 5
    """B"""
    pass

def c():
    # """C"""
    pass

Before this patch, doctest.DocTestFinder(exclude_empty=False).find(example_module) was showing:

[
  <DocTest example from example.py:5 (no examples)>,
  <DocTest example.a from example.py:5 (no examples)>,
  <DocTest example.b from example.py:5 (no examples)>,
  <DocTest example.c from example.py:None (no examples)>,
]

Notice that lineno was 5 for example and example.a. Which is clearly wrong, because example.b is on the 5th line. But, example.c had lineno=None.

DocTest docs clearly state:

  • lineno: The line number within filename where this DocTest
    begins, or None if the line number is unavailable. This
    line number is zero-based, with respect to the beginning of
    the file.

So, now lineno is set to None when __doc__ cannot be found. Now, the result for our example module will be:

[
  <DocTest example from example.py:None (no examples)>, 
  <DocTest example.a from example.py:None (no examples)>,
  <DocTest example.b from example.py:5 (no examples)>, 
  <DocTest example.c from example.py:None (no examples)>, 
]

Now, everything looks correct.

https://bugs.python.org/issue28249

CC @corona10 as my mentor 🙂

Lib/doctest.py Outdated Show resolved Hide resolved
@AlexWaygood AlexWaygood added the type-bug An unexpected behavior, bug, or error label Jan 9, 2022
@github-actions
Copy link

github-actions bot commented Feb 10, 2022

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Feb 10, 2022
@sobolevn sobolevn removed the stale Stale PR or inactive for long period of time. label Feb 10, 2022
Copy link
Contributor

@MaxwellDupre MaxwellDupre left a comment

Ran 58 tests in 4.543s
OK
Looks ok.

@ambv
Copy link
Contributor

ambv commented May 19, 2022

I rebased and resolved a conflict with main.

@ambv ambv merged commit 8db2b3b into python:main May 19, 2022
13 checks passed
@miss-islington
Copy link
Contributor

miss-islington commented May 19, 2022

Thanks @sobolevn for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10, 3.11.
🐍🍒⛄1�7🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 19, 2022
…nGH-30498)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit 8db2b3b)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@miss-islington
Copy link
Contributor

miss-islington commented May 19, 2022

Sorry, @sobolevn and @ambv, I could not cleanly backport this to 3.10 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 8db2b3b6878aba9f12844526bce966b7eed81aee 3.10

@miss-islington
Copy link
Contributor

miss-islington commented May 19, 2022

Sorry @sobolevn and @ambv, I had trouble checking out the 3.9 backport branch.
Please backport using cherry_picker on command line.
cherry_picker 8db2b3b6878aba9f12844526bce966b7eed81aee 3.9

@bedevere-bot
Copy link

bedevere-bot commented May 19, 2022

GH-92978 is a backport of this pull request to the 3.11 branch.

ambv added a commit that referenced this pull request May 19, 2022
) (GH-92978)

(cherry picked from commit 8db2b3b)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
ambv pushed a commit to ambv/cpython that referenced this pull request May 19, 2022
…pythonGH-30498)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit 8db2b3b)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@bedevere-bot
Copy link

bedevere-bot commented May 19, 2022

GH-92981 is a backport of this pull request to the 3.10 branch.

ambv added a commit that referenced this pull request May 19, 2022
…GH-30498) (#92981)

(cherry picked from commit 8db2b3b)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants