-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Current problem
a = 1
b = 2
if a > b and b > a:
pass
compare.py:4:3: R1716: Simplify chained comparison between the operands (chained-comparison)
While it's nice some message that points to the error, it's not quite correct.
Desired solution
Detect circular comparisons which simplify to False
.
This is a graph pattern is solved with a topological sort. The main risks are the performance overhead for building the graph, which should be small because the number of statements in any given if
is usually low. There is also the complexity risk of having a graph and associated algorithms implementation embedded in the code.
Having the graph could open up the following:
Could see if we can suggest simplifying if a >= b and b <= a:
to if a == b:
but this could be more difficult. There might also be something we can do with or
statements.
Building the graph would also allow us to pick up situations like
elif a > 1 and a > 10:
pass
elif a < 100 and a < 10:
pass
which currently do not emit.
This issue came up while thinking about #5800. It I'm not immediately sure how to suggest the refactor with the current implementation. Building this graph might more readily suggests the refactor.
We could modify chained-comparison
to detect comparison cycles and do #5800. But unless there are objections I want to try the graph implementation because of the benefits described above.