Check for circular comparisons and other comparison improvements #5814
Labels
Enhancement ✨
Improvement to a component
Needs PR
This issue is accepted, sufficiently specified and now needs an implementation
Current problem
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:
toif a == b:
but this could be more difficult. There might also be something we can do withor
statements.Building the graph would also allow us to pick up situations like
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.The text was updated successfully, but these errors were encountered: