question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Early return should not take `return None` form when it is not required

See original GitHub issue

Rule 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:closed
  • Created 2 years ago
  • Comments:21 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
sobolevncommented, Aug 31, 2021

Are you running pythno3.9?

1reaction
sobolevncommented, Aug 29, 2021

Ok, we also need to take a closer look at:

def some_funct():
    if first_cond:
        return None
    if second_cond:
        return None
    print('a')

I would re-formulate the rule as: if all function return nodes are return None -> than we must use plain return.

@DhirajChauhan40 Something like (untested):

is_all_none = (
            issubclass(returning_type, ast.Return) and
            has_values and
            all(
                ret_node.value.value is None
                for ret_node in return_nodes
                if isinstance(ret_node.value, (compat.nodes.Constant, ast.NameConstant))
            )
        )
        if is_all_none:
            self.add_violation(...)

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:

  1. Add new violation type in violations/consistency.py
  2. Add tests
  3. Have fun 🙂
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found