-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 autofixes max/mins in the wrong order #15887
Comments
Yes, agreed that this is buggy. Here's a minimal repro for how this can change the order of elements that compare equal but are not equivalent: >>> import itertools
>>> COUNT = itertools.count()
>>> from functools import total_ordering
>>> @total_ordering
... class Foo:
... def __init__(self, value):
... self.value = value
... self.count = next(COUNT)
... def __eq__(self, other):
... isinstance(other, Foo) and other.value == self.value
... def __lt__(self, other):
... if not isinstance(other, Foo):
... return NotImplemented
... self.value < other.value
...
>>> current = Foo(1)
>>> top = Foo(1)
>>> current.count
0
>>> top.count
1
>>> current = min(top, current)
>>> current.count
1 |
Can I give a try? :-) |
Go for it! Let us know if you need any help or clarification :-) |
Just a clarification. I was looking into the code and I saw this snippet /// R1730, R1731
pub(crate) fn if_stmt_min_max(checker: &mut Checker, stmt_if: &ast::StmtIf) Two small questions:
|
Yes, Ruff has a single rule that corresponds to two pylint rules. I'd expect that the same bug is present for |
Looking at the python documentation:
As a general rule, the variable that we want to update must always be in the first position in the Therefore, the autofix discussed at the beginning of this issue was indeed correct. But the problem mentioned is real and present. In fact, we have at line 452 of if_stmt_min_max.py.snap
which is wrong as it should be I will check if we can enforce the above mentioned rule in the suggestion and in the auto fix ;-) Or am I understanding something wrong? |
That all sounds correct to me. Thanks for the detailed write up! |
## Summary The PR addresses the issue #15887 For two objects `a` and `b`, we ensure that the auto-fix and the suggestion is of the form `a = min(a, b)` (or `a = max(a, b)`). This is because we want to be consistent with the python implementation of the methods: `min` and `max`. See the above issue for more details. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
Description
Ruff v0.9.4
Output:
Ruff autofixes this to
self.top = min(self.top, curr)
, but this is incorrect.The correct autofix is
self.top = min(curr, self.top)
, because if curr and self.top are equivalent, self.top should not be updated.This autofix works when equivalent values are interchangeable, but when we use custom overrides for the
__eq__
function, values may be equal but not interchangeable.The text was updated successfully, but these errors were encountered: