not is applied to a comparison that has an operator for the opposite test
What it finds
not a == b, not x in items and not x is None have direct forms: a != b, x not in items and x is not None, which read in one go. Comparisons with < and the like are left alone, since for some values, such as NaN and sets, the opposite operator does not mean the negation. The fix writes the opposite operator; it is safe for in and is, and unsafe for == and !=, whose methods a class may define independently.
From the rule's source, negated_comparison.rs.
def check(a, b, items): if not a == b:tx0 reports: not negates a comparison; != tests the opposite directly return 1 if not (a in items):tx0 reports: not in tests the opposite directly return 2 if not a is None:tx0 reports: is not tests the opposite directly return 3 return not a < b or not a == b == 1 or a not in items Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:negated-comparison" = "note"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:negated-comparison] reason="..."tx0 explain tx0/python:negated-comparison shows the same in the terminal. Found a false positive? Report it.