Skip to content
Sarj Code Standards

no-nonlocal-statement

Reject nonlocal statements; pass state explicitly or give it a typed owner.

Why

Rebinding an enclosing function's variables hides state changes across callable boundaries. An explicit return value or typed state owner makes that dependency visible.

Fix

Return the updated value and assign it in the caller, or move callback state onto a typed owner. Do not just delete the declaration: that changes Python's name binding and can break the callable.

Examples

Before — flagged A callback rebinds its enclosing function's state
app/counter.py
def increment(value: int) -> int:
def update() -> None:
nonlocal value
value += 1
update()
return value
After — preferred The caller assigns an explicit return value
app/counter.py
def increment(value: int) -> int:
def update(current: int) -> int:
return current + 1
return update(value)