Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot parse assignment expression in preview style since 23.1 #3758

Closed
wizpig64 opened this issue Jun 30, 2023 · 2 comments · Fixed by #4155
Closed

cannot parse assignment expression in preview style since 23.1 #3758

wizpig64 opened this issue Jun 30, 2023 · 2 comments · Fixed by #4155
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. T: bug Something isn't working

Comments

@wizpig64
Copy link

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

@wizpig64 wizpig64 added the T: bug Something isn't working label Jun 30, 2023
@JelleZijlstra JelleZijlstra added the C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. label Jun 30, 2023
@JelleZijlstra
Copy link
Collaborator

Thanks for reporting! I'm guessing we incorrectly remove the parentheses here. Should be an easy fix.

@JelleZijlstra
Copy link
Collaborator

Bisects to 658c8d8 (#3440), cc @yilei

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants