-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add checker for consistency of return statement #1267
Comments
I personally like no-else-return, but we can move it to a less important stage in 2.0. Regarding the issue itself, sounds okay, although, now that we are talking about no-else-return, I would put it in the same stage / tier, meaning a kind of nitpick. For example, the refactoring checker has some checks that are pretty useful, that can make the code more readable, while these two are just visually pleasant. |
I disagree about it being just a 'visually pleasant' - it may be an indicator of messy interface. If function sometimes returns value, sometimes returns early "without a value", what does return value represent? If return is meant to be |
I am sorry but let's agree to disagree. There was no problem with having no return until the PEP 8 specifically mandated it. |
I'm getting ` def to_json(self, pretty=False):
Any explanation as to why I'm seeing this lint error? Doesn't seem consistent with the explanation given in this thread. |
Try to use the following changes: ...
if pretty:
return json.dumps(raw_survey, indent=2)
- else:
- return json.dumps(raw_survey)`
+ return json.dumps(raw_survey)` if the sentence I means, you don't need a More info about: http://eslint.org/docs/rules/no-else-return |
Makes quite a lot of sense! Thanks. |
To any beginner contributors, this will involve implementing a new check, likely on the One idea for an implementation might be to create a stack of the return statements in each functiondef by defining visit_functiondef and leave_functiondef on the checker, similarly to what the variables checker does (https://github.com/PyCQA/pylint/blob/30f0ff9178f75385c63ee0c445223641494a95aa/pylint/checkers/variables.py#L563), except that you would be storing a list of the return nodes in the function, and checking a visited return node against the return nodes in the stack. |
I'm working on it. Could you please add a "Work in progress" label? |
Steps to reproduce
Consider PEP8:
Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable).
Yes:
No:
Current behavior
R: 2, 4: Unnecessary "else" after "return" (no-else-return)
for foo method of Yes section.Expected behavior
inconsistent-returns
emitted for samples inNo:
section. Message should be emitted for function and not individual returns (as inconsistency occurs in function). Accepted functions are those that have (only returns with explicit value and no implicit return) or (only empty returns and implicit return at the end).Also,
no-else-return
seems more like a nitpick and less like a valuable check.pylint --version output
master
Since PyCQA/pycodestyle#431 seems to be rejected due to 'no AST' policy, it likely should be covered by pylint.
The text was updated successfully, but these errors were encountered: