@@ -973,29 +973,7 @@ def _maybe_split_omitting_optional_parens(
973973 try :
974974 # The RHSResult Omitting Optional Parens.
975975 rhs_oop = _first_right_hand_split (line , omit = omit )
976- is_split_right_after_equal = (
977- len (rhs .head .leaves ) >= 2 and rhs .head .leaves [- 2 ].type == token .EQUAL
978- )
979- rhs_head_contains_brackets = any (
980- leaf .type in BRACKETS for leaf in rhs .head .leaves [:- 1 ]
981- )
982- # the -1 is for the ending optional paren
983- rhs_head_short_enough = is_line_short_enough (
984- rhs .head , mode = replace (mode , line_length = mode .line_length - 1 )
985- )
986- rhs_head_explode_blocked_by_magic_trailing_comma = (
987- rhs .head .magic_trailing_comma is None
988- )
989- if (
990- not (
991- is_split_right_after_equal
992- and rhs_head_contains_brackets
993- and rhs_head_short_enough
994- and rhs_head_explode_blocked_by_magic_trailing_comma
995- )
996- # the omit optional parens split is preferred by some other reason
997- or _prefer_split_rhs_oop_over_rhs (rhs_oop , rhs , mode )
998- ):
976+ if _prefer_split_rhs_oop_over_rhs (rhs_oop , rhs , mode ):
999977 yield from _maybe_split_omitting_optional_parens (
1000978 rhs_oop , line , mode , features = features , omit = omit
1001979 )
@@ -1006,8 +984,15 @@ def _maybe_split_omitting_optional_parens(
1006984 if line .is_chained_assignment :
1007985 pass
1008986
1009- elif not can_be_split (rhs .body ) and not is_line_short_enough (
1010- rhs .body , mode = mode
987+ elif (
988+ not can_be_split (rhs .body )
989+ and not is_line_short_enough (rhs .body , mode = mode )
990+ and not (
991+ Preview .wrap_long_dict_values_in_parens
992+ and rhs .opening_bracket .parent
993+ and rhs .opening_bracket .parent .parent
994+ and rhs .opening_bracket .parent .parent .type == syms .dictsetmaker
995+ )
1011996 ):
1012997 raise CannotSplit (
1013998 "Splitting failed, body is still too long and can't be split."
@@ -1038,6 +1023,44 @@ def _prefer_split_rhs_oop_over_rhs(
10381023 Returns whether we should prefer the result from a split omitting optional parens
10391024 (rhs_oop) over the original (rhs).
10401025 """
1026+ # contains unsplittable type ignore
1027+ if (
1028+ rhs_oop .head .contains_unsplittable_type_ignore ()
1029+ or rhs_oop .body .contains_unsplittable_type_ignore ()
1030+ or rhs_oop .tail .contains_unsplittable_type_ignore ()
1031+ ):
1032+ return True
1033+
1034+ # Retain optional parens around dictionary values
1035+ if (
1036+ Preview .wrap_long_dict_values_in_parens
1037+ and rhs .opening_bracket .parent
1038+ and rhs .opening_bracket .parent .parent
1039+ and rhs .opening_bracket .parent .parent .type == syms .dictsetmaker
1040+ and rhs .body .bracket_tracker .delimiters
1041+ ):
1042+ # Unless the split is inside the key
1043+ return any (leaf .type == token .COLON for leaf in rhs_oop .tail .leaves )
1044+
1045+ # the split is right after `=`
1046+ if not (len (rhs .head .leaves ) >= 2 and rhs .head .leaves [- 2 ].type == token .EQUAL ):
1047+ return True
1048+
1049+ # the left side of assignment contains brackets
1050+ if not any (leaf .type in BRACKETS for leaf in rhs .head .leaves [:- 1 ]):
1051+ return True
1052+
1053+ # the left side of assignment is short enough (the -1 is for the ending optional
1054+ # paren)
1055+ if not is_line_short_enough (
1056+ rhs .head , mode = replace (mode , line_length = mode .line_length - 1 )
1057+ ):
1058+ return True
1059+
1060+ # the left side of assignment won't explode further because of magic trailing comma
1061+ if rhs .head .magic_trailing_comma is not None :
1062+ return True
1063+
10411064 # If we have multiple targets, we prefer more `=`s on the head vs pushing them to
10421065 # the body
10431066 rhs_head_equal_count = [leaf .type for leaf in rhs .head .leaves ].count (token .EQUAL )
@@ -1065,10 +1088,6 @@ def _prefer_split_rhs_oop_over_rhs(
10651088 # the first line is short enough
10661089 and is_line_short_enough (rhs_oop .head , mode = mode )
10671090 )
1068- # contains unsplittable type ignore
1069- or rhs_oop .head .contains_unsplittable_type_ignore ()
1070- or rhs_oop .body .contains_unsplittable_type_ignore ()
1071- or rhs_oop .tail .contains_unsplittable_type_ignore ()
10721091 )
10731092
10741093
0 commit comments