Skip to content

SIM300 fix swaps operands with side effects #14761

Closed
@dscorbett

Description

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions