Skip to content

failed to format a file with a with-statement and a named-expression #4629

@15r10nk

Description

@15r10nk

Describe the bug

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

with {} as name_3, (name_2 := name_4):

    def name_3[**name_1]():
        pass

black reported the following error:

> black -l 100 -C bug.py
error: cannot format bug.py: Cannot parse: 1:26: with {} as name_3, name_2 := name_4:

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

but it can be parsed by cpython:

from ast import parse
parse(
    'with {} as name_3, (name_2 := name_4):\n'
    '\n'
    '    def name_3[**name_1]():\n'
    '        pass\n'
)

result:


returncode: 0

Environment

  • Black's version: current main (dbb14ea)
  • 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)
The issue description above was created by a script. Let me know if you think it can be improved.
One think I noted is that the following code causes also a formatting failure:

with a, (name_2 := name_4):
        pass

But pysource-minimize did not found this minimal example for some reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions