Recursively unwrap tuples in del statements (#4628)

This commit is contained in:
Tushar Sadhwani 2025-03-20 03:32:40 +05:30 committed by GitHub
parent 5342d2eeda
commit dbb14eac93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 2 deletions

View File

@ -9,6 +9,7 @@
### Stable style ### Stable style
<!-- Changes that affect Black's stable style --> <!-- Changes that affect Black's stable style -->
- Fix crash while formatting a long `del` statement containing tuples (#4628)
### Preview style ### Preview style

View File

@ -213,7 +213,7 @@ def _stringify_ast(node: ast.AST, parent_stack: list[ast.AST]) -> Iterator[str]:
and isinstance(node, ast.Delete) and isinstance(node, ast.Delete)
and isinstance(item, ast.Tuple) and isinstance(item, ast.Tuple)
): ):
for elt in item.elts: for elt in _unwrap_tuples(item):
yield from _stringify_ast_with_new_parent( yield from _stringify_ast_with_new_parent(
elt, parent_stack, node elt, parent_stack, node
) )
@ -250,3 +250,11 @@ def _stringify_ast(node: ast.AST, parent_stack: list[ast.AST]) -> Iterator[str]:
) )
yield f"{' ' * len(parent_stack)}) # /{node.__class__.__name__}" yield f"{' ' * len(parent_stack)}) # /{node.__class__.__name__}"
def _unwrap_tuples(node: ast.Tuple) -> Iterator[ast.AST]:
for elt in node.elts:
if isinstance(elt, ast.Tuple):
yield from _unwrap_tuples(elt)
else:
yield elt

View File

@ -31,7 +31,8 @@
raise ValueError(err.format(key)) raise ValueError(err.format(key))
concatenated_strings = "some strings that are " "concatenated implicitly, so if you put them on separate " "lines it will fit" concatenated_strings = "some strings that are " "concatenated implicitly, so if you put them on separate " "lines it will fit"
del concatenated_strings, string_variable_name, normal_function_name, normal_name, need_more_to_make_the_line_long_enough del concatenated_strings, string_variable_name, normal_function_name, normal_name, need_more_to_make_the_line_long_enough
del ([], name_1, name_2), [(), [], name_4, name_3], name_1[[name_2 for name_1 in name_0]]
del (),
# output # output
@ -91,3 +92,9 @@
normal_name, normal_name,
need_more_to_make_the_line_long_enough, need_more_to_make_the_line_long_enough,
) )
del (
([], name_1, name_2),
[(), [], name_4, name_3],
name_1[[name_2 for name_1 in name_0]],
)
del ((),)