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] C408 - unnecessary-collection-call - syntax error when fixing within f-string #4249

Closed
Hnasar opened this issue May 5, 2023 · 5 comments · Fixed by #4563
Closed
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@Hnasar
Copy link
Contributor

Hnasar commented May 5, 2023

Presumably replacing dict() with {} gets confused when inside an f-string when it comes to using " vs ' (or maybe it's the nested {)

# test.py
f"{dict(x='y')}"  # should turn into f"{{'x': 'y'}}"
$ ruff --select=C408 test.py  --fix

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

test.py:1:4: C408 Unnecessary `dict` call (rewrite as a literal)

Using ruff 0.0.264

@charliermarsh charliermarsh added the bug Something isn't working label May 5, 2023
@charliermarsh
Copy link
Member

Good call, thanks!

@charliermarsh charliermarsh added the good first issue Good for newcomers label May 5, 2023
@charliermarsh
Copy link
Member

(We track in_f_string, so we should just avoid triggering this rule, and perhaps some others in this category, when in_f_string is true.)

@dhruvmanila
Copy link
Member

I think it's the braces and not the quotes as the quotes are taken as is. So, this should be avoided in dict and set comprehensions.

@akx
Copy link
Contributor

akx commented May 9, 2023

Maybe the heuristics in the new ruff::rules::flynt::helpers::to_fstring_elem function could be of more generic use here :)

@DavideCanton
Copy link
Contributor

I can work on it if nobody else wants to

DavideCanton added a commit to DavideCanton/ruff that referenced this issue May 21, 2023
This fixes some C40* rules by adding spaces around dict and
set literals inside f-string, and quotes correctly string literals
inside f-string, since dict kwargs must be converted into
string literals.
DavideCanton added a commit to DavideCanton/ruff that referenced this issue May 21, 2023
DavideCanton added a commit to DavideCanton/ruff that referenced this issue May 22, 2023
DavideCanton added a commit to DavideCanton/ruff that referenced this issue May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants