Skip to content

needless-bool

Pythonstyleno fixdefault: offpython/ruff

Reports an if that returns True or False and is followed, directly or in an else, by a return of the opposite boolean.

The if only turns a condition into the bool it already is. The condition itself, or bool(...) of it, is the value, and the four lines hide that.

To fix, return the condition.

Reported
...
def f(x):
if x:
return True
return False
...
python:
lint:
rules:
needless-bool: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line needless-bool in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ruff SIM103 full