You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multiline expression brackets with format specifiers don't work in f-strings #110259
Update: i tried to check how annoying would be to support the other option (support multi-line format specifiers) and turns out that is quite easy to support thanks to how we have structured this (yay!). So only if we want we can support that.
It would look like this, for clarification:
code = """\
f"__{
1
+
1:a
b
c
}__"
"""
print(ast.dump(ast.parse(code), indent=4))
Notice that this has an important difference that I think it makes is less appealing: extra trailing newlines are included in the format specifier, which I think it will make formatters unhappy (@tusharsadhwani, please confirm)
@tusharsadhwani black can't reformat the format specs without breaking datetime.datetime (for example).
@pablogsal I don't think it matters all that much. I can't imagine that there are many single quote multi-line format specs, although they no doubt exist.
Activity
pablogsal commentedon Oct 3, 2023
Thanks for the report! I have opened #110271 to fix it
pythongh-110259: Fix f-strings with multiline expressions and format …
pythongh-110259: Fix f-strings with multiline expressions and format …
pythongh-110259: Fix f-strings with multiline expressions and format …
ericvsmith commentedon Oct 3, 2023
I'd be careful on that last one. If
xis adatetime.datetime, it's a valid format spec.ericvsmith commentedon Oct 3, 2023
In case that's not clear: format specs for
datetime.datetimecan contain any characters, and are passed on tostrftime. Perhaps more usefully:pablogsal commentedon Oct 3, 2023
Hummmm, then what you are saying is that we should not include the newline in the single quoted case but we should do it in the triple quoted case?
pablogsal commentedon Oct 3, 2023
Ah, indeed, this is the case because the triple quote case works like that in 3.11:
lysnikolaou commentedon Oct 5, 2023
I don't understand why this is the case. Shouldn't these both lead to the same AST?
Multi-line f-string:
Single-quoted f-string:
ericvsmith commentedon Oct 5, 2023
I don't think it's necessarily an issue. I was just pointing out that the error shown in the original report
(a ValueError from the bad format spec "d\n" for int) is different from the error in
(a SyntaxError). In any event, the format spec should be allowed to have newlines, including trailing newlines.
12 remaining items
pablogsal commentedon Oct 5, 2023
Update: i tried to check how annoying would be to support the other option (support multi-line format specifiers) and turns out that is quite easy to support thanks to how we have structured this (yay!). So only if we want we can support that.
It would look like this, for clarification:
which prints:
Sorry for the back and forth @lysnikolaou @ericvsmith. I think we are free to choose whatever we prefer.
pablogsal commentedon Oct 5, 2023
Notice that this has an important difference that I think it makes is less appealing: extra trailing newlines are included in the format specifier, which I think it will make formatters unhappy (@tusharsadhwani, please confirm)
tusharsadhwani commentedon Oct 5, 2023
Can confirm, second option will restrict code formatting, pretty much making fstrings with format specs unformattable.
But, fstrings don't get formatted by black right now as-is and there's no confirmation on my end that that's desired.
gh-110259: Fix f-strings with multiline expressions and format specs (#…
pythongh-110259: Fix f-strings with multiline expressions and format …
ericvsmith commentedon Oct 5, 2023
@tusharsadhwani black can't reformat the format specs without breaking datetime.datetime (for example).
@pablogsal I don't think it matters all that much. I can't imagine that there are many single quote multi-line format specs, although they no doubt exist.
pablogsal commentedon Oct 5, 2023
Ok, I ended implementing the single-line restruction for format specs in single-quoted f-strings. Thanks everyone for your help, you rock 🚀
[3.12] gh-110259: Fix f-strings with multiline expressions and format…
tusharsadhwani commentedon Oct 5, 2023
@ericvsmith wasn't single quote multiline format spec invalid syntax until now?
ericvsmith commentedon Oct 5, 2023
@tusharsadhwani : I thought somewhere on this thread someone said it worked in 3.11, but I guess I misread that.
Fix lexing single-quoted f-string with multi-line format spec (#7787)
Fix lexing single-quoted f-string with multi-line format spec (#7787)
pythongh-110259: Fix f-strings with multiline expressions and format …
Fix lexing single-quoted f-string with multi-line format spec (#7787)