@@ -1136,6 +1136,14 @@ def _get_last_non_comment_leaf(line: Line) -> Optional[int]:
1136
1136
return None
1137
1137
1138
1138
1139
+ def _can_add_trailing_comma (leaf : Leaf , features : Collection [Feature ]) -> bool :
1140
+ if is_vararg (leaf , within = {syms .typedargslist }):
1141
+ return Feature .TRAILING_COMMA_IN_DEF in features
1142
+ if is_vararg (leaf , within = {syms .arglist , syms .argument }):
1143
+ return Feature .TRAILING_COMMA_IN_CALL in features
1144
+ return True
1145
+
1146
+
1139
1147
def _safe_add_trailing_comma (safe : bool , delimiter_priority : int , line : Line ) -> Line :
1140
1148
if (
1141
1149
safe
@@ -1160,20 +1168,21 @@ def delimiter_split(
1160
1168
If the appropriate Features are given, the split will add trailing commas
1161
1169
also in function signatures and calls that contain `*` and `**`.
1162
1170
"""
1163
- try :
1164
- last_leaf = line .leaves [- 1 ]
1165
- except IndexError :
1171
+ if len (line .leaves ) == 0 :
1166
1172
raise CannotSplit ("Line empty" ) from None
1173
+ last_leaf = line .leaves [- 1 ]
1167
1174
1168
1175
bt = line .bracket_tracker
1169
1176
try :
1170
1177
delimiter_priority = bt .max_delimiter_priority (exclude = {id (last_leaf )})
1171
1178
except ValueError :
1172
1179
raise CannotSplit ("No delimiters found" ) from None
1173
1180
1174
- if delimiter_priority == DOT_PRIORITY :
1175
- if bt .delimiter_count_with_priority (delimiter_priority ) == 1 :
1176
- raise CannotSplit ("Splitting a single attribute from its owner looks wrong" )
1181
+ if (
1182
+ delimiter_priority == DOT_PRIORITY
1183
+ and bt .delimiter_count_with_priority (delimiter_priority ) == 1
1184
+ ):
1185
+ raise CannotSplit ("Splitting a single attribute from its owner looks wrong" )
1177
1186
1178
1187
current_line = Line (
1179
1188
mode = line .mode , depth = line .depth , inside_brackets = line .inside_brackets
@@ -1212,15 +1221,8 @@ def append_comments(leaf: Leaf) -> Iterator[Line]:
1212
1221
yield from append_comments (leaf )
1213
1222
1214
1223
lowest_depth = min (lowest_depth , leaf .bracket_depth )
1215
- if leaf .bracket_depth == lowest_depth :
1216
- if is_vararg (leaf , within = {syms .typedargslist }):
1217
- trailing_comma_safe = (
1218
- trailing_comma_safe and Feature .TRAILING_COMMA_IN_DEF in features
1219
- )
1220
- elif is_vararg (leaf , within = {syms .arglist , syms .argument }):
1221
- trailing_comma_safe = (
1222
- trailing_comma_safe and Feature .TRAILING_COMMA_IN_CALL in features
1223
- )
1224
+ if trailing_comma_safe and leaf .bracket_depth == lowest_depth :
1225
+ trailing_comma_safe = _can_add_trailing_comma (leaf , features )
1224
1226
1225
1227
if last_leaf .type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf :
1226
1228
current_line = _safe_add_trailing_comma (
0 commit comments