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

Fix for RUF023 should be unsafe because slot order can matter #12653

Closed
dscorbett opened this issue Aug 3, 2024 · 0 comments · Fixed by #12692
Closed

Fix for RUF023 should be unsafe because slot order can matter #12653

dscorbett opened this issue Aug 3, 2024 · 0 comments · Fixed by #12692
Assignees
Labels
fixes Related to suggested fixes for violations

Comments

@dscorbett
Copy link

The fix for RUF023 should be marked unsafe because the order of slots can be intentionally relied on, and changing it can change behavior. The fix is still safe if __slots__ is declared as a set.

$ ruff --version
ruff 0.5.6
$ cat ruf023.py 
class Fraction:
    __slots__ = "numerator", "denominator"

    __match_args__ = __slots__

    def __init__(self, numerator, denominator):
        self.numerator = numerator
        self.denominator = denominator


match Fraction(1, 2):
    case Fraction(1, 2):
        print("matched")
    case _:
        print("not matched")
$ python ruf023.py
matched
$ ruff check --isolated --preview --select RUF023 ruf023.py --fix
Found 1 error (1 fixed, 0 remaining).
$ python ruf023.py
not matched
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
3 participants