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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

"EOF in multi-line string" on string containing same quote nested in f-string #4350

Open
Feuermurmel opened this issue May 5, 2024 · 2 comments 路 May be fixed by #4365
Open

"EOF in multi-line string" on string containing same quote nested in f-string #4350

Feuermurmel opened this issue May 5, 2024 · 2 comments 路 May be fixed by #4365
Labels
T: bug Something isn't working

Comments

@Feuermurmel
Copy link

Feuermurmel commented May 5, 2024

Describe the bug

black cannot format this type of code: f"{'\''}"

$ black foo.py
error: cannot format foo.py: Cannot parse: 1:3: EOF in multi-line string

Oh no! 馃挜 馃挃 馃挜
1 file failed to reformat.

I was thinking that it might be the same bug as #4334, but the example in that issue (f'{'a'}') doesn't produce an error in black 24.4.2 anymore, and Python 3.12.2 accepts the above code:

import ast
from pathlib import Path

print(f"{'a'}")
print(ast.parse(Path(__file__).read_text()))
$ python3 example.py 
a
<ast.Module object at 0x1014a9410>

To Reproduce

Playground

Expected behavior

I have no opinion on how this should be formatted. Leaving this code as it is would be fine.

Environment

$ black --version
black, 24.4.2 (compiled: yes)
Python (CPython) 3.12.2

macOS 12.7.4 (21H1123)

@Feuermurmel Feuermurmel added the T: bug Something isn't working label May 5, 2024
@Feuermurmel Feuermurmel changed the title "Cannot parse" on string containing same quote nested in f-strings "EOF in multi-line string" on string containing same quote nested in f-strings May 5, 2024
@Feuermurmel Feuermurmel changed the title "EOF in multi-line string" on string containing same quote nested in f-strings "EOF in multi-line string" on string containing same quote nested in f-string May 5, 2024
@devshah9
Copy link

I guess it because backslash() aren't allowed in f-string brackets. You can use the double backslash if you want it on the string

@Feuermurmel
Copy link
Author

I guess it because backslash() aren't allowed in f-string brackets. You can use the double backslash if you want it on the string

They are allowed since Python 3.12: https://godbolt.org/z/9zGsezrEh

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
2 participants