@@ -1135,6 +1135,14 @@ def _get_last_non_comment_leaf(line: Line) -> Optional[int]:
1135
1135
return None
1136
1136
1137
1137
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
+
1138
1146
def _safe_add_trailing_comma (safe : bool , delimiter_priority : int , line : Line ) -> Line :
1139
1147
if (
1140
1148
safe
@@ -1156,20 +1164,21 @@ def delimiter_split(
1156
1164
If the appropriate Features are given, the split will add trailing commas
1157
1165
also in function signatures and calls that contain `*` and `**`.
1158
1166
"""
1159
- try :
1160
- last_leaf = line .leaves [- 1 ]
1161
- except IndexError :
1167
+ if len (line .leaves ) == 0 :
1162
1168
raise CannotSplit ("Line empty" ) from None
1169
+ last_leaf = line .leaves [- 1 ]
1163
1170
1164
1171
bt = line .bracket_tracker
1165
1172
try :
1166
1173
delimiter_priority = bt .max_delimiter_priority (exclude = {id (last_leaf )})
1167
1174
except ValueError :
1168
1175
raise CannotSplit ("No delimiters found" ) from None
1169
1176
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" )
1173
1182
1174
1183
current_line = Line (
1175
1184
mode = line .mode , depth = line .depth , inside_brackets = line .inside_brackets
@@ -1198,15 +1207,8 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
1198
1207
yield from append_to_line (comment_after )
1199
1208
1200
1209
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 )
1210
1212
1211
1213
if last_leaf .type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf :
1212
1214
current_line = _safe_add_trailing_comma (
@@ -1220,6 +1222,7 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
1220
1222
current_line = Line (
1221
1223
mode = line .mode , depth = line .depth , inside_brackets = line .inside_brackets
1222
1224
)
1225
+
1223
1226
if current_line :
1224
1227
current_line = _safe_add_trailing_comma (
1225
1228
trailing_comma_safe , delimiter_priority , current_line
0 commit comments