Skip to content

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 🙂

@AlexWaygood AlexWaygood added the type-bug An unexpected behavior, bug, or error label Jan 9, 2022
@github-actions
Copy link

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 added the needs backport to 3.11 only security fixes label May 19, 2022
@ambv ambv merged commit 8db2b3b into python:main May 19, 2022
@miss-islington
Copy link
Contributor

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.
🐍🍒⛏🤖

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

Co-authored-by: Kumar Aditya <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
(cherry picked from commit 8db2b3b)

Co-authored-by: Nikita Sobolev <[email protected]>
@miss-islington
Copy link
Contributor

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

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

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 <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
Co-authored-by: Nikita Sobolev <[email protected]>
ambv pushed a commit to ambv/cpython that referenced this pull request May 19, 2022
…pythonGH-30498)

Co-authored-by: Kumar Aditya <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
(cherry picked from commit 8db2b3b)

Co-authored-by: Nikita Sobolev <[email protected]>
@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label May 19, 2022
@bedevere-bot
Copy link

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 <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
Co-authored-by: Nikita Sobolev <[email protected]>
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.

8 participants