MCPcopy
hub / github.com/psf/black / _maybe_split_omitting_optional_parens

Function _maybe_split_omitting_optional_parens

src/black/linegen.py:1064–1129  ·  view source on GitHub ↗
(
    rhs: RHSResult,
    line: Line,
    mode: Mode,
    features: Collection[Feature] = (),
    omit: Collection[LeafID] = (),
)

Source from the content-addressed store, hash-verified

1062
1063
1064def _maybe_split_omitting_optional_parens(
1065 rhs: RHSResult,
1066 line: Line,
1067 mode: Mode,
1068 features: Collection[Feature] = (),
1069 omit: Collection[LeafID] = (),
1070) -> Iterator[Line]:
1071 if (
1072 Feature.FORCE_OPTIONAL_PARENTHESES not in features
1073 # the opening bracket is an optional paren
1074 and rhs.opening_bracket.type == token.LPAR
1075 and not rhs.opening_bracket.value
1076 # the closing bracket is an optional paren
1077 and rhs.closing_bracket.type == token.RPAR
1078 and not rhs.closing_bracket.value
1079 # it's not an import (optional parens are the only thing we can split on
1080 # in this case; attempting a split without them is a waste of time)
1081 and not line.is_import
1082 # and we can actually remove the parens
1083 and can_omit_invisible_parens(rhs, mode.line_length)
1084 ):
1085 omit = {id(rhs.closing_bracket), *omit}
1086 try:
1087 # The RHSResult Omitting Optional Parens.
1088 rhs_oop = _first_right_hand_split(line, omit=omit)
1089 if _prefer_split_rhs_oop_over_rhs(rhs_oop, rhs, mode):
1090 yield from _maybe_split_omitting_optional_parens(
1091 rhs_oop, line, mode, features=features, omit=omit
1092 )
1093 return
1094
1095 except CannotSplit as e:
1096 # For chained assignments we want to use the previous successful split
1097 if line.is_chained_assignment:
1098 pass
1099
1100 elif (
1101 not can_be_split(rhs.body)
1102 and not is_line_short_enough(rhs.body, mode=mode)
1103 and not (
1104 Preview.wrap_long_dict_values_in_parens
1105 and rhs.opening_bracket.parent
1106 and rhs.opening_bracket.parent.parent
1107 and rhs.opening_bracket.parent.parent.type == syms.dictsetmaker
1108 )
1109 ):
1110 raise CannotSplit(
1111 "Splitting failed, body is still too long and can't be split."
1112 ) from e
1113
1114 elif (
1115 rhs.head.contains_multiline_strings()
1116 or rhs.tail.contains_multiline_strings()
1117 ):
1118 raise CannotSplit(
1119 "The current optional pair of parentheses is bound to fail to"
1120 " satisfy the splitting algorithm because the head or the tail"
1121 " contains multiline strings which by definition never fit one"

Callers 1

right_hand_splitFunction · 0.85

Calls 8

can_be_splitFunction · 0.90
is_line_short_enoughFunction · 0.90
ensure_visibleFunction · 0.90
_first_right_hand_splitFunction · 0.85
CannotSplitClass · 0.85

Tested by

no test coverage detected