Early return should not take `return None` form when it is not required
See original GitHub issueRule request
Thesis
Right now this code:
def _check_parent(self, node: ast.If) -> None:
parent = get_parent(node)
if parent is None:
return None
if isinstance(parent, _ELSE_NODES):
body = parent.body + parent.orelse
else:
body = getattr(parent, 'body', [node])
next_index_in_parent = body.index(node) + 1
if keywords.next_node_returns_bool(body, next_index_in_parent):
self.add_violation(SimplifiableReturningIfViolation(node))
passes our style check. Note return None
part there. It is the only return
. And it means that None
part is not required.
Moreover, it should not be allowed. Simple early returns must be just
return`.
Issue Analytics
- State:
- Created 2 years ago
- Comments:21 (20 by maintainers)
Top Results From Across the Web
Is it ok to skip "return None"? - python - Stack Overflow
Yes, if you do not return any value from a Python function, it returns None. So, whether to explicitly return None is a...
Read more >return None or not to return None : r/Python - Reddit
So do you guys do the same thing? Whats wrong with returning None in the first place? What strategies do you use to...
Read more >Allow to return None without noqa · Issue #2323 - GitHub
I tried without the return line: 157:1 DAR202 Excess "Returns" in Docstring: + return """Get files key, to upload to. If None, uploaded...
Read more >The Python return Statement: Usage and Best Practices
If your function has multiple return statements and returning None is a valid option, then you should consider the explicit use of return...
Read more >inspect — Inspect live objects — Python 3.11.1 documentation
Return the name of the Python source file in which an object was defined or None if no way can be identified to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Are you running pythno3.9?
Ok, we also need to take a closer look at:
I would re-formulate the rule as: if all function
return
nodes arereturn None
-> than we must use plainreturn
.@DhirajChauhan40 Something like (untested):
You can put it here: https://github.com/wemake-services/wemake-python-styleguide/blob/d41d4b9d70dcad0ece5e7ed7320560de58999fcf/wemake_python_styleguide/visitors/ast/keywords.py#L123
You would still have to:
violation
type inviolations/consistency.py