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

PLR1730 fix for <= and >= orders equal arguments wrong #16040

Closed
dscorbett opened this issue Feb 8, 2025 · 3 comments · Fixed by #16080
Closed

PLR1730 fix for <= and >= orders equal arguments wrong #16040

dscorbett opened this issue Feb 8, 2025 · 3 comments · Fixed by #16080
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome

Comments

@dscorbett
Copy link

Description

The fix for if-stmt-min-max (PLR1730) in Ruff 0.9.5 changes the program’s behavior when the operator is <= or >= and the values are equal. Of the following four examples, the first two were broken by #15930, and the last two were already incorrect.

$ cat >plr1730.py <<'# EOF'
t, c = [], []
if t >= c: t = c
print(t is c)

t, c = [], []
if t <= c: t = c
print(t is c)

t, c = [], []
if c <= t: t = c
print(t is c)

t, c = [], []
if c >= t: t = c
print(t is c)
# EOF

$ python plr1730.py
True
True
True
True

$ ruff --isolated check --preview --select PLR1730 plr1730.py --fix
Found 2 errors (2 fixed, 0 remaining).

$ cat plr1730.py
t, c = [], []
t = min(t, c)
print(t is c)

t, c = [], []
t = max(t, c)
print(t is c)

t, c = [], []
t = min(t, c)
print(t is c)

t, c = [], []
t = max(t, c)
print(t is c)

$ python plr1730.py
False
False
False
False
@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Feb 8, 2025
@MichaReiser MichaReiser added the help wanted Contributions especially welcome label Feb 8, 2025
@VascoSch92
Copy link
Contributor

Oh sorry... This is my fault 🙈

Can I re-fix it? Now I understand better the problem, and I would like to remediate.

@AlexWaygood
Copy link
Member

@VascoSch92 of course!

@MichaReiser
Copy link
Member

Oh sorry... This is my fault 🙈

Don't worry. This is really on me. I should have caught that during the review. Thanks for taking another try at it.

MichaReiser added a commit that referenced this issue Feb 12, 2025
… (`PLR1730`) (#16080)

The PR addresses the issue #16040 .

---

The logic used into the rule is the following:

Suppose to have an expression of the form 

```python
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 <.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
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 help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants