Skip to content

[flake8-simplify] SIM109 fix reorders statements incorrectly #18945

Closed

Description

@MeGaGiGaGon

Summary

I found this while working on #15584

compare-with-tuple (SIM109) does not respect the or ordering in expressions. x or y == z or y == w should be turned into x or (y in (z, w)), but instead becomes y in (z, w) or x, changing the meaning of the code.

https://play.ruff.rs/f7ed8e28-c451-48d8-ace1-c1fdb85f51cd

PS ~\Desktop\New_folder\ruff>Get-Content issue.py
x = None
y = "y"
z = "z"
w = "w"
print(x or y == z or y == w)
PS ~\Desktop\New_folder\ruff>py issue.py
False
PS ~\Desktop\New_folder\ruff>uvx ruff check issue.py --isolated --select SIM
issue.py:5:7: SIM109 Use `y in (z, w)` instead of multiple equality comparisons
  |
3 | z = "z"
4 | w = "w"
5 | print(x or y == z or y == w)
  |       ^^^^^^^^^^^^^^^^^^^^^ SIM109
  |
  = help: Replace with `y in (z, w)`

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).
PS ~\Desktop\New_folder\ruff>uvx ruff check issue.py --isolated --select SIM --fix --unsafe-fixes
Found 1 error (1 fixed, 0 remaining).
PS ~\Desktop\New_folder\ruff>Get-Content issue.py
x = None
y = "y"
z = "z"
w = "w"
print(y in (z, w) or x)
PS ~\Desktop\New_folder\ruff>py issue.py
None

Other misc things about SIM109 I noticed:
The lint currently doesn't apply at all if there are comments inside the expr, but I don't know why. I think it should instead be a normally safe fix, and only unsafe if there are comments. (There is the same amount of __eq__ calls pre and post fix, ordering in those calls is preserved, and the code already doesn't lint if there is any possibility of other side effects.)

Version

ruff 0.12.0 (87f0feb 2025-06-17) + playground

Activity

  1. changed the title [-][`flake8-simplify`] `SIM109` does not respect `or` ordering (false positive)[/-] [+][`flake8-simplify`] `SIM109` fix reorders statements incorrectly[/+] on Jun 25, 2025
  2. added
    bugAn issue describing something that isn't working, or a PR that fixes a bug
    fixesRelated to suggested fixes for violations
    on Jun 25, 2025
  3. ntBre commented on Jun 25, 2025

    @ntBre
    Contributor

    Thanks, this seems like a bug. Maybe we can use OperatorPrecedence here.

    With regard to the fix, I'm not sure if we want to upgrade it to a safe fix when there are no comments, but we could at least avoid the continue in the presence of comments since it's already unsafe.

  4. mikeleppane commented on Aug 5, 2025

    @mikeleppane
    Contributor

    I'm working on this.

  5. added a commit that references this issue on Sep 15, 2026
    42d9f30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue describing something that isn't working, or a PR that fixes a bugfixesRelated 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