Both operands of an operator are the same
What it finds
a and a, x < x: an operator applied to the same operand twice gives a fixed or redundant result, which usually means one side was meant to be something else.
Left out are == and !=, since x != x is the test for a NaN; is, since obj.attr is obj.attr tests that a property caches its value; and arithmetic, since x - x tells a finite float from an infinite one. Operands that call, await or assign anything may give different values each time, and operands without names are constants that read as intended. A comparison inside an assertion, as in assert not x < x or self.assertFalse(x < x), tests the operator itself.
From the rule's source, identical_operands.rs.
def check(a, b, item, items, test): if a is not None and a is not None:tx0 reports: both operands of and are a is not None pass if item.start < item.start:tx0 reports: both operands of < are item.start pass if a or a:tx0 reports: both operands of or are a pass if items[0] >= items[0]:tx0 reports: both operands of >= are items[0] pass if a != a or a == a or a - a or a is a: pass if next(items) < next(items) or 1 < 1: pass if a and b: pass assert not a < a test.assertFalse(b > b) return (a < b) and (a < b)tx0 reports: both operands of and are a < b Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/python:identical-operands" = "off"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:identical-operands] reason="..."tx0 explain tx0/python:identical-operands shows the same in the terminal. Found a false positive? Report it.