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

Broken f-string formatting after ruff 0.9 #15459

Closed
AlexElvers opened this issue Jan 13, 2025 · 1 comment · Fixed by #15471
Closed

Broken f-string formatting after ruff 0.9 #15459

AlexElvers opened this issue Jan 13, 2025 · 1 comment · Fixed by #15471
Assignees
Labels
bug Something isn't working formatter Related to the formatter

Comments

@AlexElvers
Copy link

Ruff 0.9.0/1 are introducing syntax syntax errors in f-strings with braces.

While f-strings with simple sets and dicts do not remove the necessary spaces:

$ echo 'print(f"{ {1, 2, 3} }")\nprint(f"{ {1: 2} }")' > main.py

$ ruff format --diff main.py
1 file already formatted

the spaces around the braces are removed in version 0.9.0/1, which leads to syntax errors:

$ echo 'print(f"{ {1, 2, 3} - {2} }")\nprint(f"{ {1: 2}.keys() }")' > main.py

$ ruff format --diff main.py
--- main.py
+++ main.py
@@ -1,2 +1,2 @@
-print(f"{ {1, 2, 3} - {2} }")
-print(f"{ {1: 2}.keys() }")
+print(f"{{1, 2, 3} - {2}}")
+print(f"{{1: 2}.keys()}")

1 file would be reformatted

$ ruff format main.py
1 file reformatted

$ ruff check main.py
main.py:1:18: SyntaxError: f-string: single '}' is not allowed
  |
1 | print(f"{{1, 2, 3} - {2}}")
  |                  ^
2 | print(f"{{1: 2}.keys()}")
  |

main.py:1:25: SyntaxError: f-string: single '}' is not allowed
  |
1 | print(f"{{1, 2, 3} - {2}}")
  |                         ^
2 | print(f"{{1: 2}.keys()}")
  |

main.py:2:15: SyntaxError: f-string: single '}' is not allowed
  |
1 | print(f"{{1, 2, 3} - {2}}")
2 | print(f"{{1: 2}.keys()}")
  |               ^
  |

main.py:2:23: SyntaxError: f-string: single '}' is not allowed
  |
1 | print(f"{{1, 2, 3} - {2}}")
2 | print(f"{{1: 2}.keys()}")
  |                       ^
  |

Found 4 errors.

$ python main.py
  File "/tmp/foo/main.py", line 1
    print(f"{{1, 2, 3} - {2}}")
                              ^
SyntaxError: f-string: single '}' is not allowed

Ruff 0.8.6 kept these f-strings unchanged:

$ uvx [email protected] format --diff main.py
1 file already formatted
@AlexWaygood AlexWaygood added the formatter Related to the formatter label Jan 13, 2025
@MichaReiser MichaReiser added the bug Something isn't working label Jan 13, 2025
@MichaReiser
Copy link
Member

MichaReiser commented Jan 13, 2025

Thanks for reporting this. We do have code handling dictionary and other expressions that use curly braces but it only tests the expression itself rather than the left most or right most sub-expression.

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.

3 participants