Skip to content

Error trying to format line with parameter assignment and generator #2551

@tabrezm

Description

@tabrezm

Describe the bug

Black throws an error trying to format files containing a parameter assignment with a generator. This is valid Python code.

error: cannot format util.py: Cannot parse: 200:43:     if any(match := pattern_error.match(s) for s in buffer):
Oh no! 💥 💔 💥
1 file failed to reformat.

To Reproduce

Code sample:

    if any(match := pattern_error.match(s) for s in buffer):
        if match.group(2) == data_not_available:
            # Error OK to ignore.
            pass

Expected behavior

Black formats the file without errors. It seems to handle parameter assignment elsewhere just fine:

            if not (next_line := next(buffer_iter, None)):
                # Buffer is incomplete, so we return early and wait to be
                # reinvoked with a complete buffer.
                return status

Environment (please complete the following information):

  • Version: main
  • OS and Python version: Mac/Python 3.9.7

Does this bug also happen on main?

Yes

Additional context

None

Metadata

Metadata

Assignees

Labels

C: parserHow we parse code. Or fail to parse it.T: bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions