Skip to content

Latest commit

 

History

History
122 lines (91 loc) · 2.29 KB

File metadata and controls

122 lines (91 loc) · 2.29 KB

flake8-return Style Guide (Python 3.13)

The flake8-return rules ensure consistent and explicit return behaviour, Ensuring your functions are clear in intent and free from unnecessary control flow. Follow these rules:

R501 — Avoid Explicit return None if It's the Only Return

# BAD:
def func():
    return None

# GOOD:
def func():
    return

Use return alone instead of return None when the function's only result is None.


R502 — Avoid Implicit None in Functions That May Return a Value

# BAD:
def func(x):
    if x > 0:
        return x
    # implicitly returns None (bad)

# GOOD:
def func(x):
    if x > 0:
        return x
    return 0

Ensure all branches explicitly return a value if any branch does.


R503 — Add an Explicit Return at the End

# BAD:
def func(x):
    if x > 0:
        return x
    # no return (bad)

# GOOD:
def func(x):
    if x > 0:
        return x
    return -1

Don't rely on implicit None—always return something at the end.


R504 — Avoid Redundant Variable Assignment Before return

# BAD:
def func():
    result = compute()
    return result

# GOOD:
def func():
    return compute()

Inline return expressions unless the variable is reused meaningfully before returning.


R505–R508 — Eliminate Unnecessary else After Terminal Statements

Avoid else after return, raise, break, or continue. These statements already exit control flow.

# BAD:
if cond:
    return x
else:
    return y

# GOOD:
if cond:
    return x
return y

This applies similarly for raise, break, and continue.

# BAD:
for x in xs:
    if x > 0:
        break
    else:
        log()

# GOOD:
for x in xs:
    if x > 0:
        break
    log()

These rules apply to regular and async def functions alike.


Use the flake8-return rules to enforce predictable and clean return logic, enhancing readability and correctness.