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

RUF057 fix introduces an error when the argument contains a newline #15598

Open
dscorbett opened this issue Jan 20, 2025 · 1 comment · May be fixed by #15703
Open

RUF057 fix introduces an error when the argument contains a newline #15598

dscorbett opened this issue Jan 20, 2025 · 1 comment · May be fixed by #15703
Labels
bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features

Comments

@dscorbett
Copy link

The fix for unnecessary-round (RUF057) in Ruff 0.9.2 can introduce a syntax error or change behavior when the argument to round contains a newline.

Here is a crash:

$ cat ruf057_1.py
round(-
1)

$ ruff --isolated check --fix --preview --select RUF057 ruf057_1.py

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 `ruf057_1.py`, the rule codes RUF057, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

ruf057_1.py:1:1: RUF057 Value being rounded is already an integer
  |
1 | / round(-
2 | | 1)
  | |__^ RUF057
  |
  = help: Remove unnecessary `round` call

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

Here is a syntax error:

$ cat ruf057_2.py
round(1
* 1)

$ ruff --isolated check --fix --preview --select RUF057 ruf057_2.py
Found 1 error (1 fixed, 0 remaining).

$ cat ruf057_2.py
1
* 1

$ python ruf057_2.py
  File "ruf057_2.py", line 2
    * 1
    ^^^
SyntaxError: can't use starred expression here

Here is a runtime behavior change:

$ cat ruf057_3.py
x = round(1
+ 1)
print(x)

$ python ruf057_3.py
2

$ ruff --isolated check --fix --preview --select RUF057 ruf057_3.py
Found 1 error (1 fixed, 0 remaining).

$ cat ruf057_3.py
x = 1
+ 1
print(x)

$ python ruf057_3.py
1
@dhruvmanila
Copy link
Member

dhruvmanila commented Jan 20, 2025

Thanks for the examples!

We need to preserve the parentheses in these cases. The code in which ruff crashes also has the same change as your other examples:

error: Fix introduced a syntax error in `-` with rule codes RUF057: Expected an expression at byte range 1..2
---
-
1
---

cc @InSyncWithFoo in case you're interested

@dhruvmanila dhruvmanila added bug Something isn't working fixes Related to suggested fixes for violations labels Jan 20, 2025
@dylwil3 dylwil3 added the preview Related to preview mode features label Jan 24, 2025
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
3 participants