ruff_python_formatter 0.0.4

This is an internal component crate of Ruff
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
use ruff_formatter::{FormatError, RemoveSoftLinesBuffer, format_args, write};
use ruff_python_ast::{
    AnyNodeRef, Expr, ExprAttribute, ExprCall, FString, Operator, StmtAssign, StringLike, TString,
    TypeParams,
};

use crate::builders::parenthesize_if_expands;
use crate::comments::{
    Comments, LeadingDanglingTrailingComments, SourceComment, trailing_comments,
};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::expr_lambda::ExprLambdaLayout;
use crate::expression::parentheses::{
    NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, optional_parentheses,
};
use crate::expression::{
    can_omit_optional_parentheses, has_own_parentheses, has_parentheses,
    maybe_parenthesize_expression,
};
use crate::other::interpolated_string::InterpolatedStringLayout;
use crate::prelude::*;
use crate::statement::trailing_semicolon;
use crate::string::StringLikeExtensions;
use crate::string::implicit::{
    FormatImplicitConcatenatedStringExpanded, FormatImplicitConcatenatedStringFlat,
    ImplicitConcatenatedLayout,
};

#[derive(Default)]
pub struct FormatStmtAssign;

impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
    fn fmt_fields(&self, item: &StmtAssign, f: &mut PyFormatter) -> FormatResult<()> {
        let StmtAssign {
            range: _,
            node_index: _,
            targets,
            value,
        } = item;

        let (first, rest) = targets.split_first().ok_or(FormatError::syntax_error(
            "Expected at least on assignment target",
        ))?;

        // The first target is special because it never gets parenthesized nor does the formatter remove parentheses if unnecessary.
        let format_first = FormatTargetWithEqualOperator {
            target: first,
            preserve_parentheses: true,
        };

        // Avoid parenthesizing the value if the last target before the assigned value expands.
        if let Some((last, head)) = rest.split_last() {
            format_first.fmt(f)?;

            for target in head {
                FormatTargetWithEqualOperator {
                    target,
                    preserve_parentheses: false,
                }
                .fmt(f)?;
            }

            FormatStatementsLastExpression::RightToLeft {
                before_operator: AnyBeforeOperator::Expression(last),
                operator: AnyAssignmentOperator::Assign,
                value,
                statement: item.into(),
            }
            .fmt(f)?;
        }
        // Avoid parenthesizing the value for single-target assignments where the
        // target has its own parentheses (list, dict, tuple, ...) and the target expands.
        else if has_target_own_parentheses(first, f.context())
            && !f.context().is_expression_parenthesized(first.into())
        {
            FormatStatementsLastExpression::RightToLeft {
                before_operator: AnyBeforeOperator::Expression(first),
                operator: AnyAssignmentOperator::Assign,
                value,
                statement: item.into(),
            }
            .fmt(f)?;
        }
        // For single targets that have no split points, parenthesize the value only
        // if it makes it fit. Otherwise omit the parentheses.
        else {
            format_first.fmt(f)?;
            FormatStatementsLastExpression::left_to_right(value, item).fmt(f)?;
        }

        if f.options().source_type().is_ipynb()
            && f.context().node_level().is_last_top_level_statement()
            && trailing_semicolon(item.into(), f.context().source()).is_some()
            && matches!(targets.as_slice(), [Expr::Name(_)])
        {
            token(";").fmt(f)?;
        }

        Ok(())
    }
}

/// Formats a single target with the equal operator.
struct FormatTargetWithEqualOperator<'a> {
    target: &'a Expr,

    /// Whether parentheses should be preserved as in the source or if the target
    /// should only be parenthesized if necessary (because of comments or because it doesn't fit).
    preserve_parentheses: bool,
}

impl Format<PyFormatContext<'_>> for FormatTargetWithEqualOperator<'_> {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
        // Preserve parentheses for the first target or around targets with leading or trailing comments.
        if self.preserve_parentheses
            || f.context().comments().has_leading(self.target)
            || f.context().comments().has_trailing(self.target)
        {
            self.target.format().fmt(f)?;
        } else if should_parenthesize_target(self.target, f.context()) {
            parenthesize_if_expands(&self.target.format().with_options(Parentheses::Never))
                .fmt(f)?;
        } else {
            self.target
                .format()
                .with_options(Parentheses::Never)
                .fmt(f)?;
        }

        write!(f, [space(), token("="), space()])
    }
}

/// Formats the last expression in statements that start with a keyword (like `return`) or after an operator (assignments).
///
/// The implementation avoids parenthesizing unsplittable values (like `None`, `True`, `False`, Names, a subset of strings)
/// if the value won't fit even when parenthesized.
///
/// ## Trailing comments
/// Trailing comments are inlined inside the `value`'s parentheses rather than formatted at the end
/// of the statement for unsplittable values if the `value` gets parenthesized.
///
/// Inlining the trailing comments prevent situations where the parenthesized value
/// still exceeds the configured line width, but parenthesizing helps to make the trailing comment fit.
/// Instead, it only parenthesizes `value` if it makes both the `value` and the trailing comment fit.
/// See [PR 8431](https://github.com/astral-sh/ruff/pull/8431) for more details.
///
/// The implementation formats the statement's and value's trailing end of line comments:
/// * after the expression if the expression needs no parentheses (necessary or the `expand_parent` makes the group never fit).
/// * inside the parentheses if the expression exceeds the line-width.
///
/// ```python
/// a = loooooooooooooooooooooooooooong # with_comment
/// b = (
///     short # with_comment
/// )
/// ```
///
/// Which gets formatted to:
///
/// ```python
/// # formatted
/// a = (
///     loooooooooooooooooooooooooooong # with comment
/// )
/// b = short # with comment
/// ```
///
/// The long name gets parenthesized because it exceeds the configured line width and the trailing comment of the
/// statement gets formatted inside (instead of outside) the parentheses.
///
/// No parentheses are added for `short` because it fits into the configured line length, regardless of whether
/// the comment exceeds the line width or not.
///
/// This logic isn't implemented in `place_comment` by associating trailing statement comments to the expression because
/// doing so breaks the suite empty lines formatting that relies on trailing comments to be stored on the statement.
#[derive(Debug)]
pub(super) enum FormatStatementsLastExpression<'a> {
    /// Prefers to split what's left of `value` before splitting the value.
    ///
    /// ```python
    /// aaaaaaa[bbbbbbbb] = some_long_value
    /// ```
    ///
    /// This layout splits `aaaaaaa[bbbbbbbb]` first assuming the whole statements exceeds the line width, resulting in
    ///
    /// ```python
    /// aaaaaaa[
    ///     bbbbbbbb
    /// ] = some_long_value
    /// ```
    ///
    /// This layout is preferred over [`Self::RightToLeft`] if the left is unsplittable (single
    /// keyword like `return` or a Name) because it has better performance characteristics.
    LeftToRight {
        /// The right side of an assignment or the value returned in a return statement.
        value: &'a Expr,

        /// The parent statement that encloses the `value` expression.
        statement: AnyNodeRef<'a>,
    },

    /// Prefers parenthesizing the value before splitting the left side. Specific to assignments.
    ///
    /// Formats what's left of `value` together with the assignment operator and the assigned `value`.
    /// This layout prefers parenthesizing the value over parenthesizing the left (target or type annotation):
    ///
    /// ```python
    /// aaaaaaa[bbbbbbbb] = some_long_value
    /// ```
    ///
    /// gets formatted to...
    ///
    /// ```python
    /// aaaaaaa[bbbbbbbb] = (
    ///     some_long_value
    /// )
    /// ```
    ///
    /// ... regardless whether the value will fit or not.
    ///
    /// The left only gets parenthesized if the left exceeds the configured line width on its own or
    /// is forced to split because of a magical trailing comma or contains comments:
    ///
    /// ```python
    /// aaaaaaa[bbbbbbbb_exceeds_the_line_width] = some_long_value
    /// ```
    ///
    /// gets formatted to
    /// ```python
    /// aaaaaaa[
    ///     bbbbbbbb_exceeds_the_line_width
    /// ] = some_long_value
    /// ```
    ///
    /// The layout avoids parenthesizing the value when the left splits to avoid
    /// unnecessary parentheses. Adding the parentheses, as shown in the below example, reduces readability.
    ///
    /// ```python
    /// aaaaaaa[
    ///     bbbbbbbb_exceeds_the_line_width
    /// ] = (
    ///     some_long_value
    /// )
    ///
    /// ## Non-fluent Call Expressions
    /// Non-fluent call expressions in the `value` position are only parenthesized if the opening parentheses
    /// exceeds the configured line length. The layout prefers splitting after the opening parentheses
    /// if the `callee` expression and the opening parentheses fit.
    /// fits on the line.
    RightToLeft {
        /// The expression that comes before the assignment operator. This is either
        /// the last target, or the type annotation of an annotated assignment.
        before_operator: AnyBeforeOperator<'a>,

        /// The assignment operator. Either `Assign` (`=`) or the operator used by the augmented assignment statement.
        operator: AnyAssignmentOperator,

        /// The assigned `value`.
        value: &'a Expr,

        /// The assignment statement.
        statement: AnyNodeRef<'a>,
    },
}

impl<'a> FormatStatementsLastExpression<'a> {
    pub(super) fn left_to_right<S: Into<AnyNodeRef<'a>>>(value: &'a Expr, statement: S) -> Self {
        Self::LeftToRight {
            value,
            statement: statement.into(),
        }
    }
}

impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
        match self {
            FormatStatementsLastExpression::LeftToRight { value, statement } => {
                let can_inline_comment = should_inline_comments(value, *statement, f.context());

                let string_like = StringLike::try_from(*value).ok();
                let format_interpolated_string = string_like
                    .and_then(|string| format_interpolated_string_assignment(string, f.context()));

                let format_implicit_flat = string_like.and_then(|string| {
                    FormatImplicitConcatenatedStringFlat::new(string, f.context())
                });

                if !can_inline_comment
                    && format_implicit_flat.is_none()
                    && format_interpolated_string.is_none()
                {
                    return maybe_parenthesize_value(value, *statement).fmt(f);
                }

                let comments = f.context().comments().clone();
                let expression_comments = comments.leading_dangling_trailing(*value);

                if let Some(inline_comments) = OptionalParenthesesInlinedComments::new(
                    &expression_comments,
                    *statement,
                    &comments,
                ) {
                    let group_id = f.group_id("optional_parentheses");

                    // Special case for implicit concatenated strings in assignment value positions.
                    // The special handling is necessary to prevent an instability where an assignment has
                    // a trailing own line comment and the implicit concatenated string fits on the line,
                    // but only if the comment doesn't get inlined.
                    //
                    // ```python
                    // ____aaa = (
                    //     "aaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvv"
                    // )  # c
                    // ```
                    //
                    // Without the special handling, this would get formatted to:
                    // ```python
                    // ____aaa = (
                    //     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvv"
                    // )  # c
                    // ```
                    //
                    // However, this now gets reformatted again because Ruff now takes the `BestFit` layout for the string
                    // because the value is no longer an implicit concatenated string.
                    // ```python
                    // ____aaa = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvv"  # c
                    // ```
                    //
                    // The special handling here ensures that the implicit concatenated string only gets
                    // joined **if** it fits with the trailing comment inlined. Otherwise, keep the multiline
                    // formatting.
                    if let Some(flat) = format_implicit_flat {
                        inline_comments.mark_formatted();
                        let string = flat.string();

                        let flat = format_with(|f| {
                            if string.is_interpolated_string() {
                                let mut buffer = RemoveSoftLinesBuffer::new(&mut *f);

                                write!(buffer, [flat])
                            } else {
                                flat.fmt(f)
                            }
                        })
                        .memoized();

                        // F-string or T-string containing an expression with a magic trailing comma, a comment, or a
                        // multiline debug expression should never be joined. Use the default layout.
                        // ```python
                        // aaaa = f"abcd{[
                        //    1,
                        //    2,
                        // ]}" "more"
                        // ```
                        if string.is_interpolated_string() && flat.inspect(f)?.will_break() {
                            inline_comments.mark_unformatted();

                            return write!(
                                f,
                                [maybe_parenthesize_expression(
                                    value,
                                    *statement,
                                    Parenthesize::IfBreaks,
                                )]
                            );
                        }

                        let expanded = format_with(|f| {
                            let f =
                                &mut WithNodeLevel::new(NodeLevel::Expression(Some(group_id)), f);

                            write!(
                                f,
                                [FormatImplicitConcatenatedStringExpanded::new(
                                    string,
                                    ImplicitConcatenatedLayout::MaybeFlat
                                )]
                            )
                        });

                        // Join the implicit concatenated string if it fits on a single line
                        // ```python
                        // a = "testmorelong" # comment
                        // ```
                        let single_line = format_with(|f| write!(f, [flat, inline_comments]));

                        // Parenthesize the string but join the implicit concatenated string and inline the comment.
                        // ```python
                        // a = (
                        //      "testmorelong" # comment
                        // )
                        // ```
                        let joined_parenthesized = format_with(|f| {
                            group(&format_args![
                                token("("),
                                soft_block_indent(&format_args![flat, inline_comments]),
                                token(")"),
                            ])
                            .with_id(Some(group_id))
                            .should_expand(true)
                            .fmt(f)
                        });

                        // Keep the implicit concatenated string multiline and don't inline the comment.
                        // ```python
                        // a = (
                        //      "test"
                        //      "more"
                        //      "long"
                        // ) # comment
                        // ```
                        let implicit_expanded = format_with(|f| {
                            group(&format_args![
                                token("("),
                                block_indent(&expanded),
                                token(")"),
                                inline_comments,
                            ])
                            .with_id(Some(group_id))
                            .should_expand(true)
                            .fmt(f)
                        });

                        // We can't use `optional_parentheses` here because the `inline_comments` contains
                        // a `expand_parent` which results in an instability because the next format
                        // collapses the parentheses.
                        // We can't use `parenthesize_if_expands` because it defaults to
                        // the *flat* layout when the expanded layout doesn't fit.
                        best_fitting![single_line, joined_parenthesized, implicit_expanded]
                            .with_mode(BestFittingMode::AllLines)
                            .fmt(f)?;
                    } else if let Some(format_interpolated_string) = format_interpolated_string {
                        inline_comments.mark_formatted();

                        let interpolated_string_flat = format_with(|f| {
                            let mut buffer = RemoveSoftLinesBuffer::new(&mut *f);

                            write!(buffer, [format_interpolated_string])
                        })
                        .memoized();
                        // F/T-String containing an interpolation with a magic trailing comma, a comment, or a
                        // multiline debug interpolation should never be joined. Use the default layout.
                        // ```python
                        // aaaa = f"aaaa {[
                        //     1, 2,
                        // ]} bbbb"
                        // ```
                        if interpolated_string_flat.inspect(f)?.will_break() {
                            inline_comments.mark_unformatted();

                            return write!(
                                f,
                                [maybe_parenthesize_expression(
                                    value,
                                    *statement,
                                    Parenthesize::IfBreaks,
                                )]
                            );
                        }

                        // Considering the following example:
                        // ```python
                        // aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{
                        //     expression}moreeeeeeeeeeeeeeeee"
                        // ```

                        // Flatten the f/t-string.
                        // ```python
                        // aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{expression}moreeeeeeeeeeeeeeeee"
                        // ```
                        let single_line =
                            format_with(|f| write!(f, [interpolated_string_flat, inline_comments]));

                        // Parenthesize the t-string and flatten the t-string.
                        // ```python
                        // aaaaaaaaaaaaaaaaaa = (
                        //     t"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{expression}moreeeeeeeeeeeeeeeee"
                        // )
                        // ```
                        let joined_parenthesized = format_with(|f| {
                            group(&format_args![
                                token("("),
                                soft_block_indent(&format_args![
                                    interpolated_string_flat,
                                    inline_comments
                                ]),
                                token(")"),
                            ])
                            .with_id(Some(group_id))
                            .should_expand(true)
                            .fmt(f)
                        });

                        // Avoid flattening or parenthesizing the f/t-string, keep the original
                        // f/t-string formatting.
                        // ```python
                        // aaaaaaaaaaaaaaaaaa = t"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{
                        //     expression
                        // }moreeeeeeeeeeeeeeeee"
                        // ```
                        let format_interpolated_string = format_with(|f| {
                            write!(f, [format_interpolated_string, inline_comments])
                        });

                        best_fitting![
                            single_line,
                            joined_parenthesized,
                            format_interpolated_string
                        ]
                        .with_mode(BestFittingMode::AllLines)
                        .fmt(f)?;
                    } else {
                        best_fit_parenthesize(&format_once(|f| {
                            inline_comments.mark_formatted();

                            value.format().with_options(Parentheses::Never).fmt(f)?;

                            if !inline_comments.is_empty() {
                                // If the expressions exceeds the line width, format the comments in the parentheses
                                if_group_breaks(&inline_comments).fmt(f)?;
                            }

                            Ok(())
                        }))
                        .with_group_id(Some(group_id))
                        .fmt(f)?;

                        if !inline_comments.is_empty() {
                            // If the line fits into the line width, format the comments after the parenthesized expression
                            if_group_fits_on_line(&inline_comments)
                                .with_group_id(Some(group_id))
                                .fmt(f)?;
                        }
                    }

                    Ok(())
                } else {
                    // Preserve the parentheses if the expression has any leading or trailing comments,
                    // to avoid syntax errors, similar to `maybe_parenthesize_expression`.
                    value.format().with_options(Parentheses::Always).fmt(f)
                }
            }
            FormatStatementsLastExpression::RightToLeft {
                before_operator,
                operator,
                value,
                statement,
            } => {
                let should_inline_comments = should_inline_comments(value, *statement, f.context());

                let string_like = StringLike::try_from(*value).ok();
                let format_interpolated_string = string_like
                    .and_then(|string| format_interpolated_string_assignment(string, f.context()));
                let format_implicit_flat = string_like.and_then(|string| {
                    FormatImplicitConcatenatedStringFlat::new(string, f.context())
                });
                // Use the normal `maybe_parenthesize_layout` for splittable `value`s.
                if !should_inline_comments
                    && !should_non_inlineable_use_best_fit(value, *statement, f.context())
                    && format_implicit_flat.is_none()
                    && format_interpolated_string.is_none()
                {
                    return write!(
                        f,
                        [
                            before_operator,
                            space(),
                            operator,
                            space(),
                            maybe_parenthesize_value(value, *statement)
                        ]
                    );
                }

                let comments = f.context().comments().clone();
                let expression_comments = comments.leading_dangling_trailing(*value);

                // Don't inline comments for attribute and call expressions for black compatibility
                let inline_comments = if should_inline_comments
                    || format_implicit_flat.is_some()
                    || format_interpolated_string.is_some()
                {
                    OptionalParenthesesInlinedComments::new(
                        &expression_comments,
                        *statement,
                        &comments,
                    )
                } else if expression_comments.has_leading()
                    || expression_comments.has_trailing_own_line()
                {
                    None
                } else {
                    Some(OptionalParenthesesInlinedComments::default())
                };

                let Some(inline_comments) = inline_comments else {
                    // Preserve the parentheses if the expression has any leading or trailing own line comments
                    // same as `maybe_parenthesize_expression`
                    return write!(
                        f,
                        [
                            before_operator,
                            space(),
                            operator,
                            space(),
                            value.format().with_options(Parentheses::Always)
                        ]
                    );
                };

                // Prevent inline comments to be formatted as part of the expression.
                inline_comments.mark_formatted();

                let last_target = before_operator.memoized();
                let last_target_breaks = last_target.inspect(f)?.will_break();

                // Don't parenthesize the `value` if it is known that the target will break.
                // This is mainly a performance optimisation that avoids unnecessary memoization
                // and using the costly `BestFitting` layout if it is already known that only the last variant
                // can ever fit because the left breaks.
                if format_implicit_flat.is_none()
                    && format_interpolated_string.is_none()
                    && last_target_breaks
                {
                    return write!(
                        f,
                        [
                            last_target,
                            space(),
                            operator,
                            space(),
                            value.format().with_options(Parentheses::Never),
                            inline_comments
                        ]
                    );
                }

                let format_value = format_with(|f| {
                    if let Some(format_implicit_flat) = format_implicit_flat.as_ref() {
                        if format_implicit_flat.string().is_interpolated_string() {
                            // Remove any soft line breaks emitted by the f-string formatting.
                            // This is important when formatting f-strings as part of an assignment right side
                            // because `best_fit_parenthesize` will otherwise still try to break inner
                            // groups if wrapped in a `group(..).should_expand(true)`
                            let mut buffer = RemoveSoftLinesBuffer::new(&mut *f);
                            write!(buffer, [format_implicit_flat])
                        } else {
                            format_implicit_flat.fmt(f)
                        }
                    } else if let Some(format_interpolated_string) =
                        format_interpolated_string.as_ref()
                    {
                        // Similar to above, remove any soft line breaks emitted by the f-string
                        // formatting.
                        let mut buffer = RemoveSoftLinesBuffer::new(&mut *f);
                        write!(buffer, [format_interpolated_string])
                    } else {
                        value.format().with_options(Parentheses::Never).fmt(f)
                    }
                })
                .memoized();

                // Tries to fit the `left` and the `value` on a single line:
                // ```python
                // a = b = c
                // ```
                let single_line = format_with(|f| {
                    write!(
                        f,
                        [
                            last_target,
                            space(),
                            operator,
                            space(),
                            format_value,
                            inline_comments
                        ]
                    )
                });

                // Don't break the last assignment target but parenthesize the value to see if it fits (break right first).
                //
                // ```python
                // a["bbbbb"] = (
                //      c
                // )
                // ```
                let flat_target_parenthesize_value = format_with(|f| {
                    write!(
                        f,
                        [
                            last_target,
                            space(),
                            operator,
                            space(),
                            token("("),
                            group(&soft_block_indent(&format_args![
                                format_value,
                                inline_comments
                            ]))
                            .should_expand(true),
                            token(")")
                        ]
                    )
                });

                // Fall back to parenthesizing (or splitting) the last target part if we can't make the value
                // fit. Don't parenthesize the value to avoid unnecessary parentheses.
                //
                // ```python
                // a[
                //      "bbbbb"
                // ] = c
                // ```
                let split_target_flat_value = format_with(|f| {
                    write!(
                        f,
                        [
                            group(&last_target).should_expand(true),
                            space(),
                            operator,
                            space(),
                            format_value,
                            inline_comments
                        ]
                    )
                });

                // For call expressions, prefer breaking after the call expression's opening parentheses
                // over parenthesizing the entire call expression.
                // For subscripts, try breaking the subscript first
                // For attribute chains that contain any parenthesized value: Try expanding the parenthesized value first.
                if value.is_call_expr() || value.is_subscript_expr() || value.is_attribute_expr() {
                    best_fitting![
                        single_line,
                        // Avoid parenthesizing the call expression if the `(` fit on the line
                        format_args![
                            last_target,
                            space(),
                            operator,
                            space(),
                            group(&format_value).should_expand(true),
                        ],
                        flat_target_parenthesize_value,
                        split_target_flat_value
                    ]
                    .fmt(f)
                } else if let Some(format_implicit_flat) = &format_implicit_flat {
                    // F-String containing an expression with a magic trailing comma, a comment, or a
                    // multiline debug expression should never be joined. Use the default layout.
                    //
                    // ```python
                    // aaaa = f"abcd{[
                    //    1,
                    //    2,
                    // ]}" "more"
                    // ```
                    if format_implicit_flat.string().is_interpolated_string()
                        && format_value.inspect(f)?.will_break()
                    {
                        inline_comments.mark_unformatted();

                        return write!(
                            f,
                            [
                                before_operator,
                                space(),
                                operator,
                                space(),
                                maybe_parenthesize_expression(
                                    value,
                                    *statement,
                                    Parenthesize::IfBreaks
                                )
                            ]
                        );
                    }

                    let group_id = f.group_id("optional_parentheses");
                    let format_expanded = format_with(|f| {
                        let f = &mut WithNodeLevel::new(NodeLevel::Expression(Some(group_id)), f);

                        FormatImplicitConcatenatedStringExpanded::new(
                            StringLike::try_from(*value).unwrap(),
                            ImplicitConcatenatedLayout::MaybeFlat,
                        )
                        .fmt(f)
                    })
                    .memoized();

                    // Keep the target flat, parenthesize the value, and keep it multiline.
                    //
                    // ```python
                    // Literal[ "a", "b"] = (
                    //      "looooooooooooooooooooooooooooooong"
                    //      "string"
                    //  ) # comment
                    // ```
                    let flat_target_value_parenthesized_multiline = format_with(|f| {
                        write!(
                            f,
                            [
                                last_target,
                                space(),
                                operator,
                                space(),
                                token("("),
                                group(&soft_block_indent(&format_expanded))
                                    .with_id(Some(group_id))
                                    .should_expand(true),
                                token(")"),
                                inline_comments
                            ]
                        )
                    });

                    // Expand the parent and parenthesize the joined string with the inlined comment.
                    //
                    // ```python
                    // Literal[
                    //     "a",
                    //     "b",
                    //  ] = (
                    //      "not that long string" # comment
                    //  )
                    // ```
                    let split_target_value_parenthesized_flat = format_with(|f| {
                        write!(
                            f,
                            [
                                group(&last_target).should_expand(true),
                                space(),
                                operator,
                                space(),
                                token("("),
                                group(&soft_block_indent(&format_args![
                                    format_value,
                                    inline_comments
                                ]))
                                .should_expand(true),
                                token(")")
                            ]
                        )
                    });

                    // The most expanded variant: Expand both the target and the string.
                    //
                    // ```python
                    // Literal[
                    //     "a",
                    //     "b",
                    //  ] = (
                    //      "looooooooooooooooooooooooooooooong"
                    //      "string"
                    //  ) # comment
                    // ```
                    let split_target_value_parenthesized_multiline = format_with(|f| {
                        write!(
                            f,
                            [
                                group(&last_target).should_expand(true),
                                space(),
                                operator,
                                space(),
                                token("("),
                                group(&soft_block_indent(&format_expanded))
                                    .with_id(Some(group_id))
                                    .should_expand(true),
                                token(")"),
                                inline_comments
                            ]
                        )
                    });

                    // This is only a perf optimisation. No point in trying all the "flat-target"
                    // variants if we know that the last target must break.
                    if last_target_breaks {
                        best_fitting![
                            split_target_flat_value,
                            split_target_value_parenthesized_flat,
                            split_target_value_parenthesized_multiline,
                        ]
                        .with_mode(BestFittingMode::AllLines)
                        .fmt(f)
                    } else {
                        best_fitting![
                            single_line,
                            flat_target_parenthesize_value,
                            flat_target_value_parenthesized_multiline,
                            split_target_flat_value,
                            split_target_value_parenthesized_flat,
                            split_target_value_parenthesized_multiline,
                        ]
                        .with_mode(BestFittingMode::AllLines)
                        .fmt(f)
                    }
                } else if let Some(format_interpolated_string) = &format_interpolated_string {
                    // F/T-String containing an interpolation with a magic trailing comma, a comment, or a
                    // multiline debug expression should never be joined. Use the default layout.
                    //
                    // ```python
                    // aaaa, bbbb = t"aaaa {[
                    //     1, 2,
                    // ]} bbbb"
                    // ```
                    if format_value.inspect(f)?.will_break() {
                        inline_comments.mark_unformatted();

                        return write!(
                            f,
                            [
                                before_operator,
                                space(),
                                operator,
                                space(),
                                maybe_parenthesize_expression(
                                    value,
                                    *statement,
                                    Parenthesize::IfBreaks
                                )
                            ]
                        );
                    }

                    let format_interpolated_string =
                        format_with(|f| write!(f, [format_interpolated_string, inline_comments]))
                            .memoized();

                    // Considering the following initial source:
                    //
                    // ```python
                    // aaaaaaaaaaaa["bbbbbbbbbbbbbbbb"] = (
                    //     t"aaaaaaaaaaaaaaaaaaa {
                    //         aaaaaaaaa + bbbbbbbbbbb + cccccccccccccc} ddddddddddddddddddd"
                    // )
                    // ```
                    //
                    // Keep the target flat, and use the regular f/t-string formatting.
                    //
                    // ```python
                    // aaaaaaaaaaaa["bbbbbbbbbbbbbbbb"] = t"aaaaaaaaaaaaaaaaaaa {
                    //     aaaaaaaaa + bbbbbbbbbbb + cccccccccccccc
                    // } ddddddddddddddddddd"
                    // ```
                    let flat_target_regular_interpolated_string = format_with(|f| {
                        write!(
                            f,
                            [
                                last_target,
                                space(),
                                operator,
                                space(),
                                format_interpolated_string
                            ]
                        )
                    });

                    // Expand the parent and parenthesize the flattened f/t-string.
                    //
                    // ```python
                    // aaaaaaaaaaaa[
                    //     "bbbbbbbbbbbbbbbb"
                    // ] = (
                    //     t"aaaaaaaaaaaaaaaaaaa {aaaaaaaaa + bbbbbbbbbbb + cccccccccccccc} ddddddddddddddddddd"
                    // )
                    // ```
                    let split_target_value_parenthesized_flat = format_with(|f| {
                        write!(
                            f,
                            [
                                group(&last_target).should_expand(true),
                                space(),
                                operator,
                                space(),
                                token("("),
                                group(&soft_block_indent(&format_args![
                                    format_value,
                                    inline_comments
                                ]))
                                .should_expand(true),
                                token(")")
                            ]
                        )
                    });

                    // Expand the parent, and use the regular f/t-string formatting.
                    //
                    // ```python
                    // aaaaaaaaaaaa[
                    //     "bbbbbbbbbbbbbbbb"
                    // ] = t"aaaaaaaaaaaaaaaaaaa {
                    //     aaaaaaaaa + bbbbbbbbbbb + cccccccccccccc
                    // } ddddddddddddddddddd"
                    // ```
                    let split_target_regular_interpolated_string = format_with(|f| {
                        write!(
                            f,
                            [
                                group(&last_target).should_expand(true),
                                space(),
                                operator,
                                space(),
                                format_interpolated_string,
                            ]
                        )
                    });

                    // This is only a perf optimisation. No point in trying all the "flat-target"
                    // variants if we know that the last target must break.
                    if last_target_breaks {
                        best_fitting![
                            split_target_flat_value,
                            split_target_value_parenthesized_flat,
                            split_target_regular_interpolated_string,
                        ]
                        .with_mode(BestFittingMode::AllLines)
                        .fmt(f)
                    } else {
                        best_fitting![
                            single_line,
                            flat_target_parenthesize_value,
                            flat_target_regular_interpolated_string,
                            split_target_flat_value,
                            split_target_value_parenthesized_flat,
                            split_target_regular_interpolated_string,
                        ]
                        .with_mode(BestFittingMode::AllLines)
                        .fmt(f)
                    }
                } else {
                    best_fitting![
                        single_line,
                        flat_target_parenthesize_value,
                        split_target_flat_value
                    ]
                    .fmt(f)
                }
            }
        }
    }
}

#[derive(Debug, Copy, Clone)]
enum InterpolatedString<'a> {
    FString(&'a FString),
    TString(&'a TString),
}

impl Format<PyFormatContext<'_>> for InterpolatedString<'_> {
    fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
        match self {
            InterpolatedString::FString(string) => string.format().fmt(f),
            InterpolatedString::TString(string) => string.format().fmt(f),
        }
    }
}

/// Formats an f/t-string that is at the value position of an assignment statement.
///
/// For legibility, we discuss only the case of f-strings below, but the
/// same comments apply to t-strings.
///
/// This is just a wrapper around [`FormatFString`](crate::other::f_string::FormatFString) while
/// considering a special case when the f-string is at an assignment statement's value position.
/// This is necessary to prevent an instability where an f-string contains a multiline expression
/// and the f-string fits on the line, but only when it's surrounded by parentheses.
///
/// ```python
/// aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{
///     expression}moreeeeeeeeeeeeeeeee"
/// ```
///
/// Without the special handling, this would get formatted to:
/// ```python
/// aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{
///     expression
/// }moreeeeeeeeeeeeeeeee"
/// ```
///
/// However, if the parentheses already existed in the source like:
/// ```python
/// aaaaaaaaaaaaaaaaaa = (
///     f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{expression}moreeeeeeeeeeeeeeeee"
/// )
/// ```
///
/// Then, it would remain unformatted because it fits on the line. This means that even in the
/// first example, the f-string should be formatted by surrounding it with parentheses.
///
/// One might ask why not just use the `BestFit` layout in this case. Consider the following
/// example in which the f-string doesn't fit on the line even when surrounded by parentheses:
/// ```python
/// xxxxxxx = f"{
///     {'aaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccccccccc'}
/// }"
/// ```
///
/// The `BestFit` layout will format this as:
/// ```python
/// xxxxxxx = (
///     f"{
///         {
///             'aaaaaaaaaaaaaaaaaaaaaaaaa',
///             'bbbbbbbbbbbbbbbbbbbbbbbbbbb',
///             'cccccccccccccccccccccccccc',
///         }
///     }"
/// )
/// ```
///
/// The reason for this is because (a) f-string already has a multiline expression thus it tries to
/// break the expression and (b) the `BestFit` layout doesn't considers the layout where the
/// multiline f-string isn't surrounded by parentheses.
fn format_interpolated_string_assignment<'a>(
    string: StringLike<'a>,
    context: &PyFormatContext,
) -> Option<InterpolatedString<'a>> {
    let (interpolated_string, elements) = match string {
        StringLike::TString(expr) => {
            let t_string = expr.as_single_part_tstring()?;
            (InterpolatedString::TString(t_string), &t_string.elements)
        }
        StringLike::FString(expr) => {
            let f_string = expr.as_single_part_fstring()?;
            (InterpolatedString::FString(f_string), &f_string.elements)
        }
        _ => {
            return None;
        }
    };

    // If the f/t-string is flat, there are no breakpoints from which it can be made multiline.
    // This is the case when the f/t-string has no expressions or if it does then the expressions
    // are flat (no newlines).
    if InterpolatedStringLayout::from_interpolated_string_elements(elements, context.source())
        .is_flat()
    {
        return None;
    }

    // This checks whether the f/t-string is multi-line and it can *never* be flattened. Thus,
    // it's useless to try the flattened layout.
    if string.is_multiline(context) {
        return None;
    }

    Some(interpolated_string)
}

#[derive(Debug, Default)]
struct OptionalParenthesesInlinedComments<'a> {
    expression: &'a [SourceComment],
    statement: &'a [SourceComment],
}

impl<'a> OptionalParenthesesInlinedComments<'a> {
    fn new(
        expression_comments: &LeadingDanglingTrailingComments<'a>,
        statement: AnyNodeRef<'a>,
        comments: &'a Comments<'a>,
    ) -> Option<Self> {
        if expression_comments.has_leading() || expression_comments.has_trailing_own_line() {
            return None;
        }

        let statement_trailing_comments = comments.trailing(statement);
        let after_end_of_line = statement_trailing_comments
            .partition_point(|comment| comment.line_position().is_end_of_line());
        let (stmt_inline_comments, _) = statement_trailing_comments.split_at(after_end_of_line);

        let after_end_of_line = expression_comments
            .trailing
            .partition_point(|comment| comment.line_position().is_end_of_line());

        let (expression_inline_comments, trailing_own_line_comments) =
            expression_comments.trailing.split_at(after_end_of_line);

        debug_assert!(
            trailing_own_line_comments.is_empty(),
            "The method should have returned early if the expression has trailing own line comments"
        );

        Some(OptionalParenthesesInlinedComments {
            expression: expression_inline_comments,
            statement: stmt_inline_comments,
        })
    }

