Skip to content

Commit 738f998

Browse files
committed
Disallow newlines in format specifiers of sinlge quoted f- or t-strings
1 parent b68f0a9 commit 738f998

16 files changed

+438
-477
lines changed

crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_701.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@
7474
f'{(abc:=10)}'
7575

7676
f"This is a really long string, but just make sure that you reflow fstrings {
77-
2+2:d
78-
}"
77+
2+2:d}"
7978
f"This is a really long string, but just make sure that you reflow fstrings correctly {2+2:d}"
8079

8180
f"{2+2=}"

crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,7 @@
282282

283283
# Combine conversion flags with format specifiers
284284
x = f"{x = ! s
285-
:>0
286-
287-
}"
288-
# This is interesting. There can be a comment after the format specifier but only if it's
289-
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
290-
# We'll format is as trailing comments.
291-
x = f"{x !s
292-
:>0
293-
# comment 21
294-
}"
285+
:>0}"
295286

296287
x = f"{
297288
x!s:>{
@@ -315,14 +306,14 @@
315306
"""
316307

317308
# Mix of various features.
318-
f"{ # comment 26
309+
f"""{ # comment 26
319310
foo # after foo
320311
:>{
321312
x # after x
322313
}
323314
# comment 27
324315
# comment 28
325-
} woah {x}"
316+
} woah {x}"""
326317

327318

328319
f"""{foo
@@ -336,8 +327,7 @@
336327
f"{
337328
# comment 31
338329
foo
339-
:>
340-
}"
330+
:>}"
341331

342332
# Assignment statement
343333

@@ -491,13 +481,11 @@
491481

492482
# This is not a multiline f-string even though it has a newline after the format specifier.
493483
aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeee{
494-
a:.3f
495-
}moreeeeeeeeeeeeeeeeeetest" # comment
484+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
496485

497486
aaaaaaaaaaaaaaaaaa = (
498487
f"testeeeeeeeeeeeeeeeeeeeeeeeee{
499-
a:.3f
500-
}moreeeeeeeeeeeeeeeeeetest" # comment
488+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
501489
)
502490

503491
# The newline is only considered when it's a tripled-quoted f-string.

crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/tstring.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,7 @@
278278

279279
# Combine conversion flags with format specifiers
280280
x = t"{x = ! s
281-
:>0
282-
283-
}"
284-
# This is interesting. There can be a comment after the format specifier but only if it's
285-
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
286-
# We'll format is as trailing comments.
287-
x = t"{x !s
288-
:>0
289-
# comment 21
290-
}"
281+
:>0}"
291282

