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

[pylint] Correct ordering of arguments in fix for if-stmt-min-max (PLR1730) #16080

Merged
merged 4 commits into from
Feb 12, 2025

Conversation

VascoSch92
Copy link
Contributor

The PR addresses the issue #16040 .


The logic used into the rule is the following:

Suppose to have an expression of the form

if a cmp b:
    c = d

where a, b, c and d are Python obj and cmp one of <, >, <=, >=.

Then:

  • if a=c and b=d

    • if <= fix with a = max(b, a)
    • if >= fix with a = min(b, a)
    • if > fix with a = min(a, b)
    • if < fix with a = max(a, b)
  • if a=d and b=c

    • if <= fix with b = min(a, b)
    • if >= fix with b = max(a, b)
    • if > fix with b = max(b, a)
    • if < fix with b = min(b, a)
  • do nothing, i.e., we cannot fix this case.


In total we have 8 different and possible cases.


| Case  | Expression       | Fix           |
|-------|------------------|---------------|
| 1     | if a >= b: a = b | a = min(b, a) |
| 2     | if a <= b: a = b | a = max(b, a) |
| 3     | if a <= b: b = a | b = min(a, b) |
| 4     | if a >= b: b = a | b = max(a, b) |
| 5     | if a > b: a = b  | a = min(a, b) |
| 6     | if a < b: a = b  | a = max(a, b) |
| 7     | if a < b: b = a  | b = min(b, a) |
| 8     | if a > b: b = a  | b = max(b, a) |

I added them in the tests.

Please double-check that I didn't make any mistakes. It's quite easy to mix up > and <.

Copy link
Contributor

github-actions bot commented Feb 10, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood AlexWaygood linked an issue Feb 10, 2025 that may be closed by this pull request
@dylwil3 dylwil3 added bug Something isn't working fixes Related to suggested fixes for violations labels Feb 10, 2025
@dylwil3 dylwil3 changed the title [pylint] Fix rule PLR17130 [pylint] Correct ordering of arguments in fix for if-stmt-min-max (PLR1730) Feb 10, 2025
else if cmp_left == assignment_value && cmp_right == target {
match op {
CmpOp::Lt => (MinMax::Min, true),
CmpOp::LtE => (MinMax::Min, true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is incorrect and should instead be false to match your table from the PR summary.

Please also carefully review the snapshot changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case where are in the situation:

if a <= b:
    b = a

which is fixed by b = min(a, b).

So you are right ;-)

Please also carefully review the snapshot changes.

I corrected the snapshot and I double-checked all the other changes I did. Everything follow the table posted in the description of the PR.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@MichaReiser MichaReiser merged commit ae1b381 into astral-sh:main Feb 12, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PLR1730 fix for <= and >= orders equal arguments wrong
3 participants