Skip to content

Add checker for consistency of return statement #1267

Closed
@rogalski

Description

@rogalski

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:

def foo(x):
    if x >= 0:
        return math.sqrt(x)
    else:
        return None

def bar(x):
    if x < 0:
        return None
    return math.sqrt(x)

No:

def foo(x):
    if x >= 0:
        return math.sqrt(x)

def bar(x):
    if x < 0:
        return
    return math.sqrt(x)

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 in No: 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions