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

C413 fix is wrong when a newline appears between sorted and the opening parenthesis #15789

Closed
dscorbett opened this issue Jan 28, 2025 · 2 comments · Fixed by #15825
Closed
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@dscorbett
Copy link

Description

The fix for unnecessary-call-around-sorted (C413) in Ruff 0.9.3 can introduce a syntax error or change behavior when a newline appears between sorted and the opening parenthesis.

Syntax error:

$ cat >c413_1.py <<'# EOF'
reversed(sorted
(""))
# EOF

$ ruff --isolated check  --select C413 c413_1.py --unsafe-fixes --fix

error: Fix introduced a syntax error. Reverting all changes.

This indicates a bug in Ruff. If you could open an issue at:

    https://github.com/astral-sh/ruff/issues/new?title=%5BFix%20error%5D

...quoting the contents of `c413_1.py`, the rule codes C413, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

c413_1.py:1:1: C413 Unnecessary `reversed()` call around `sorted()`
  |
1 | / reversed(sorted
2 | | (""))
  | |_____^ C413
  |
  = help: Remove unnecessary `reversed()` call

Found 1 error.
[*] 1 fixable with the --fix option.

Changed behavior:

$ cat >c413_2.py <<'# EOF'
x = list(sorted
("xy"))
print(x)
# EOF

$ python c413_2.py
['x', 'y']

$ ruff --isolated check  --select C413 c413_2.py --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ cat c413_2.py
x = sorted
("xy")
print(x)

$ python c413_2.py
<built-in function sorted>
@dscorbett dscorbett changed the title C413 fix is wrong when a newline appears between sorted opening parenthesis C413 fix is wrong when a newline appears between sorted and the opening parenthesis Jan 28, 2025
@dylwil3 dylwil3 added bug Something isn't working fixes Related to suggested fixes for violations labels Jan 28, 2025
@dylwil3 dylwil3 self-assigned this Jan 29, 2025
@dylwil3
Copy link
Collaborator

dylwil3 commented Jan 29, 2025

Thanks for this! This seems to keep cropping up... maybe worth adding a general edit for removing a function call so we don't have to rediscover this logic each time.

@dylwil3
Copy link
Collaborator

dylwil3 commented Jan 30, 2025

This seems to keep cropping up... maybe worth adding a general edit for removing a function call so we don't have to rediscover this logic each time.

Didn't end up doing this since the fix for this rule used codegen while the fix for the other rule I was thinking of used a text-based edit, so the logic was different.

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
Projects
None yet
2 participants