diff --git a/CHANGES.md b/CHANGES.md index cc42cde..148f8bc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ ### Stable style +- Fix crash while formatting a long `del` statement containing tuples (#4628) ### Preview style diff --git a/src/black/parsing.py b/src/black/parsing.py index 0019b0c..93d017c 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -213,7 +213,7 @@ def _stringify_ast(node: ast.AST, parent_stack: list[ast.AST]) -> Iterator[str]: and isinstance(node, ast.Delete) and isinstance(item, ast.Tuple) ): - for elt in item.elts: + for elt in _unwrap_tuples(item): yield from _stringify_ast_with_new_parent( 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__}" + + +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 diff --git a/tests/data/cases/cantfit.py b/tests/data/cases/cantfit.py index f002326..6191192 100644 --- a/tests/data/cases/cantfit.py +++ b/tests/data/cases/cantfit.py @@ -31,7 +31,8 @@ raise ValueError(err.format(key)) 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 ([], name_1, name_2), [(), [], name_4, name_3], name_1[[name_2 for name_1 in name_0]] +del (), # output @@ -91,3 +92,9 @@ 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 ((),)