Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
require Return section only if return is not None nor commentary #25008
require Return section only if return is not None nor commentary #25008
Changes from 33 commits
575abc4
4b6406f
b204f11
0a76a3a
d35ec84
e7d9bed
2cdd367
0e25d93
d5d9800
073a6b6
0143b7c
76188dd
2051ea6
ff17a79
bafc25d
172115a
d5e0de8
0e61d2c
4c89beb
b04110a
4b01092
0928ba6
57fdb1e
00d5fdb
0a0c05e
6ab2534
504ea10
17737d1
fe23351
783eb39
2c41bd7
6e53215
2c60e4d
11b827b
3e93cbe
1660060
be6c906
0b361de
2ebfa33
65b0f56
bbf39d0
2c66c89
d99edb3
07a4b01
4d6ec7e
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
can you add a test for a property that returns something (without the Returns section, which is the correct).
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.
such a test was already present before this PR:
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.
it is negative test, not like the one being commented here, so it's inside the BadReturns class
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.
What is happening here is that, when
inspect.getsource()
runs, it returns the function's code as it is on the source file, indentation included. So, for example, if the function is a method:Getting the source of B will return
with the previously present indentation kept.
If we feed this directly to
ast.parse()
, it causes an indentation error, since the first thing read on the string is a space, and it is unexpected.An easy fix is to apply
.strip()
to the string containing the function source, which remove all whitespaces in the beginning and the end. In my example, the result would be:Which solves the previous problem.
The thing is, on more complex cases, this approach isn't enough. Consider the real example:
By applying
.strip()
, we get:This leaves the
@Appender
(I think it's a decorator) 1 indentation level bellow the function's signature, which is not proper syntax and raises another indentation error.To avoid this problem, the present solution removes the same number os whitespaces from each line, as if the function was declared at an otherwise empty file:
That said, I'm open to suggestions if someone can think of a simpler way to solve the described problem.
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.
Hmm is there any way to simplify what is going on here? Is there no way to extract the
.returns
from the AST directly instead of having to recurse everything?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.
In our case, there isn't.
The AST provides the
.walk()
method to iterate through nodes. According to the doc, itWithout context, there is no way to tell if a return node is inside a nested inner function or not, which is needed for one of the requirements raised by @datapythonista.
I explain this a bit further here.
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.
As opposing to using
ast.walk()
to iterate over all the nodes on the method's AST, here I'm recursively walking the tree myself.The reason for that is to respect cases like the following:
In this case, there is a inner function which returns a string, but the function being evaluated itself always return None, thus shouldn't have a Returns section on the docstring.
If I read all the nodes indiscriminately, as happens with
ast.walk()
, I can't distinguish returns inside nested functions from returns on the function actually being evaluated. If I search trough it myself, though, I can prune the recursion when I reach a (nested) function node, thus ignoring it's entire subtree and possible return nodes in it.