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

Cannot format f-strings where the contents re-use the same quotes as the enclosing f-string #4334

Closed
user27182 opened this issue Apr 25, 2024 · 1 comment
Labels
T: bug Something isn't working

Comments

@user27182
Copy link

Describe the bug

Black fails to format f-strings where the contents re-use the same quotes as the enclosing f-string.

f'{'a'}'

Running this inside of https://black.vercel.app/?version=main

yields this error:

cannot use --safe with this file; failed to parse source file AST: f-string: unterminated string (, line 2)
This could be caused by running Black with an older Python version that does not support new syntax used in your source file.

This is very similar to #4329 , but there the MRE seems to involve nested f-strings.

Additional context

Something like f'{'a'}' is valid code in python 3.12. See:

https://docs.python.org/3/whatsnew/3.12.html#pep-701-syntactic-formalization-of-f-strings

@user27182 user27182 added the T: bug Something isn't working label Apr 25, 2024
@JelleZijlstra
Copy link
Collaborator

Black's AST safety check relies on Python's builtin AST library. If that is running on a version of Python before 3.12, this file won't parse. It will work fine if you run Black on Python 3.12 or higher.

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

No branches or pull requests

2 participants