Skip to content

cannot parse assignment expression in preview style since 23.1 #3758

@wizpig64

Description

@wizpig64

Hello,

I seem to have found a bug in black's preview style regarding assignment expressions, that has been present since version 23.1.0.

Describe the bug

When using an assignment expression in my code example, black with preview=true complains it cannot parse the line. Black preview=false accepts the code happily and leaves no changes.

To Reproduce

Here's my code example:

# file.py
from pydriller import Commit

commits: list[Commit] = []
update_hashes: list[str] = []
upstream_messages: list[str] = []
parsed = [
    {
        "hash": commit.hash,
        "author": f"{commit.author.name} <{commit.author.email}>",
        # black 23.1 --preview can't parse the following line:
        "is_update": (up := commit.hash in update_hashes),
        "is_upstream": up and commit.msg in upstream_messages,
    }
    for commit in commits
]

And run it with these arguments:

$ black file.py --target-version py311 --preview

The resulting error is:

cannot format file.py: Cannot parse: 12:24: "is_update": up := commit.hash in update_hashes,

Expected behavior

It should parse the line like it does with preview=false.

Also, look at the error above. The line of code shown doesn't include the parentheses like it does in my source. Without the parens, cpython can't parse it either!

Environment

  • Black's version: 23.3, also tested on main branch at g839ef35.
  • OS and Python version: Linux/Python 3.11.4

Additional context

I love you lots <3

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: preview styleIssues with the preview and unstable style. Add the name of the responsible feature in the title.T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions