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

PLC2801 fix needs parentheses before subscript or attribute access #15745

Open
dscorbett opened this issue Jan 25, 2025 · 3 comments · May be fixed by #15762
Open

PLC2801 fix needs parentheses before subscript or attribute access #15745

dscorbett opened this issue Jan 25, 2025 · 3 comments · May be fixed by #15762
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features

Comments

@dscorbett
Copy link

Description

The fix for unnecessary-dunder-call (PLC2801) in Ruff 0.9.3 can change the program’s behavior when the rewritten expression is followed by an attribute access or a subscript access. The fix should wrap the rewritten expression in parentheses in those contexts.

$ cat >plc2801.py <<'#EOF'
print(1j.__add__(1.0).real)
print("x".__add__("y")[0])
#EOF

$ python plc2801.py
1.0
x

$ ruff --isolated check --preview --select PLC2801 plc2801.py --unsafe-fixes --fix
Found 2 errors (2 fixed, 0 remaining).

$ cat plc2801.py
print(1j + 1.0.real)
print("x" + "y"[0])

$ python plc2801.py
(1+1j)
xy
@dylwil3 dylwil3 added bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features labels Jan 25, 2025
@mishamsk
Copy link
Contributor

@dylwil3 @AlexWaygood Happy to fix this. I checked the code, there is already a check and logic for wrapping expression in parens, it's just ignoring attribute access, subscript and function call for some reason as lower precedence, which is not correct. Probably a matter of removing 3 lines + updating a test

@dylwil3
Copy link
Collaborator

dylwil3 commented Jan 26, 2025

Go for it!

@mishamsk
Copy link
Contributor

Go for it!

thanks. done #15762

not exactly 3 lines though 🙈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants