black icon indicating copy to clipboard operation
black copied to clipboard

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

Open Feuermurmel opened this issue 1 year ago • 2 comments

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 https://github.com/psf/black/issues/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 avatar May 05 '24 16:05 Feuermurmel

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

devshah9 avatar May 15 '24 05:05 devshah9

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

Feuermurmel avatar May 15 '24 10:05 Feuermurmel