Skip to content

Commit 0f18001

Browse files
authored
chore: Refactor delimiter_split() (#4257)
Signed-off-by: RedGuy12 <[email protected]>
1 parent 8990023 commit 0f18001

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/black/linegen.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,14 @@ def _get_last_non_comment_leaf(line: Line) -> Optional[int]:
11351135
return None
11361136

11371137

1138+
def _can_add_trailing_comma(leaf: Leaf, features: Collection[Feature]) -> bool:
1139+
if is_vararg(leaf, within={syms.typedargslist}):
1140+
return Feature.TRAILING_COMMA_IN_DEF in features
1141+
if is_vararg(leaf, within={syms.arglist, syms.argument}):
1142+
return Feature.TRAILING_COMMA_IN_CALL in features
1143+
return True
1144+
1145+
11381146
def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) -> Line:
11391147
if (
11401148
safe
@@ -1156,20 +1164,21 @@ def delimiter_split(
11561164
If the appropriate Features are given, the split will add trailing commas
11571165
also in function signatures and calls that contain `*` and `**`.
11581166
"""
1159-
try:
1160-
last_leaf = line.leaves[-1]
1161-
except IndexError:
1167+
if len(line.leaves) == 0:
11621168
raise CannotSplit("Line empty") from None
1169+
last_leaf = line.leaves[-1]
11631170

11641171
bt = line.bracket_tracker
11651172
try:
11661173
delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
11671174
except ValueError:
11681175
raise CannotSplit("No delimiters found") from None
11691176

1170-
if delimiter_priority == DOT_PRIORITY:
1171-
if bt.delimiter_count_with_priority(delimiter_priority) == 1:
1172-
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
1177+
if (
1178+
delimiter_priority == DOT_PRIORITY
1179+
and bt.delimiter_count_with_priority(delimiter_priority) == 1
1180+
):
1181+
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
11731182

11741183
current_line = Line(
11751184
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
@@ -1198,15 +1207,8 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
11981207
yield from append_to_line(comment_after)
11991208

12001209
lowest_depth = min(lowest_depth, leaf.bracket_depth)
1201-
if leaf.bracket_depth == lowest_depth:
1202-
if is_vararg(leaf, within={syms.typedargslist}):
1203-
trailing_comma_safe = (
1204-
trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features
1205-
)
1206-
elif is_vararg(leaf, within={syms.arglist, syms.argument}):
1207-
trailing_comma_safe = (
1208-
trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
1209-
)
1210+
if trailing_comma_safe and leaf.bracket_depth == lowest_depth:
1211+
trailing_comma_safe = _can_add_trailing_comma(leaf, features)
12101212

12111213
if last_leaf.type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf:
12121214
current_line = _safe_add_trailing_comma(
@@ -1220,6 +1222,7 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
12201222
current_line = Line(
12211223
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
12221224
)
1225+
12231226
if current_line:
12241227
current_line = _safe_add_trailing_comma(
12251228
trailing_comma_safe, delimiter_priority, current_line

0 commit comments

Comments
 (0)