Disallow newlines in format specifiers of single quoted f- or t-strings (#18708)

This commit is contained in:
Micha Reiser 2025-06-18 14:56:15 +02:00 committed by GitHub
parent 23261a38a0
commit 1188ffccc4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 521 additions and 513 deletions

View file

@ -324,7 +324,12 @@ fn format_file(source: &str, options: &PyFormatOptions, input_path: &Path) -> St
(Cow::Owned(without_markers), content)
} else {
let printed = format_module_source(source, options.clone()).expect("Formatting to succeed");
let printed = format_module_source(source, options.clone()).unwrap_or_else(|err| {
panic!(
"Formatting `{input_path} was expected to succeed but it failed: {err}",
input_path = input_path.display()
)
});
let formatted_code = printed.into_code();
ensure_stability_when_formatting_twice(&formatted_code, options, input_path);

View file

@ -81,8 +81,7 @@ x = f"a{2+2:=^{foo(x+y**2):something else}one more}b"
f'{(abc:=10)}'
f"This is a really long string, but just make sure that you reflow fstrings {
2+2:d
}"
2+2:d}"
f"This is a really long string, but just make sure that you reflow fstrings correctly {2+2:d}"
f"{2+2=}"

View file

@ -284,16 +284,7 @@ x = f"aaaaaaaaa { x = !r }"
# Combine conversion flags with format specifiers
x = f"{x = !s
:>0
}"
# This is interesting. There can be a comment after the format specifier but only if it's
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
# We'll format is as trailing comments.
x = f"{x !s
:>0
# comment 21
}"
:>0}"
x = f"{
x!s:>{
@ -301,6 +292,13 @@ x = f"{
# comment 21-2
}}"
f"{1
# comment 21-3
:}"
f"{1 # comment 21-4
:} a"
x = f"""
{ # comment 22
@ -317,14 +315,14 @@ x = f"""{"foo " + # comment 24
"""
# Mix of various features.
f"{ # comment 26
f"""{ # comment 26
foo # after foo
:>{
x # after x
}
# comment 27
# comment 28
} woah {x}"
} woah {x}"""
f"""{foo
@ -338,8 +336,7 @@ f"""{foo
f"{
# comment 31
foo
:>
}"
:>}"
# Assignment statement
@ -493,13 +490,11 @@ aaaaa[aaaaaaaaaaa] = (
# This is not a multiline f-string even though it has a newline after the format specifier.
aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeee{
a:.3f
}moreeeeeeeeeeeeeeeeeetest" # comment
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
aaaaaaaaaaaaaaaaaa = (
f"testeeeeeeeeeeeeeeeeeeeeeeeee{
a:.3f
}moreeeeeeeeeeeeeeeeeetest" # comment
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
)
# The newline is only considered when it's a tripled-quoted f-string.
@ -1071,13 +1066,6 @@ x = f"aaaaaaaaa { x = !r}"
# Combine conversion flags with format specifiers
x = f"{x = !s:>0}"
# This is interesting. There can be a comment after the format specifier but only if it's
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
# We'll format is as trailing comments.
x = f"{
x!s:>0
# comment 21
}"
x = f"{
x!s:>{
@ -1085,6 +1073,15 @@ x = f"{
# comment 21-2
}}"
f"{
1
# comment 21-3
:}"
f"{
1 # comment 21-4
:} a"
x = f"""
{ # comment 22
@ -1102,13 +1099,14 @@ x = f"""{
"""
# Mix of various features.
f"{ # comment 26
foo:>{ # after foo
f"""{ # comment 26
foo # after foo
:>{
x # after x
}
# comment 27
# comment 28
} woah {x}"
} woah {x}"""
f"""{
@ -1895,13 +1893,6 @@ x = f"aaaaaaaaa { x = !r}"
# Combine conversion flags with format specifiers
x = f"{x = !s:>0}"
# This is interesting. There can be a comment after the format specifier but only if it's
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
# We'll format is as trailing comments.
x = f"{
x!s:>0
# comment 21
}"
x = f"{
x!s:>{
@ -1909,6 +1900,15 @@ x = f"{
# comment 21-2
}}"
f"{
1
# comment 21-3
:}"
f"{
1 # comment 21-4
:} a"
x = f"""
{ # comment 22
@ -1926,13 +1926,14 @@ x = f"""{
"""
# Mix of various features.
f"{ # comment 26
foo:>{ # after foo
f"""{ # comment 26
foo # after foo
:>{
x # after x
}
# comment 27
# comment 28
} woah {x}"
} woah {x}"""
f"""{

View file

@ -280,16 +280,7 @@ x = t"aaaaaaaaa { x = !r }"
# Combine conversion flags with format specifiers
x = t"{x = !s
:>0
}"
# This is interesting. There can be a comment after the format specifier but only if it's
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
# We'll format is as trailing comments.
x = t"{x !s
:>0
# comment 21
}"
:>0}"
x = f"{
x!s:>{
@ -297,6 +288,13 @@ x = f"{
# comment 21-2
}}"
f"{1
# comment 21-3
:}"
f"{1 # comment 21-4
:} a"
x = t"""
{ # comment 22
x = :.0{y # comment 23
@ -312,14 +310,14 @@ x = t"""{"foo " + # comment 24
"""
# Mix of various features.
t"{ # comment 26
t"""{ # comment 26
foo # after foo
:>{
x # after x
}
# comment 27
# comment 28
} woah {x}"
} woah {x}"""
# Assignment statement
@ -473,13 +471,11 @@ aaaaa[aaaaaaaaaaa] = (
# This is not a multiline t-string even though it has a newline after the format specifier.
aaaaaaaaaaaaaaaaaa = t"testeeeeeeeeeeeeeeeeeeeeeeeee{
a:.3f
}moreeeeeeeeeeeeeeeeeetest" # comment
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
aaaaaaaaaaaaaaaaaa = (
t"testeeeeeeeeeeeeeeeeeeeeeeeee{
a:.3f
}moreeeeeeeeeeeeeeeeeetest" # comment
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
)
# The newline is only considered when it's a tripled-quoted t-string.
@ -1045,13 +1041,6 @@ x = t"aaaaaaaaa { x = !r}"
# Combine conversion flags with format specifiers
x = t"{x = !s:>0}"
# This is interesting. There can be a comment after the format specifier but only if it's
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
# We'll format is as trailing comments.
x = t"{
x!s:>0
# comment 21
}"
x = f"{
x!s:>{
@ -1059,6 +1048,15 @@ x = f"{
# comment 21-2
}}"
f"{
1
# comment 21-3
:}"
f"{
1 # comment 21-4
:} a"
x = t"""
{ # comment 22
x = :.0{y # comment 23
@ -1075,13 +1073,14 @@ x = t"""{
"""
# Mix of various features.
t"{ # comment 26
foo:>{ # after foo
t"""{ # comment 26
foo # after foo
:>{
x # after x
}
# comment 27
# comment 28
} woah {x}"
} woah {x}"""
# Assignment statement