(
leaves: list[Leaf], original: Line, opening_bracket: Leaf
)
| 1231 | |
| 1232 | |
| 1233 | def _ensure_trailing_comma( |
| 1234 | leaves: list[Leaf], original: Line, opening_bracket: Leaf |
| 1235 | ) -> bool: |
| 1236 | if not leaves: |
| 1237 | return False |
| 1238 | # Ensure a trailing comma for imports |
| 1239 | if original.is_import: |
| 1240 | return True |
| 1241 | # ...and standalone function arguments |
| 1242 | if not original.is_def: |
| 1243 | return False |
| 1244 | if opening_bracket.value != "(": |
| 1245 | return False |
| 1246 | # Don't add commas if we already have any commas |
| 1247 | if any( |
| 1248 | leaf.type == token.COMMA and not is_part_of_annotation(leaf) for leaf in leaves |
| 1249 | ): |
| 1250 | return False |
| 1251 | |
| 1252 | # Find a leaf with a parent (comments don't have parents) |
| 1253 | leaf_with_parent = next((leaf for leaf in leaves if leaf.parent), None) |
| 1254 | if leaf_with_parent is None: |
| 1255 | return True |
| 1256 | # Don't add commas inside parenthesized return annotations |
| 1257 | if get_annotation_type(leaf_with_parent) == "return": |
| 1258 | return False |
| 1259 | # Don't add commas inside PEP 604 unions |
| 1260 | if ( |
| 1261 | leaf_with_parent.parent |
| 1262 | and leaf_with_parent.parent.next_sibling |
| 1263 | and leaf_with_parent.parent.next_sibling.type == token.VBAR |
| 1264 | ): |
| 1265 | return False |
| 1266 | return True |
| 1267 | |
| 1268 | |
| 1269 | def bracket_split_build_line( |
no test coverage detected