blank-line-before-branch

Require branch statements to be visually separated in large suites.

Message

Missing blank line before return/raise/break/continue in a large suite.

Settings

SettingDescriptionTypeDefault
allow_guard_ladder_final_branch Allow compact final branches in guard-ladder control flow. bool True
allow_related_return_tails Allow compact returns that immediately return a just-created value. bool True
compact_tail_max_statements Maximum compact tail size allowed before a final branch statement. int 2
max_suite_non_empty_lines Minimum non-empty suite size before branch statements require spacing. int 2

Valid examples

def f(value: int) -> int:
    x = value + 1
    y = x + 1

    return y
Show more
def f(value: int) -> int:
    x = value + 1
    return x
def f(parts: list[str]) -> dict[str, int]:
    cleaned = [part.strip() for part in parts]
    joined = ",".join(cleaned)
    payload: dict[str, int] = {"count": len(cleaned), "width": len(joined)}
    return payload
def f(value: int) -> int:
    x = value + 1
    y = x + 1
    z = y + 1
    # comment separator
    return z
def f() -> int:
    """Return constant."""
    return 1
    value = 2
def f(value: int) -> int:
    x = value + 1
    y = x + 1
    return y
async def f() -> None:
    try:
        work()
    except Exception:
        cleanup_a()
        cleanup_b()
        await cleanup_c()
        collector_id = None
        raise
async def f() -> None:
    try:
        work()
    except Exception:
        cleanup()
        state = None
        log_error()
        raise
async def f() -> None:
    try:
        work()
    finally:
        cleanup()
        log_teardown()
        return
def f(created_at: object) -> object:
    payload = {"created_at": created_at}
    return ArchivedPost(created_at=created_at, payload=payload)
def f(shell_name: str, interactive: bool) -> list[str]:
    if shell_name == "zsh":
        return ["-lic"]
    if interactive:
        return ["-ic"]
    return ["-lc"]
def f(values: list[int]) -> int:
    total = 0
    for value in values:
        total += value
    return total

Invalid examples

def f(value: int) -> int:
    x = value + 1
    y = x + 1
    z = y + 1
    return z

Suggested fix

def f(value: int) -> int:
    x = value + 1
    y = x + 1
    z = y + 1

    return z
Show more
def f(values: list[int]) -> int:
    total = 0
    message = str(total)
    flag = bool(message)
    raise RuntimeError("boom")

Suggested fix

def f(values: list[int]) -> int:
    total = 0
    message = str(total)
    flag = bool(message)

    raise RuntimeError("boom")
def f(value: int) -> int:
    x = value + 1
    return x

Suggested fix

def f(value: int) -> int:
    x = value + 1

    return x
def f(parts: list[str]) -> dict[str, int]:
    cleaned = [part.strip() for part in parts]
    joined = ",".join(cleaned)
    payload: dict[str, int] = {"count": len(cleaned), "width": len(joined)}

    return payload

Suggested fix

def f(parts: list[str]) -> dict[str, int]:
    cleaned = [part.strip() for part in parts]
    joined = ",".join(cleaned)
    payload: dict[str, int] = {"count": len(cleaned), "width": len(joined)}
    return payload