An if or ternary tests a literal, so the same branch runs every time
What it finds
if "debug":, x if [] else y: a condition that is a literal or a display has the same truth value every time, so one branch never runs. It is usually a name or a call that lost its parts.
True, False, None, 0 and 1 as conditions are left alone, since they switch code on and off on purpose, and so are while loops, where while True is a loop that ends with break.
From the rule's source, fixed_truth_condition.rs.
def run(flag, items): if 'debug':tx0 reports: the condition 'debug' has the same truth value every time return 1 elif []:tx0 reports: the condition [] has the same truth value every time return 2 if True or flag: return 3 if 0: return 4 value = 1 if (5,) else 2tx0 reports: the condition (5,) has the same truth value every time while True: break return value if items else None Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:fixed-truth-condition" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:fixed-truth-condition] reason="..."tx0 explain tx0/python:fixed-truth-condition shows the same in the terminal. Found a false positive? Report it.