Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for circular comparisons and other comparison improvements #5814

Open
areveny opened this issue Feb 16, 2022 · 4 comments · May be fixed by #7611
Open

Check for circular comparisons and other comparison improvements #5814

areveny opened this issue Feb 16, 2022 · 4 comments · May be fixed by #7611
Assignees
Labels
Enhancement ✨ Improvement to a component Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@areveny
Copy link
Contributor

areveny commented Feb 16, 2022

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.

@areveny areveny added Enhancement ✨ Improvement to a component Proposal 📨 labels Feb 16, 2022
@areveny areveny self-assigned this Feb 16, 2022
@areveny areveny changed the title Check for impossible circular comparisons Check for circular comparisons and other comparison improvements Feb 17, 2022
@areveny
Copy link
Contributor Author

areveny commented Mar 29, 2022

Thanks for merging. I have the core logic of this assembled, I'll finish up the documentation and add a PR now that we're on 2.14.

@Pierre-Sassoulas Pierre-Sassoulas added Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Proposal 📨 labels Jul 5, 2022
@Pierre-Sassoulas
Copy link
Member

@areveny do you still want to handle this ?

@areveny
Copy link
Contributor Author

areveny commented Jul 11, 2022

Yes! Thank you for checking. I have been sitting on this for quite a while. I will make time to get the PR in.

@Pierre-Sassoulas
Copy link
Member

No rush, I was just checking if I should clear the assignment :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants