Skip to content

Commit b68f0a9

Browse files
committed
Don't drop comments for cases that are now syntax errors
1 parent dd12dd9 commit b68f0a9

File tree

7 files changed

+91
-30
lines changed

7 files changed

+91
-30
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@
285285
:>0
286286
287287
}"
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+
}"
288295

289296
x = f"{
290297
x!s:>{

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,12 @@
284284
# This is interesting. There can be a comment after the format specifier but only if it's
285285
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
286286
# We'll format is as trailing comments.
287-
x = t"{
287+
x = t"{x !s
288+
:>0
289+
# comment 21
290+
}"
291+
292+
x = f"{
288293
x!s:>{
289294
0
290295
# comment 21
@@ -305,14 +310,14 @@
305310
"""
306311

307312
# Mix of various features.
308-
t"""{ # comment 26
313+
t"{ # comment 26
309314
foo # after foo
310315
:>{
311316
x # after x
312317
}
313318
# comment 27
314319
# comment 28
315-
} woah {x}"""
320+
} woah {x}"
316321
317322
# Assignment statement
318323

crates/ruff_python_formatter/src/comments/placement.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,27 @@ fn handle_enclosed_comment<'a>(
323323
AnyNodeRef::TString(tstring) => CommentPlacement::dangling(tstring, comment),
324324
AnyNodeRef::InterpolatedElement(element) => {
325325
if let Some(preceding) = comment.preceding_node() {
326-
// Own line comment before format specifier
327-
// ```py
328-
// aaaaaaaaaaa = f"""asaaaaaaaaaaaaaaaa {
329-
// aaaaaaaaaaaa + bbbbbbbbbbbb + ccccccccccccccc + dddddddd
330-
// # comment
331-
// :.3f} cccccccccc"""
332-
// ```
333-
if comment.line_position().is_own_line()
334-
&& element.format_spec.is_some()
335-
&& comment.following_node().is_some()
336-
{
337-
return CommentPlacement::trailing(preceding, comment);
326+
if comment.line_position().is_own_line() && element.format_spec.is_some() {
327+
return if comment.following_node().is_some() {
328+
// Own line comment before format specifier
329+
// ```py
330+
// aaaaaaaaaaa = f"""asaaaaaaaaaaaaaaaa {
331+
// aaaaaaaaaaaa + bbbbbbbbbbbb + ccccccccccccccc + dddddddd
332+
// # comment
333+
// :.3f} cccccccccc"""
334+
// ```
335+
CommentPlacement::trailing(preceding, comment)
336+
} else {
337+
// TODO: This can be removed once format specifiers with a newline are a syntax error.
338+
// This is to handle cases like:
339+
// ```py
340+
// x = f"{x !s
341+
// :>0
342+
// # comment 21
343+
// }"
344+
// ```
345+
CommentPlacement::trailing(element, comment)
346+
};
338347
}
339348
}
340349

crates/ruff_python_formatter/src/other/interpolated_string_element.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ impl Format<PyFormatContext<'_>> for FormatInterpolatedElement<'_> {
241241

242242
if conversion.is_none() && format_spec.is_none() {
243243
bracket_spacing.fmt(f)?;
244+
} else if comments.has_trailing_own_line(self.element) {
244245
}
245246

246247
Ok(())
@@ -258,7 +259,15 @@ impl Format<PyFormatContext<'_>> for FormatInterpolatedElement<'_> {
258259
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
259260

260261
if self.context.is_multiline() {
261-
if format_spec.is_none() {
262+
// TODO: The `or comments.has_trailing...` can be removed once newlines in format specs are a syntax error.
263+
// This is to support the following case:
264+
// ```py
265+
// x = f"{x !s
266+
// :>0
267+
// # comment 21
268+
// }"
269+
// ```
270+
if format_spec.is_none() || comments.has_trailing_own_line(self.element) {
262271
group(&format_args![
263272
open_parenthesis_comments,
264273
soft_block_indent(&item)

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ x = f"{x = ! s
291291
:>0
292292

293293
}"
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+
}"
294301
295302
x = f"{
296303
x!s:>{
@@ -1072,6 +1079,13 @@ x = f"aaaaaaaaa { x = !r}"
10721079

10731080
# Combine conversion flags with format specifiers
10741081
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+
}"
10751089
10761090
x = f"{
10771091
x!s:>{
@@ -1099,9 +1113,10 @@ x = f"""{
10991113
f"{ # comment 26
11001114
foo:>{ # after foo
11011115
x # after x
1102-
# comment 27
1103-
# comment 28
1104-
}} woah {x}"
1116+
}
1117+
# comment 27
1118+
# comment 28
1119+
} woah {x}"
11051120
11061121
11071122
f"""{
@@ -1892,6 +1907,13 @@ x = f"aaaaaaaaa { x = !r}"
18921907

18931908
# Combine conversion flags with format specifiers
18941909
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+
}"
18951917
18961918
x = f"{
18971919
x!s:>{
@@ -1919,9 +1941,10 @@ x = f"""{
19191941
f"{ # comment 26
19201942
foo:>{ # after foo
19211943
x # after x
1922-
# comment 27
1923-
# comment 28
1924-
}} woah {x}"
1944+
}
1945+
# comment 27
1946+
# comment 28
1947+
} woah {x}"
19251948
19261949
19271950
f"""{

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,12 @@ x = t"{x = ! s
290290
# This is interesting. There can be a comment after the format specifier but only if it's
291291
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
292292
# We'll format is as trailing comments.
293-
x = t"{
293+
x = t"{x !s
294+
:>0
295+
# comment 21
296+
}"
297+
298+
x = f"{
294299
x!s:>{
295300
0
296301
# comment 21
@@ -311,14 +316,14 @@ x = t"""{"foo " + # comment 24
311316
"""
312317
313318
# Mix of various features.
314-
t"""{ # comment 26
319+
t"{ # comment 26
315320
foo # after foo
316321
:>{
317322
x # after x
318323
}
319324
# comment 27
320325
# comment 28
321-
} woah {x}"""
326+
} woah {x}"
322327
323328
# Assignment statement
324329

@@ -1052,6 +1057,11 @@ x = t"{x = !s:>0}"
10521057
# on it's own line. Refer to https://github.com/astral-sh/ruff/pull/7787 for more details.
10531058
# We'll format is as trailing comments.
10541059
x = t"{
1060+
x!s:>0
1061+
# comment 21
1062+
}"
1063+
1064+
x = f"{
10551065
x!s:>{
10561066
0
10571067
# comment 21
@@ -1073,13 +1083,13 @@ x = t"""{
10731083
"""
10741084
10751085
# Mix of various features.
1076-
t"""{ # comment 26
1086+
t"{ # comment 26
10771087
foo:>{ # after foo
10781088
x # after x
10791089
}
10801090
# comment 27
10811091
# comment 28
1082-
} woah {x}"""
1092+
} woah {x}"
10831093
10841094
# Assignment statement
10851095

crates/ty_python_semantic/src/python_platform.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::fmt::{Display, Formatter};
22

3-
use ruff_macros::RustDoc;
4-
53
/// The target platform to assume when resolving types.
64
#[derive(Debug, Clone, PartialEq, Eq)]
75
#[cfg_attr(
86
feature = "serde",
9-
derive(serde::Serialize, serde::Deserialize, RustDoc),
7+
derive(serde::Serialize, serde::Deserialize, ruff_macros::RustDoc),
108
serde(rename_all = "kebab-case")
119
)]
1210
pub enum PythonPlatform {

0 commit comments

Comments
 (0)