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

[Autofix error] RUF005 autofix fails inside f-string: f"{a() + ['b']}" #4324

Closed
konstin opened this issue May 9, 2023 · 2 comments · Fixed by #4487
Closed

[Autofix error] RUF005 autofix fails inside f-string: f"{a() + ['b']}" #4324

konstin opened this issue May 9, 2023 · 2 comments · Fixed by #4487
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@konstin
Copy link
Member

konstin commented May 9, 2023

In ruff 99a755f, the following snippet causes the autofix RUF005 to introduce a syntax error:

f"{a() + ['b']}"
$ ./ruff --no-cache --select RUF005 --fix code.py

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

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

    https://github.com/charliermarsh/ruff/issues/new?title=%5BAutofix%20error%5D

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

code.py:1:4: RUF005 Consider `[*a(), "b"]` instead of concatenation
Found 1 error.

This was originally discovered in https://github.com/Mojino01/hugging_face/blob/3dae0d7b4fb8d7e9383b893e4e1799191bb2ab7b/src/transformers/pipelines/base.py#L1181

@konstin konstin added bug Something isn't working fixes Related to suggested fixes for violations autofix-error labels May 9, 2023
@charliermarsh
Copy link
Member

I think we have a few options for addressing this class of issues:

  1. Always skip autofix within f-strings (easiest).
  2. Skip autofix within f-strings on a rule-by-rule basis. This is fine, but we're going to have to omit most rules. Even RUF005 only fails because we go through unparse_expr, which changes the quote style of the 'b' to "b".
  3. Improve unparse_expr to enforce specific quote styles within f-strings. This would require tracking the quote style of the f-string, threading it down to unparse_expr, and respecting it throughout the unparse logic.

I'm honestly tempted to do (1).

@konstin
Copy link
Member Author

konstin commented May 9, 2023

One note to add here is that most of these will valid syntax with PEP 701 implemented, currently targeted for python 3.12, i.e. we could very ambitiously allow autofixes in f-strings for projects with minimum python version 3.12.

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
Development

Successfully merging a pull request may close this issue.

2 participants