-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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-30181: parse docstring using pydoc #1505
bpo-30181: parse docstring using pydoc #1505
Conversation
PEP 257 specifies that tools which parse docstrings should strip surrounding blank lines *before* determining what is the first line <URL:https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation>.
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA. This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. Thanks again to your contribution and we look forward to looking at it! |
Apparently trivial changes don't require a CLA. Does this count as a trivial change? In any case, the changes are from Ben Finney, not myself, so I suspect it isn't going to help if I sign the CLA. |
The two tests that fail don't appear to be valid PEP 257 docstring to me. So I think maybe the tests need to be modified. |
Lib/unittest/case.py
Outdated
""" | ||
Return the test case short description from a docstring. | ||
|
||
Ths docstring is parsed by the standard `pydoc.splitdoc` function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an implementation detail. Both short_description_from_docstring
and pydoc.splitdoc
are not public API, therefore the docstring is interested only for readers of these sources. It can be just a comment. But there is no much sense to just repeat the code by words in a docstring or comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There appears to be different ideas at play here surrounding the use docstrings. With one view being docstrings should only document the public API, so users of the API know how to use the package. The other view being docstrings are equally applicable for internal functions, which should have a well defined API for internal use, so internal programmers can tell what the function does without peering into the source. Any external references to help justify one or the other side of this argument appreciated (I have no opinions on this yet myself).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have also had the out of band comment from Ben saying just because it isn't a public API doesn't make the docstring any less useful. Since a docstring will be available to anyone introspecting the object whether private or public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstrings of internal functions are not forbidden, they are just not necessary. Comments would be enough (see PEP 8).
In any case neither comment, nor docstring shouldn't just repeat the code. They should describe what function does, but not how it does this. Otherwise any change of the code will make a docstring or a comment incorrect.
Note also that the function name itself serves the documenting function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
def short_description_from_docstring(text):
# Return the summary line from a docstring.
# If there is no summary line, return None.
...
The use of the word "summary line" comes straight from PEP-257.
Lib/unittest/test/test_case.py
Outdated
@unittest.skipIf( | ||
sys.flags.optimize >= 2, | ||
"Docstrings are omitted with -O2 and above") | ||
def testShortDescriptionWithSurroundingSpaceOneLineDocstring(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is enough to just add spaces or newlines in the two preceding tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, not sure I understand. You appear to be saying that we should use the same docstring as for the testShortDescriptionWithOneLineDocstring
test but with extra white space. However it is a different test - doesn't that mean it needs a different doc string to describe how it is different from the previous docstring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean that adding spaces at the start and end of the docstring of testShortDescriptionWithOneLineDocstring and adding a newline and indentation at the start of the docstring of testShortDescriptionWithMultiLineDocstring will cover cases tested by new testes. No new tests will be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, OK, I think I understand now. So to clarify, you feel there is no need to have a separate test for the no-leading/trailing whitespace case?
FYI Ben Finney has signed the CLA, although this is not a trivial change if you make any changes to the PR yourself, @brianmay then we will need you to sign the CLA, so please do so. |
Lib/unittest/case.py
Outdated
""" | ||
Return the test case short description from a docstring. | ||
|
||
Ths docstring is parsed by the standard `pydoc.splitdoc` function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstrings of internal functions are not forbidden, they are just not necessary. Comments would be enough (see PEP 8).
In any case neither comment, nor docstring shouldn't just repeat the code. They should describe what function does, but not how it does this. Otherwise any change of the code will make a docstring or a comment incorrect.
Note also that the function name itself serves the documenting function.
Lib/unittest/case.py
Outdated
doc = self._testMethodDoc | ||
return doc and doc.split("\n")[0].strip() or None | ||
|
||
""" Return a one-line description of the test, if any; otherwise None. """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shortDescription()
is a public API. Your change removes useful information from its docstring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that the previous description is entirely relevant. Also does saying "The default implementation" add any useful information? How about:
def shortDescription(self):
"""Returns a one-line description of the test, or None if no
description has been provided.
This method returns the summary line of
the specified test method's docstring, as per PEP-257.
"""
...
Lib/unittest/test/test_case.py
Outdated
@unittest.skipIf( | ||
sys.flags.optimize >= 2, | ||
"Docstrings are omitted with -O2 and above") | ||
def testShortDescriptionWithSurroundingSpaceOneLineDocstring(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean that adding spaces at the start and end of the docstring of testShortDescriptionWithOneLineDocstring and adding a newline and indentation at the start of the docstring of testShortDescriptionWithMultiLineDocstring will cover cases tested by new testes. No new tests will be needed.
I have signed the CLA - is it possible to remove "CLA not signed" tag? |
Ah, I see the issue. Your PR has code from https://github.com/benf-wspdigital who has not signed the CLA, so the bot is blocking it as the provenance of the code isn't CLA-clean. Any way of getting that git user to sign the CLA? |
I am confused; that looks like Ben Finney's account, and you previously said he had signed the CLA. |
The bot doesn't recognize him because he has not added his Github username to his profile at bugs.python.org. |
@encukou is right. Ben Finney has signed based on his bugs.python.org account, but the bot nor I can verify that benf-swpdigital on GitHub is Ben Finney. |
GitHub determines attribution by the e-mail in the commit. This is just text, and easily spoofable – notice how the commits here ended up attributed to Ben without any action on his part, and without GitHub's knowledge of what's on b.p.o. Anyway, instead you probably want to verify the patches here are the ones from Ben, i.e. compare them to the ones on b.p.o. |
Just an FYI, this whole CLA issue of people being nice and opening PRs on other people's behalf is being discussed at python/devguide#195 . |
There would have been less confusion about who has not signed the CLA if the Knight's explanatory message began with "Hello :". |
Lib/unittest/case.py
Outdated
@@ -338,6 +339,25 @@ def __exit__(self, exc_type, exc_value, tb): | |||
.format(logging.getLevelName(self.level), self.logger.name)) | |||
|
|||
|
|||
def short_description_from_docstring(text): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function name should start with an underscore since it's not meant to be part of the public API. Also, I think a shorter name should be used like _make_short_description(docstring)
or _get_short_description(docstring)
. Information about the arguments can be gotten from the argument names or function docstring.
I talked with our lawyer and he said that as long as Ben and Brian have both signed the CLA then we are fine. So @brianmay can you just tell us your bugs.python.org username and I can manually check you also signed and then this PR will be cleared from a CLA perspective. |
I see that http://bugs.python.org/user14312 has "Contributor Form Received | Yes", bpo login "brian", GitHub login "brianmay". But I just removed the "CLA not signed" label, and the bot ... added it again, as you didn't sign it :-( I only see one bpo user with github login="brianmay". Could it be a mismatch based on the mail address? @brettcannon: Would you mind to take a look please? |
@Haypo This PR is kinda unique.. What happened is that @brianmay made the PR based on Ben Finney's patch. Because of this the bot tries to check if both Ben and Brian have signed the CLA. I see that both Ben and Brian have signed the CLA. I think from CLA perspective it's clear. Otherwise, If the conflict can be resolved, and if you approve this PR, I think this is good to go. |
I suggest to export the change as a patch (git diff master.. > patch), create a new PR (git checkout -b pydoc; git apply patch; git commit -a), and credit Ben Finney in the commit message, like:
|
On the PR itself, note that I have review comments from a couple months ago that haven't been addressed. |
Both Ben and Brian have signed the CLA and it's just Ben's refusal to set up a GitHub account with his bugs.python.org account that's causing the CLA bot to say this PR isn't in the clear. So as long as there are no other commits from anyone but Ben and Brian (which is what is currently true), then from a CLA perspective we're in the clear in this case. @cjerdonek can you change your review to "request changes" if you think they should be addressed before this PR is considered for acceptance? Plus there are merge conflicts that have to be fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For backwards compatibility reasons, the PR should preserve the behavior that docstrings of the form 'abc\ndef'
return the first line. It should also add a test for this case. While the PR can help ensure that PEP-257 compliant docstrings result in a useful result, it shouldn't require docstrings to be compliant to do so. (Many test-case docstrings out in the wild aren't.)
My suggestion would be to change the implementation of _get_short_description()
to match the current implementation, with a call to strip()
beforehand. The docstring of _get_short_description()
should also be updated accordingly.
To try and help move older pull requests forward, we are going through and backfilling 'awaiting' labels on pull requests that are lacking the label. Based on the current reviews, the best we can tell in an automated fashion is that a core developer requested changes to be made to this pull request. If/when the requested changes have been made, please leave a comment that says, |
I'm closing this as it was separately resolved by PR #18175 for https://bugs.python.org/issue39450 |
Rebased and copied Ben Finney's changes from http://bugs.python.org/issue30181
I hope master is the correct branch to merge this into.
https://bugs.python.org/issue30181