Closed
Description
opened on Dec 3, 2024
The fix for yoda-conditions
(SIM300) changes the program’s behavior in Ruff 0.8.1 when the two operands have side effects. The fix should be suppressed or marked unsafe when that happens.
This can happen when the first operand is a non-empty dictionary display, which is always marked as probably constant regardless of its contents.
$ cat sim300_1.py
{"": print(1)} == print(2)
$ python sim300_1.py
1
2
$ ruff check --isolated --select SIM300 sim300_1.py --fix
Found 1 error (1 fixed, 0 remaining).
$ cat sim300_1.py
print(2) == {"": print(1)}
$ python sim300_1.py
2
1
It can also happen when the first operand is an all-caps attribute.
$ cat sim300_2.py
class C:
def __getattr__(self, name):
print(name)
return name
C().A == print('B')
$ python sim300_2.py
A
B
$ ruff check --isolated --select SIM300 sim300_2.py --fix
Found 1 error (1 fixed, 0 remaining).
$ cat sim300_2.py
class C:
def __getattr__(self, name):
print(name)
return name
print('B') == C().A
$ python sim300_2.py
B
A
There might be other cases.
Activity