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

ruff format output breaks certain f-strings in 0.9 #15514

Closed
markjm opened this issue Jan 15, 2025 · 3 comments · Fixed by #15524
Closed

ruff format output breaks certain f-strings in 0.9 #15514

markjm opened this issue Jan 15, 2025 · 3 comments · Fixed by #15524
Assignees
Labels
bug Something isn't working formatter Related to the formatter

Comments

@markjm
Copy link

markjm commented Jan 15, 2025

Consider the file:

params = {}
string = "this is my string with " f'"{params.get("mine")}"'

ruff format in 0.9.1 replaces this to

ruff format test.py --diff
--- test.py
+++ test.py
@@ -1,2 +1,2 @@
 params = {}
-string = "this is my string with " f'"{params.get("mine")}"'
\ No newline at end of file
+string = f"this is my string with \"{params.get("mine")}\""

1 file would be reformatted

This is not valid python due to nested f-string using the same quote character (at least, its not valid before py3.12

python -m compileall test.py
Compiling 'test.py'...
***   File "test.py", line 2
    string = f"this is my string with \"{params.get("mine")}\""
                                                     ^
SyntaxError: invalid syntax

It is a very uncommon case and simple enough to just fix up myself, but figured id bug report to make yall aware.

Thanks for the great project!

@MichaReiser MichaReiser added bug Something isn't working formatter Related to the formatter labels Jan 15, 2025
@MichaReiser
Copy link
Member

I'm able to reproduce this but only when using quote-style = "preserve". Using either single or double works fine (playground)

@MichaReiser MichaReiser self-assigned this Jan 15, 2025
@markjm
Copy link
Author

markjm commented Jan 15, 2025

Sorry, forgot to include my config!

@MichaReiser
Copy link
Member

No worries. The issue contained enough details that I could figure it out myself. Thanks for reporting

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

Successfully merging a pull request may close this issue.

2 participants