Skip to content

crash while formatting a delete statement with given line length #4625

@15r10nk

Description

@15r10nk

Describe the bug

The following code can not be parsed/formatted by black:

del ([], name_2), [(), [], name_4, name_3], name_1[[name_2 for name_1 in name_0]]

black reported the following error:

> black -l 80 -C bug.py
error: cannot format bug.py: INTERNAL ERROR: Black 25.1.1.dev21+g254e829.d20250317 on Python (CPython) 3
.12.6 produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf
/black/issues.  This diff might be helpful: /tmp/blk_qtuwnx6z.log

Oh no! 💥 💔 💥
1 file failed to reformat.

but it can be parsed by cpython:

from ast import parse
parse('''
del ([], name_2), [(), [], name_4, name_3], name_1[[name_2 for name_1 in name_0]]
''')

result:


returncode: 0

black transforms the code with --fast into

del (
    ([], name_2),
    [(), [], name_4, name_3],
    name_1[[name_2 for name_1 in name_0]],
)

This has technical the same meaning but a different ast representation (tuple vs targets) https://docs.python.org/3/library/ast.html#ast.Delete.

❯ echo 'del (a,b); del a,b' | python -m ast
Module(
   body=[
      Delete(
         targets=[
            Tuple(
               elts=[
                  Name(id='a', ctx=Del()),
                  Name(id='b', ctx=Del())],
               ctx=Del())]),
      Delete(
         targets=[
            Name(id='a', ctx=Del()),
            Name(id='b', ctx=Del())])],
   type_ignores=[])

Environment

  • Black's version: current main (5342d2e
    )
  • OS and Python version: Linux/Python 3.12.6 (main, Sep 9 2024, 22:11:19) [Clang 18.1.8 ]

Additional context

The bug was found by pysource-codegen (see #3908)

Metadata

Metadata

Labels

T: bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions