Hug closing } when f-string expression has a format specifier (#18704)

This commit is contained in:
Micha Reiser 2025-06-17 07:39:42 +02:00 committed by GitHub
parent 2b731d19b9
commit c22f809049
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 323 additions and 198 deletions

View file

@ -242,18 +242,22 @@ f"{ # comment 15
}" # comment 19
# comment 20
# Single-quoted f-strings with a format specificer can be multiline
f"aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
variable:.3f} ddddddddddddddd eeeeeeee"
# The specifier of an f-string must hug the closing `}` because a multiline format specifier is invalid syntax in a single
# quoted f-string.
f"aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable
:.3f} ddddddddddddddd eeeeeeee"
# But, if it's triple-quoted then we can't or the format specificer will have a
# trailing newline
f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
variable:.3f} ddddddddddddddd eeeeeeee"""
# The same applies for triple quoted f-strings, except that we need to preserve the newline before the closing `}`.
# or we risk altering the meaning of the f-string.
f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable
:.3f} ddddddddddddddd eeeeeeee"""
f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable:.3f
} ddddddddddddddd eeeeeeee"""
# But, we can break the ones which don't have a format specifier
f"""fooooooooooooooooooo barrrrrrrrrrrrrrrrrrr {
xxxxxxxxxxxxxxx:.3f} aaaaaaaaaaaaaaaaa { xxxxxxxxxxxxxxxxxxxx } bbbbbbbbbbbb"""
aaaaaaaaaaa = f"""asaaaaaaaaaaaaaaaa {
aaaaaaaaaaaa + bbbbbbbbbbbb + ccccccccccccccc + dddddddd
# comment
:.3f} cccccccccc"""
# Throw in a random comment in it but surprise, this is not a comment but just a text
# which is part of the format specifier
@ -285,6 +289,13 @@ x = f"{x !s
# comment 21
}"
x = f"{
x!s:>{
0
# comment 21-2
}}"
x = f"""
{ # comment 22
x = :.0{y # comment 23
@ -309,6 +320,21 @@ f"{ # comment 26
# comment 28
} woah {x}"
f"""{foo
:a{
a # comment 29
# comment 30
}
}"""
# Regression test for https://github.com/astral-sh/ruff/issues/18672
f"{
# comment 31
foo
:>
}"
# Assignment statement
# Even though this f-string has multiline expression, thus allowing us to break it at the

View file

@ -240,18 +240,20 @@ t"{ # comment 15
}" # comment 19
# comment 20
# Single-quoted t-strings with a format specificer can be multiline
# The specifier of a t-string must hug the closing `}` because a multiline format specifier is invalid syntax in a single
# quoted f-string.
t"aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
variable:.3f} ddddddddddddddd eeeeeeee"
variable
:.3f} ddddddddddddddd eeeeeeee"
# But, if it's triple-quoted then we can't or the format specificer will have a
# trailing newline
t"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
variable:.3f} ddddddddddddddd eeeeeeee"""
# The same applies for triple quoted t-strings, except that we need to preserve the newline before the closing `}`.
# or we risk altering the meaning of the f-string.
t"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable
:.3f} ddddddddddddddd eeeeeeee"""
t"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable
:.3f
} ddddddddddddddd eeeeeeee"""
# But, we can break the ones which don't have a format specifier
t"""fooooooooooooooooooo barrrrrrrrrrrrrrrrrrr {
xxxxxxxxxxxxxxx:.3f} aaaaaaaaaaaaaaaaa { xxxxxxxxxxxxxxxxxxxx } bbbbbbbbbbbb"""
# Throw in a random comment in it but surprise, this is not a comment but just a text
# which is part of the format specifier
@ -283,6 +285,12 @@ x = t"{x !s
# comment 21
}"
x = f"{
x!s:>{
0
# comment 21-2
}}"
x = t"""
{ # comment 22
x = :.0{y # comment 23