-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Black doesn't format expressions inside f-strings #567
Comments
This is an unfortunate side effect of how the AST encodes f-strings :( It would be awesome if black would understand expressions inside f-strings, but that's not the case today. |
This is not handled currently because:
In particular, we cannot use newlines inside the braces and we probably should minimize the amount of whitespace compared to regular code. |
The title makes this issue a little hard to find. Something like "black not formatting expressions inside f-strings" would make it easier. |
This is a design problem. Should we enforce the same style for expressions in f-strings as we do outside of them? In particular, spaces around operators come to mind. |
I came here to write the same kind of issue 😉 BTW, thanks so much for black, this is a great tool! To my mind, yes the formatting should be the same inside and outside an f-string. But at least, PEP 8 should be respected: "Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator" Especially when applied to: x = f"{3 + 5}" |
Two points:
I think the significant whitespace here is perhaps too clever, but it solved a design problem. |
We should really fix this at some point. Here are some thoughts in no particular order:
|
For what it's worth, I believe this would be close to trivial to implement on top of LibCST, which now parses f-strings inside the grammar, and so it provides a simple API to expose the parts of the f-string expression: https://libcst.readthedocs.io/en/latest/nodes.html#formatted-strings-f-strings |
We have experienced an issue with black when splitting f-strings over multiple lines.
Black now formats this into (ignoring spacing):
while it should be adding the "f-" to the second split line:
|
@robwijnhoven I don't believe that's how Black formatting has ever worked. If you can find a reproducible example, please report a separate bug. |
Was having basic python classes today, tried to use ternary if else in f string, |
That is only valid syntax on Python 3.12 or newer, support for that is tracked at #3746 |
Once #3822 is merged it should be much more tractable to fix this long-standing issue. |
We'll need another contributor to step up and propose a PR to do this. |
Operating system: macOS 10.13.6
Python version: 3.7.0
Black version: 18.9b0
Does also happen on master: yes
Given this example
I was expecting
black
to trim the whitespace inside the f-string braces, butblack
leaves the file as-is.Is this a bug or expected?
The text was updated successfully, but these errors were encountered: