psf/black

Black fails to format f strings in function kwargs with dictionaries

Closed this issue · 1 comments

Describe the bug

Black reports "cannot use --safe with this file; failed to parse source file AST: f-string: unmatched '[' (, line 1)"

To Reproduce

Take this code:

output = render(m,h=f"{dic["key"]}")

And run it with these arguments:

$ black file.py

The resulting error is:

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

Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

Correctly reformats, with --fast the output appears to be what's expected:

Environment

  • Black's version: black, 24.4.2 (compiled: yes)
  • OS and Python version: Windows 11, Python (CPython) 3.11.0

If you want to use PEP 701 f-strings with nested quotes, you need to use Python 3.12 or newer (on previous versions, that's a syntax error)