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

Function _first_right_hand_split

src/black/linegen.py:961–1061  ·  view source on GitHub ↗

Split the line into head, body, tail starting with the last bracket pair. Note: this function should not have side effects. It's relied upon by _maybe_split_omitting_optional_parens to get an opinion whether to prefer splitting on the right side of an assignment statement.

(
    line: Line,
    omit: Collection[LeafID] = (),
)

Source from the content-addressed store, hash-verified

959
960
961def _first_right_hand_split(
962 line: Line,
963 omit: Collection[LeafID] = (),
964) -> RHSResult:
965 """Split the line into head, body, tail starting with the last bracket pair.
966
967 Note: this function should not have side effects. It's relied upon by
968 _maybe_split_omitting_optional_parens to get an opinion whether to prefer
969 splitting on the right side of an assignment statement.
970 """
971 tail_leaves: list[Leaf] = []
972 body_leaves: list[Leaf] = []
973 head_leaves: list[Leaf] = []
974 current_leaves = tail_leaves
975 opening_bracket: Leaf | None = None
976 closing_bracket: Leaf | None = None
977 for leaf in reversed(line.leaves):
978 if current_leaves is body_leaves:
979 if leaf is opening_bracket:
980 current_leaves = head_leaves if body_leaves else tail_leaves
981 current_leaves.append(leaf)
982 if current_leaves is tail_leaves:
983 if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
984 opening_bracket = leaf.opening_bracket
985 closing_bracket = leaf
986 current_leaves = body_leaves
987 if not (opening_bracket and closing_bracket and head_leaves):
988 # If there is no opening or closing_bracket that means the split failed and
989 # all content is in the tail. Otherwise, if `head_leaves` are empty, it means
990 # the matching `opening_bracket` wasn't available on `line` anymore.
991 raise CannotSplit("No brackets found")
992
993 tail_leaves.reverse()
994 body_leaves.reverse()
995 head_leaves.reverse()
996
997 body: Line | None = None
998 if (
999 Preview.hug_parens_with_braces_and_square_brackets in line.mode
1000 and tail_leaves[0].value
1001 and tail_leaves[0].opening_bracket is head_leaves[-1]
1002 ):
1003 inner_body_leaves = list(body_leaves)
1004 hugged_opening_leaves: list[Leaf] = []
1005 hugged_closing_leaves: list[Leaf] = []
1006 is_unpacking = body_leaves[0].type in [token.STAR, token.DOUBLESTAR]
1007 unpacking_offset: int = 1 if is_unpacking else 0
1008 while (
1009 len(inner_body_leaves) >= 2 + unpacking_offset
1010 and inner_body_leaves[-1].type in CLOSING_BRACKETS
1011 and inner_body_leaves[-1].opening_bracket
1012 is inner_body_leaves[unpacking_offset]
1013 ):
1014 if unpacking_offset:
1015 hugged_opening_leaves.append(inner_body_leaves.pop(0))
1016 unpacking_offset = 0
1017 hugged_opening_leaves.append(inner_body_leaves.pop(0))
1018 hugged_closing_leaves.insert(0, inner_body_leaves.pop())

Callers 2

right_hand_splitFunction · 0.85

Calls 8

is_line_short_enoughFunction · 0.90
RHSResultClass · 0.90
CannotSplitClass · 0.85
bracket_split_build_lineFunction · 0.85
replaceFunction · 0.85
popMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected