Skip to content

if-expr-with-true-false

Pythonstyleno fixdefault: offpython/ruff

Reports a conditional expression whose arms are True and False: True if a else False.

The expression is the truth of a. When a is already a comparison, it is that comparison.

To fix, write bool(a), or a itself if it is already a bool.

Reported
...
x = not a < b
x = not a == b == c
...
x = True if a else False
...
x = 1 if a else 1
...
After the fix
...
x = not a < b
x = not a == b == c
...
x = True if a else False
...
x = 1 if a else 1
...
python:
lint:
rules:
if-expr-with-true-false: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line if-expr-with-true-false 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 SIM210 full