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

Failure to format when double-quotes are in an f-string's code block #4351

Closed
bmitc opened this issue May 7, 2024 · 8 comments
Closed

Failure to format when double-quotes are in an f-string's code block #4351

bmitc opened this issue May 7, 2024 · 8 comments
Labels
T: bug Something isn't working

Comments

@bmitc
Copy link

bmitc commented May 7, 2024

Describe the bug

See the code example.

To Reproduce

test = {"asdf" : 23} 

f"test['asdf']: {test["asdf"]}"

The resulting error is:

[cannot format file.py: INTERNAL ERROR: ...](error: cannot format <base_path>/format_test.py: Cannot parse: 3:23: f"test['asdf']: {test["asdf"]}")

It fails at the first " inside the f-string's code block when indexing the dictionary.

As a note, the online formatter actually gives a different error, which makes even less sense:

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

Expected behavior

This is valid syntax. For example, in a Python REPL:

>>> test = {"asdf" : 23} 
>>> type(test)
<class 'dict'>
>>> f"Testing['a']: {test["asdf"]}"
"Testing['a']: 23"

I expect no errors.

Environment

  • Black's version: 24.4.0
  • OS and Python version: Windows 11 and Python 3.12.1

Additional context

There is a workaround, and that is to replace the dictionary indexing double-quotes with single-quotes. Black is able to format this equivalent code:

test = {"asdf" : 23} 

f"test['asdf']: {test['asdf']}"

Also, regarding the online formatter, when I make this adjustment, it is also able to complete its formatting of this code. So it has an additional issue complaining about an unmatched [, which is obviously not the case if the switch from "asdf" to 'asdf' fixes that error.

@bmitc bmitc added the T: bug Something isn't working label May 7, 2024
@JelleZijlstra
Copy link
Collaborator

This is fixed in Black 24.4.1.

@bmitc
Copy link
Author

bmitc commented May 7, 2024

Thanks for the heads up! I verified this on my machine.

I was relying on the instructions:

  1. Use the online formatter at https://black.vercel.app/?version=main, which will use the latest main branch.

to get the latest version, and so I continued on with the bug report since it also has an issue. Does that mean that the latest version has a new, different issue?

As mentioned in the bug report, the online formatter returns:

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

@JelleZijlstra
Copy link
Collaborator

Good point that maybe we should stop recommending the online formatter.

The online formatter doesn't work here because this syntax is new in Python 3.12, and the formatter apparently runs an older version of Black at runtime. Black's own parser is independent of the Python version, but the AST safety check relies on Python's builtin parser.

@bmitc
Copy link
Author

bmitc commented May 7, 2024

Ahh, gotcha. Yea, I apologize for not doing due diligence and running poetry update black locally, but it was faster to click the link to the online formatter. :)

I am somewhat new to the Python ecosystem, and I immediately upgraded the existing codebase I inherited to Python 3.12, so I'm often unaware of the subtle changes that have happened across the versions.

@JelleZijlstra
Copy link
Collaborator

#4352

hauntsaninja added a commit to hauntsaninja/black-playground that referenced this issue May 7, 2024
@bmitc
Copy link
Author

bmitc commented May 7, 2024

Great! Thanks!

@shamikbosefj
Copy link

Does the VSCode plugin depend on the installed Python version? My VSCode plugin is still on black=24.3.0 and does not give me an option to update

@bmitc
Copy link
Author

bmitc commented May 28, 2024

It says in the extension page that:

The extension ships with black=24.4.2.

For my personal use, I use the VS Code setting (which is documented in the above link):

"black-formatter.importStrategy": "fromEnvironment",

That's because I use Poetry for all of my Python development, I configure Poetry to place the virtual environment (.venv directory) local to my Poetry project, and then I configure VS Code to use the virtual environment, which I think is done via a workspace setting. That way, I don't rely on an extension or system installation of Python, Black, or any other tool. This makes it much better when working across projects because each project defines their own Python and Black versions.

jpadilla pushed a commit to jpadilla/black-playground that referenced this issue Jul 27, 2024
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

3 participants