    fn is_empty(&self) -> bool {
        self.expression.is_empty() && self.statement.is_empty()
    }

    fn iter_comments(&self) -> impl Iterator<Item = &'a SourceComment> {
        self.expression.iter().chain(self.statement)
    }

    fn mark_formatted(&self) {
        for comment in self.expression {
            comment.mark_formatted();
        }
    }

    fn mark_unformatted(&self) {
        for comment in self.expression {
            comment.mark_unformatted();
        }
    }
}

impl Format<PyFormatContext<'_>> for OptionalParenthesesInlinedComments<'_> {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
        for comment in self.iter_comments() {
            comment.mark_unformatted();
        }

        write!(
            f,
            [
                trailing_comments(self.expression),
                trailing_comments(self.statement)
            ]
        )
    }
}

#[derive(Copy, Clone, Debug)]
pub(super) enum AnyAssignmentOperator {
    Assign,
    AugAssign(Operator),
}

impl Format<PyFormatContext<'_>> for AnyAssignmentOperator {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
        match self {
            AnyAssignmentOperator::Assign => token("=").fmt(f),
            AnyAssignmentOperator::AugAssign(operator) => {
                write!(f, [operator.format(), token("=")])
            }
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub(super) enum AnyBeforeOperator<'a> {
    Expression(&'a Expr),
    TypeParams(&'a TypeParams),
}

impl Format<PyFormatContext<'_>> for AnyBeforeOperator<'_> {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
        match self {
            AnyBeforeOperator::Expression(expression) => {
                // Preserve parentheses around targets with comments.
                if f.context().comments().has_leading(*expression)
                    || f.context().comments().has_trailing(*expression)
                {
                    expression
                        .format()
                        .with_options(Parentheses::Preserve)
                        .fmt(f)
                }
                // Never parenthesize targets that come with their own parentheses, e.g. don't parenthesize lists or dictionary literals.
                else if should_parenthesize_target(expression, f.context()) {
                    if can_omit_optional_parentheses(expression, f.context()) {
                        optional_parentheses(&expression.format().with_options(Parentheses::Never))
                            .fmt(f)
                    } else {
                        parenthesize_if_expands(
                            &expression.format().with_options(Parentheses::Never),
                        )
                        .fmt(f)
                    }
                } else {
                    expression.format().with_options(Parentheses::Never).fmt(f)
                }
            }
            // Never parenthesize type params
            AnyBeforeOperator::TypeParams(type_params) => type_params.format().fmt(f),
        }
    }
}

/// Returns `true` for unsplittable expressions for which comments should be inlined.
fn should_inline_comments(
    expression: &Expr,
    parent: AnyNodeRef,
    context: &PyFormatContext,
) -> bool {
    match expression {
        Expr::Name(_) | Expr::NoneLiteral(_) | Expr::NumberLiteral(_) | Expr::BooleanLiteral(_) => {
            true
        }
        Expr::StringLiteral(string) => {
            string.needs_parentheses(parent, context) == OptionalParentheses::BestFit
        }
        Expr::BytesLiteral(bytes) => {
            bytes.needs_parentheses(parent, context) == OptionalParentheses::BestFit
        }
        Expr::FString(fstring) => {
            fstring.needs_parentheses(parent, context) == OptionalParentheses::BestFit
        }
        Expr::TString(tstring) => {
            tstring.needs_parentheses(parent, context) == OptionalParentheses::BestFit
        }
        _ => false,
    }
}

/// Tests whether an expression for which comments shouldn't be inlined should use the best fit layout
fn should_non_inlineable_use_best_fit(
    expr: &Expr,
    parent: AnyNodeRef,
    context: &PyFormatContext,
) -> bool {
    match expr {
        Expr::Attribute(attribute) => {
            attribute.needs_parentheses(parent, context) == OptionalParentheses::BestFit
        }
        Expr::Call(call) => call.needs_parentheses(parent, context) == OptionalParentheses::BestFit,
        Expr::Subscript(subscript) => {
            subscript.needs_parentheses(parent, context) == OptionalParentheses::BestFit
        }
        _ => false,
    }
}

/// Returns `true` for targets that have their own set of parentheses when they split,
/// in which case we want to avoid parenthesizing the assigned value.
pub(super) fn has_target_own_parentheses(target: &Expr, context: &PyFormatContext) -> bool {
    matches!(target, Expr::Tuple(_)) || has_own_parentheses(target, context).is_some()
}

pub(super) fn should_parenthesize_target(target: &Expr, context: &PyFormatContext) -> bool {
    !(has_target_own_parentheses(target, context)
        || is_attribute_with_parenthesized_value(target, context))
}

fn is_attribute_with_parenthesized_value(target: &Expr, context: &PyFormatContext) -> bool {
    match target {
        Expr::Attribute(ExprAttribute { value, .. }) => {
            has_parentheses(value.as_ref(), context).is_some()
                || is_attribute_with_parenthesized_value(value, context)
        }
        Expr::Subscript(_) => true,
        Expr::Call(ExprCall { arguments, .. }) => !arguments.is_empty(),
        _ => false,
    }
}

/// Like [`maybe_parenthesize_expression`] but with special handling for lambdas.
fn maybe_parenthesize_value<'a>(
    expression: &'a Expr,
    parent: AnyNodeRef<'a>,
) -> MaybeParenthesizeValue<'a> {
    MaybeParenthesizeValue { expression, parent }
}

struct MaybeParenthesizeValue<'a> {
    expression: &'a Expr,
    parent: AnyNodeRef<'a>,
}

impl Format<PyFormatContext<'_>> for MaybeParenthesizeValue<'_> {
    fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
        let MaybeParenthesizeValue { expression, parent } = self;

        if let Expr::Lambda(lambda) = expression
            && !f.context().comments().has_leading(lambda)
        {
            parenthesize_if_expands(&lambda.format().with_options(ExprLambdaLayout::Assignment))
                .fmt(f)
        } else {
            maybe_parenthesize_expression(expression, *parent, Parenthesize::IfBreaks).fmt(f)
        }
    }
}