292283
x = f"{
293284
x!s:>{
@@ -310,14 +301,14 @@
310301
"""
311302

312303
# Mix of various features.
313-
t"{ # comment 26
304+
t"""{ # comment 26
314305
foo # after foo
315306
:>{
316307
x # after x
317308
}
318309
# comment 27
319310
# comment 28
320-
} woah {x}"
311+
} woah {x}"""
321312

322313
# Assignment statement
323314

@@ -471,13 +462,11 @@
471462

472463
# This is not a multiline t-string even though it has a newline after the format specifier.
473464
aaaaaaaaaaaaaaaaaa = t"testeeeeeeeeeeeeeeeeeeeeeeeee{
474-
a:.3f
475-
}moreeeeeeeeeeeeeeeeeetest" # comment
465+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
476466

477467
aaaaaaaaaaaaaaaaaa = (
478468
t"testeeeeeeeeeeeeeeeeeeeeeeeee{
479-
a:.3f
480-
}moreeeeeeeeeeeeeeeeeetest" # comment
469+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
481470
)
482471

483472
# The newline is only considered when it's a tripled-quoted t-string.

crates/ruff_python_formatter/src/other/interpolated_string_element.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ruff_python_ast::{
77
};
88
use ruff_text_size::{Ranged, TextSlice};
99

10-
use crate::comments::{dangling_open_parenthesis_comments, trailing_comments};
10+
use crate::comments::dangling_open_parenthesis_comments;
1111
use crate::context::{
1212
InterpolatedStringState, NodeLevel, WithInterpolatedStringState, WithNodeLevel,
1313
};
@@ -214,31 +214,6 @@ impl Format<PyFormatContext<'_>> for FormatInterpolatedElement<'_> {
214214
}
215215
}
216216

217-
// These trailing comments can only occur if the format specifier is
218-
// present. For example,
219-
//
220-
// ```python
221-
// f"{
222-
// x:.3f
223-
// # comment
224-
// }"
225-
// ```
226-
227-
// This can also be triggered outside of a format spec, at
228-
// least until https://github.com/astral-sh/ruff/issues/18632 is a syntax error
229-
// TODO(https://github.com/astral-sh/ruff/issues/18632) Remove this
230-
// and double check if it is still necessary for the triple quoted case
231-
// once this is a syntax error.
232-
// ```py
233-
// f"{
234-
// foo
235-
// :{x}
236-
// # comment 28
237-
// } woah {x}"
238-
// ```
239-
// Any other trailing comments are attached to the expression itself.
240-
trailing_comments(comments.trailing(self.element)).fmt(f)?;
241-
242217
if conversion.is_none() && format_spec.is_none() {
243218
bracket_spacing.fmt(f)?;
244219
} else if comments.has_trailing_own_line(self.element) {

crates/ruff_python_formatter/tests/fixtures.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,12 @@ fn format_file(source: &str, options: &PyFormatOptions, input_path: &Path) -> St
324324

325325
(Cow::Owned(without_markers), content)
326326
} else {
327-
let printed = format_module_source(source, options.clone()).expect("Formatting to succeed");
327+
let printed = format_module_source(source, options.clone()).unwrap_or_else(|err| {
328+
panic!(
329+
"Formatting of `{input_path} to succeed but it failed: {err}",
330+
input_path = input_path.display()
331+
)
332+
});
328333
let formatted_code = printed.into_code();
329334

330335
ensure_stability_when_formatting_twice(&formatted_code, options, input_path);

crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__pep_701.py.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ x = f"a{2+2:=^{foo(x+y**2):something else}one more}b"
8181
f'{(abc:=10)}'
8282
8383
f"This is a really long string, but just make sure that you reflow fstrings {
84-
2+2:d
85-
}"
84+
2+2:d}"
8685
f"This is a really long string, but just make sure that you reflow fstrings correctly {2+2:d}"
8786
8887
f"{2+2=}"

crates/ruff_python_formatter/tests/snapshots/format@expression__fstring.py.snap

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,7 @@ x = f"aaaaaaaaa { x = ! r }"
288288

289289
# Combine conversion flags with format specifiers
290290
x = f"{x = ! s
291-
:>0
292-
293-
}"
294-
# This is interesting. There can be a comment after the format specifier but only if it's
295-
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
296-
# We'll format is as trailing comments.
297-
x = f"{x !s
298-
:>0
299-
# comment 21
300-
}"
291+
:>0}"
301292
302293
x = f"{
303294
x!s:>{
@@ -321,14 +312,14 @@ x = f"""{"foo " + # comment 24
321312
"""
322313
323314
# Mix of various features.
324-
f"{ # comment 26
315+
f"""{ # comment 26
325316
foo # after foo
326317
:>{
327318
x # after x
328319
}
329320
# comment 27
330321
# comment 28
331-
} woah {x}"
322+
} woah {x}"""
332323
333324
334325
f"""{foo
@@ -342,8 +333,7 @@ f"""{foo
342333
f"{
343334
# comment 31
344335
foo
345-
:>
346-
}"
336+
:>}"
347337
348338
# Assignment statement
349339

@@ -497,13 +487,11 @@ aaaaa[aaaaaaaaaaa] = (
497487

498488
# This is not a multiline f-string even though it has a newline after the format specifier.
499489
aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeee{
500-
a:.3f
501-
}moreeeeeeeeeeeeeeeeeetest" # comment
490+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
502491

503492
aaaaaaaaaaaaaaaaaa = (
504493
f"testeeeeeeeeeeeeeeeeeeeeeeeee{
505-
a:.3f
506-
}moreeeeeeeeeeeeeeeeeetest" # comment
494+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
507495
)
508496

509497
# The newline is only considered when it's a tripled-quoted f-string.
@@ -1079,13 +1067,6 @@ x = f"aaaaaaaaa { x = !r}"
10791067

10801068
# Combine conversion flags with format specifiers
10811069
x = f"{x = !s:>0}"
1082-
# This is interesting. There can be a comment after the format specifier but only if it's
1083-
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
1084-
# We'll format is as trailing comments.
1085-
x = f"{
1086-
x!s:>0
1087-
# comment 21
1088-
}"
10891070

10901071
x = f"{
10911072
x!s:>{
@@ -1110,13 +1091,13 @@ x = f"""{
11101091
"""
11111092
11121093
# Mix of various features.
1113-
f"{ # comment 26
1094+
f"""{ # comment 26
11141095
foo:>{ # after foo
11151096
x # after x
11161097
}
11171098
# comment 27
11181099
# comment 28
1119-
} woah {x}"
1100+
} woah {x}"""
11201101
11211102
11221103
f"""{
@@ -1907,13 +1888,6 @@ x = f"aaaaaaaaa { x = !r}"
19071888

19081889
# Combine conversion flags with format specifiers
19091890
x = f"{x = !s:>0}"
1910-
# This is interesting. There can be a comment after the format specifier but only if it's
1911-
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
1912-
# We'll format is as trailing comments.
1913-
x = f"{
1914-
x!s:>0
1915-
# comment 21
1916-
}"
19171891

19181892
x = f"{
19191893
x!s:>{
@@ -1938,13 +1912,13 @@ x = f"""{
19381912
"""
19391913
19401914
# Mix of various features.
1941-
f"{ # comment 26
1915+
f"""{ # comment 26
19421916
foo:>{ # after foo
19431917
x # after x
19441918
}
19451919
# comment 27
19461920
# comment 28
1947-
} woah {x}"
1921+
} woah {x}"""
19481922
19491923
19501924
f"""{

crates/ruff_python_formatter/tests/snapshots/format@expression__tstring.py.snap

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,7 @@ x = t"aaaaaaaaa { x = ! r }"
284284

285285
# Combine conversion flags with format specifiers
286286
x = t"{x = ! s
287-
:>0
288-
289-
}"
290-
# This is interesting. There can be a comment after the format specifier but only if it's
291-
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
292-
# We'll format is as trailing comments.
293-
x = t"{x !s
294-
:>0
295-
# comment 21
296-
}"
287+
:>0}"
297288
298289
x = f"{
299290
x!s:>{
@@ -316,14 +307,14 @@ x = t"""{"foo " + # comment 24
316307
"""
317308
318309
# Mix of various features.
319-
t"{ # comment 26
310+
t"""{ # comment 26
320311
foo # after foo
321312
:>{
322313
x # after x
323314
}
324315
# comment 27
325316
# comment 28
326-
} woah {x}"
317+
} woah {x}"""
327318
328319
# Assignment statement
329320

@@ -477,13 +468,11 @@ aaaaa[aaaaaaaaaaa] = (
477468

478469
# This is not a multiline t-string even though it has a newline after the format specifier.
479470
aaaaaaaaaaaaaaaaaa = t"testeeeeeeeeeeeeeeeeeeeeeeeee{
480-
a:.3f
481-
}moreeeeeeeeeeeeeeeeeetest" # comment
471+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
482472

483473
aaaaaaaaaaaaaaaaaa = (
484474
t"testeeeeeeeeeeeeeeeeeeeeeeeee{
485-
a:.3f
486-
}moreeeeeeeeeeeeeeeeeetest" # comment
475+
a:.3f}moreeeeeeeeeeeeeeeeeetest" # comment
487476
)
488477

489478
# The newline is only considered when it's a tripled-quoted t-string.
@@ -1053,13 +1042,6 @@ x = t"aaaaaaaaa { x = !r}"
10531042

10541043
# Combine conversion flags with format specifiers
10551044
x = t"{x = !s:>0}"
1056-
# This is interesting. There can be a comment after the format specifier but only if it's
1057-
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
1058-
# We'll format is as trailing comments.
1059-
x = t"{
1060-
x!s:>0
1061-
# comment 21
1062-
}"
10631045

10641046
x = f"{
10651047
x!s:>{
@@ -1083,13 +1065,13 @@ x = t"""{
10831065
"""
10841066
10851067
# Mix of various features.
1086-
t"{ # comment 26
1068+
t"""{ # comment 26
10871069
foo:>{ # after foo
10881070
x # after x
10891071
}
10901072
# comment 27
10911073
# comment 28
1092-
} woah {x}"
1074+
} woah {x}"""
10931075
10941076
# Assignment statement
10951077

0 commit comments

Comments
 (0)