Skip to content

if-expr-with-twisted-arms

Pythonstyleno fixdefault: off

Reports a conditional expression of the form b if not a else a.

b if not a else a gives a when a is true and b otherwise, which is a or b. The negation only puts the arms in the opposite order, and a condition that is not negated is easier to read.

To fix, write a or b, or swap the arms and drop the not.

Reported
...
x = 1 if a else 1
x = b if not a else a
x = b if a else a
After the fix
...
x = 1 if a else 1
x = b if not a else a
x = b if a else a
python:
lint:
rules:
if-expr-with-twisted-arms: warn # off (default), warn, error

Suppress one line with sudsy-disable-next-line if-expr-with-twisted-arms 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 SIM212 full