Skip to content

Commit bde498b

Browse files
committed
Only apply new logic in places where we use FormatLastStatementExpression
1 parent 3d612d1 commit bde498b

File tree

6 files changed

+155
-20
lines changed

6 files changed

+155
-20
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,36 @@
300300
"b" # belongs to `b`
301301
)
302302

303+
a: Literal[str] = (
304+
"a"
305+
"b" # belongs to `b`
306+
)
307+
308+
a += (
309+
"a"
310+
"b" # belongs to `b`
311+
)
312+
313+
a = (
314+
r"a"
315+
r"b" # belongs to `b`
316+
)
317+
303318
a = (
304319
"a"
305320
"b"
306321
) # belongs to the assignment
307322

323+
a = (((
324+
"a"
325+
"b" # belongs to `b`
326+
)))
327+
328+
a = (((
329+
"a"
330+
"b"
331+
) # belongs to the f-string expression
332+
))
308333

309334
a = (
310335
"a" "b" # belongs to the f-string expression
@@ -334,3 +359,15 @@
334359
"Exception in {call_back_name} when handling msg on "
335360
f"'{msg.topic}': '{msg.payload}'" # belongs to last-part
336361
)
362+
363+
self._attr_unique_id = (
364+
f"{self._device.temperature.group_address_state}_"
365+
f"{self._device.target_temperature.group_address_state}_"
366+
f"{self._device.target_temperature.group_address}_"
367+
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001
368+
)
369+
370+
return (
371+
f"Exception in {call_back_name} when handling msg on "
372+
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe]
373+
)

crates/ruff_python_formatter/src/comments/placement.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ pub(super) fn place_comment<'a>(
2828
handle_parenthesized_comment(comment, source)
2929
.or_else(|comment| handle_end_of_line_comment_around_body(comment, source))
3030
.or_else(|comment| handle_own_line_comment_around_body(comment, source))
31-
.or_else(|comment| {
32-
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source)
33-
})
3431
.or_else(|comment| handle_enclosed_comment(comment, comment_ranges, source))
3532
}
3633

@@ -358,6 +355,41 @@ fn handle_enclosed_comment<'a>(
358355
AnyNodeRef::ExprGenerator(generator) if generator.parenthesized => {
359356
handle_bracketed_end_of_line_comment(comment, source)
360357
}
358+
AnyNodeRef::StmtReturn(_) => {
359+
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source)
360+
}
361+
AnyNodeRef::StmtAssign(assignment)
362+
if comment.preceding_node().is_some_and(|preceding| {
363+
preceding.ptr_eq(AnyNodeRef::from(&*assignment.value))
364+
}) =>
365+
{
366+
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source)
367+
}
368+
AnyNodeRef::StmtAnnAssign(assignment)
369+
if comment.preceding_node().is_some_and(|preceding| {
370+
assignment
371+
.value
372+
.as_deref()
373+
.is_some_and(|value| preceding.ptr_eq(value.into()))
374+
}) =>
375+
{
376+
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source)
377+
}
378+
AnyNodeRef::StmtAugAssign(assignment)
379+
if comment.preceding_node().is_some_and(|preceding| {
380+
preceding.ptr_eq(AnyNodeRef::from(&*assignment.value))
381+
}) =>
382+
{
383+
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source)
384+
}
385+
AnyNodeRef::StmtTypeAlias(assignment)
386+
if comment.preceding_node().is_some_and(|preceding| {
387+
preceding.ptr_eq(AnyNodeRef::from(&*assignment.value))
388+
}) =>
389+
{
390+
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source)
391+
}
392+
361393
_ => CommentPlacement::Default(comment),
362394
}
363395
}
@@ -2089,7 +2121,8 @@ fn handle_comprehension_comment<'a>(
20892121
CommentPlacement::Default(comment)
20902122
}
20912123

2092-
/// Handle end-of-line comments for parenthesized implicitly concatenated strings:
2124+
/// Handle end-of-line comments for parenthesized implicitly concatenated strings when used in
2125+
/// a `FormatStatementLastExpression` context:
20932126
///
20942127
/// ```python
20952128
/// a = (

crates/ruff_python_formatter/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,13 @@ if True:
186186
#[test]
187187
fn quick_test() {
188188
let source = r#"
189-
def test():
190-
a = 10 + (
191-
"Exception in {call_back_name} when handling msg on "
192-
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe]
193-
)
194-
189+
def main() -> None:
190+
if True:
191+
some_very_long_variable_name_abcdefghijk = Foo()
192+
some_very_long_variable_name_abcdefghijk = some_very_long_variable_name_abcdefghijk[
193+
some_very_long_variable_name_abcdefghijk.some_very_long_attribute_name
194+
== "This is a very long string abcdefghijk"
195+
]
195196
196197
"#;
197198
let source_type = PySourceType::Python;

crates/ruff_python_formatter/src/string/implicit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl Format<PyFormatContext<'_>> for FormatImplicitConcatenatedStringExpanded<'_
9999
StringLikePart::FString(part) => part.format().fmt(f),
100100
});
101101

102-
let part_comments = comments.leading_dangling_trailing(&part);
102+
let part_comments = comments.leading_dangling_trailing(part);
103103
joiner.entry(&format_args![
104104
leading_comments(part_comments.leading),
105105
format_part,

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/ruff_python_formatter/tests/fixtures.rs
33
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring.py
4-
snapshot_kind: text
54
---
65
## Input
76
```python
@@ -776,8 +775,7 @@ result_f = (
776775
)
777776

778777
(
779-
f"{1}"
780-
f"{2}" # comment 3
778+
f"{1}{2}" # comment 3
781779
)
782780

783781
(
@@ -1551,8 +1549,7 @@ result_f = (
15511549
)
15521550

15531551
(
1554-
f"{1}"
1555-
f"{2}" # comment 3
1552+
f"{1}{2}" # comment 3
15561553
)
15571554

15581555
(

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

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/ruff_python_formatter/tests/fixtures.rs
33
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string_assignment.py
4-
snapshot_kind: text
54
---
65
## Input
76
```python
@@ -307,11 +306,36 @@ a = (
307306
"b" # belongs to `b`
308307
)
309308
309+
a: Literal[str] = (
310+
"a"
311+
"b" # belongs to `b`
312+
)
313+
314+
a += (
315+
"a"
316+
"b" # belongs to `b`
317+
)
318+
319+
a = (
320+
r"a"
321+
r"b" # belongs to `b`
322+
)
323+
310324
a = (
311325
"a"
312326
"b"
313327
) # belongs to the assignment
314328
329+
a = (((
330+
"a"
331+
"b" # belongs to `b`
332+
)))
333+
334+
a = (((
335+
"a"
336+
"b"
337+
) # belongs to the f-string expression
338+
))
315339
316340
a = (
317341
"a" "b" # belongs to the f-string expression
@@ -341,7 +365,18 @@ a = 10 + (
341365
"Exception in {call_back_name} when handling msg on "
342366
f"'{msg.topic}': '{msg.payload}'" # belongs to last-part
343367
)
344-
```
368+
369+
self._attr_unique_id = (
370+
f"{self._device.temperature.group_address_state}_"
371+
f"{self._device.target_temperature.group_address_state}_"
372+
f"{self._device.target_temperature.group_address}_"
373+
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001
374+
)
375+
376+
return (
377+
f"Exception in {call_back_name} when handling msg on "
378+
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe]
379+
)```
345380

346381
## Output
347382
```python
@@ -672,8 +707,29 @@ a = (
672707
"b" # belongs to `b`
673708
)
674709
710+
a: Literal[str] = (
711+
"a"
712+
"b" # belongs to `b`
713+
)
714+
715+
a += (
716+
"a"
717+
"b" # belongs to `b`
718+
)
719+
720+
a = (
721+
r"a"
722+
r"b" # belongs to `b`
723+
)
724+
675725
a = "ab" # belongs to the assignment
676726
727+
a = (
728+
"a"
729+
"b" # belongs to `b`
730+
)
731+
732+
a = "ab" # belongs to the f-string expression
677733
678734
a = "ab" # belongs to the f-string expression
679735
@@ -685,8 +741,7 @@ a = (
685741
a = "abc" # belongs to the f-string expression
686742
687743
logger.error(
688-
f"Failed to run task {task} for job"
689-
f"with id {str(job.id)}" # type: ignore[union-attr]
744+
f"Failed to run task {task} for jobwith id {str(job.id)}" # type: ignore[union-attr]
690745
)
691746
692747
a = (
@@ -698,4 +753,16 @@ a = 10 + (
698753
"Exception in {call_back_name} when handling msg on "
699754
f"'{msg.topic}': '{msg.payload}'" # belongs to last-part
700755
)
756+
757+
self._attr_unique_id = (
758+
f"{self._device.temperature.group_address_state}_"
759+
f"{self._device.target_temperature.group_address_state}_"
760+
f"{self._device.target_temperature.group_address}_"
761+
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001
762+
)
763+
764+
return (
765+
f"Exception in {call_back_name} when handling msg on "
766+
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe]
767+
)
701768
```

0 commit comments

Comments
 (0)