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

Syntax error when RUF010 autofixes f"{str({})}" #5530

Closed
harupy opened this issue Jul 5, 2023 · 6 comments · Fixed by #5538
Closed

Syntax error when RUF010 autofixes f"{str({})}" #5530

harupy opened this issue Jul 5, 2023 · 6 comments · Fixed by #5538
Assignees
Labels
bug Something isn't working

Comments

@harupy
Copy link
Contributor

harupy commented Jul 5, 2023

Reproduction

> ruff --version
ruff 0.0.275

> echo 'f"{str({})}"' | ruff --select RUF010 --stdin-filename a.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/astral-sh/ruff/issues/new?title=%5BAutofix%20error%5D

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

f"{str({})}"
@harupy harupy changed the title Syntax error when RUF010 autofixes 'f"{str({})}"' Syntax error when RUF010 autofixes f"{str({})}" Jul 5, 2023
@charliermarsh
Copy link
Member

Oh boy. Did this trigger on a real project? Just curious how common it is.

@charliermarsh charliermarsh added the bug Something isn't working label Jul 5, 2023
@harupy
Copy link
Contributor Author

harupy commented Jul 5, 2023

Did this trigger on a real project?

Yes, on https://github.com/mlflow/mlflow.

Just curious how common it is.

I think it's uncommon :)

@charliermarsh
Copy link
Member

Haha we should still fix it definitely, was just looking for examples :)

@harupy
Copy link
Contributor Author

harupy commented Jul 5, 2023

The original code looked like this:

"foo bar baz {}".format(str({}))

I first applied UP032:

f"foo bar baz {str({})}"

Then, applied RUF010 to remove str() and hit the error.

@harupy
Copy link
Contributor Author

harupy commented Jul 5, 2023

f"{{}!s}" is invalid, but f"{{}}" is.

>>> f"{{}}"
'{}'
>>> f"{{}!s}"
  File "<stdin>", line 1
SyntaxError: f-string: single '}' is not allowed

@charliermarsh
Copy link
Member

Hmm yeah but they have different meanings, in the first case it's being interpreted as the literal braces whereas in the second it's an expression.

E.g. consider if the original example was: "foo bar baz {}".format(str({"a": 1})). Then f"foo bar baz {{"a": 1}}" would be invalid syntax.

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

Successfully merging a pull request may close this issue.

2 participants