Consistent Return

ID

python.consistent_return

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Python

Tags

reliability, suspicious-construct

Description

Reports functions whose body mixes value-returning return <expr> statements with bare return (or return None) statements. The mix usually means a refactor left one branch unfinished, or the author forgot a value on one path; either way the function’s contract is unclear and callers have to inspect every branch to know what they get back.

def get(key):                       # FLAW — mixed returns
    if key in cache:
        return cache[key]
    elif key.startswith("_"):
        return                      # bare
    return None                     # explicit None

def consistent(key):                # OK — every branch returns a value
    if key in cache:
        return cache[key]
    return None

Rationale

return and return None look interchangeable but the type-system view of them is different: return is "no value", return None is "the value None". Mixing the two with return <something_else> makes the function signature ambiguous to readers and to type-checkers, and is one of the most common shapes for "this function sometimes returns nothing" bugs caught in code review.

Remediation

Pick one shape for the function and apply it consistently:

  • If the function returns a value sometimes, return None explicitly on the no-value paths.

  • If the function is purely side-effecting, drop the return <value> statements.