Skip to content

Fix line detection for properties in doctest tests #6086

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

Merged
merged 1 commit into from
Nov 22, 2019

Conversation

kondratyev-nv
Copy link
Contributor

For #6082. Doctest itself does not take into account @property. Overriding the line detection method with an additional condition on isdatadescriptor helped.

blueyed
blueyed previously approved these changes Oct 28, 2019
Copy link
Contributor

@blueyed blueyed left a comment

Choose a reason for hiding this comment

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

Doesn't look too bad to me!

@blueyed
Copy link
Contributor

blueyed commented Oct 28, 2019

Is this applicable to Python itself? (then you should consider creating an issue for them, and link it here)

@blueyed
Copy link
Contributor

blueyed commented Oct 28, 2019

src/_pytest/doctest.py has unexpect coverage changes (might be flaky, but looks suspicious (it did not show the lines though)) - please retry it (apply my change, and rebase).

https://bugs.python.org/issue17446
"""
if inspect.isdatadescriptor(obj):
obj = obj.fget
Copy link
Member

Choose a reason for hiding this comment

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

this breaks for other descriptors
please check for property explicitly or sort it differently

Copy link
Contributor

Choose a reason for hiding this comment

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

@RonnyPfannschmidt thanks!
Would be good to have this covered in a test then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! I've also noticed it, working on a fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

Use the code from the PR maybe? python/cpython#3419

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion! Indeed, I will replace the condition with an explicit check for the property. However, I did not find a way to write a doctest test for a case when the function/method is a data descriptor, but not property.

@kondratyev-nv
Copy link
Contributor Author

It looks like the drop in coverage is because there are no tests when the line number is not detected. However, I did not found a way to write such a test. If anyone has some thoughts on this, I will appreciate suggestions!

@RonnyPfannschmidt
Copy link
Member

@kondratyev-nv creating a simple non-data `DistinctProperty" that wraps it could help

basically anything that has a doc and is not a normal property as far as i remember

@kondratyev-nv
Copy link
Contributor Author

kondratyev-nv commented Oct 29, 2019

@RonnyPfannschmidt To be honest, I did not completely understand you, but I've come up with kind of a strange test where line number is still not detected. Please, take a look.
Also, it looks like we can't create something that is not a normal property and so that doctest would find it. On the test discovery stage there is a condition on whether to gather __doc__s - https://github.com/python/cpython/blob/da6ce58dd5ac109485af45878fca6bfd265b43e9/Lib/doctest.py#L1025.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks a lot, great work!

@@ -0,0 +1 @@
Fix line detection for properties in doctest tests
Copy link
Member

Choose a reason for hiding this comment

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

I suggest we link to the issue:

Fix line detection for doctest samples inside ``property`` docstrings, as workaround to `bpo-17446 <https://bugs.python.org/issue17446>`__.

Also good news is that there's a PR open: python/cpython#3419

Finally, could you please rebase your changes and squash all commits? Let me know if you rather us do it. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicoddemus Thanks! I've added a link to the issue as you suggested and squashed all commits. I saw this PR to cpython, however, it's not active since Jan 2, 2018 :(

Choose a reason for hiding this comment

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

Sorry about that -- I just haven't been able to figure out what the reviewer wanted in it. Will try to get it in again soon.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks @kondratyev-nv!

@kondratyev-nv
Copy link
Contributor Author

@blueyed It looks like there is some error in assertions. I will fix tests and squash commits if you don't mind.

@blueyed
Copy link
Contributor

blueyed commented Nov 22, 2019

@blueyed It looks like there is some error in assertions. I will fix tests and squash commits if you don't mind.

👍 (I do not mind getting credit for this (i.e. you do not have to keep my commit separate, but you could use Co-Authored-By if you like to))

@blueyed blueyed merged commit d1eb89d into pytest-dev:master Nov 22, 2019
@blueyed
Copy link
Contributor

blueyed commented Nov 22, 2019

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants