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

Assert with walrus operator: Black produced invalid code on pass 1 #2144

Closed
alessandroberlati opened this issue Apr 26, 2021 · 1 comment
Closed
Labels
R: duplicate This issue or pull request already exists T: bug Something isn't working

Comments

@alessandroberlati
Copy link

alessandroberlati commented Apr 26, 2021

Describe the bug

Black produced invalid code on pass 1 when parsing an assert with walrus operator.

To Reproduce

  1. create a file test.py
def a():
    assert (x := 1 == 1)
  1. Run black test.py

Expected behavior

The output file should be the same as the input file.

Environment

  • Version: master/21.4b0
  • OS and Python version: macOS Big Sur 11.2.3 and Python3.8

Does this bug also happen on master?

Yes

Additional context

This is the log output:

❯ poetry run black test.py

error: cannot format test.py: INTERNAL ERROR: Black produced invalid code on pass 1: The requested source code has invalid Python 3 syntax.
If you are trying to format Python 2 files please reinstall Black with the 'python2' extra: `python3 -m pip install black[python2]`.. Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: /var/folders/xg/0x59hpln7h70f114ljbbjq400000gp/T/blk_es7z7u5g.log
Oh no! 💥 💔 💥
1 file failed to reformat.

❯ cat /var/folders/xg/0x59hpln7h70f114ljbbjq400000gp/T/blk_es7z7u5g.log

  File "/Users/alessandroberlati/aberlathy/test_black/.venv/lib/python3.8/site-packages/black/__init__.py", line 6508, in assert_equivalent
    dst_ast = parse_ast(dst)
  File "/Users/alessandroberlati/aberlathy/test_black/.venv/lib/python3.8/site-packages/black/__init__.py", line 6404, in parse_ast
    raise SyntaxError(
def a():
    assert x := 1 == 1

It's removing the parenthesis after the assert but that's not valid python.

@alessandroberlati alessandroberlati added the T: bug Something isn't working label Apr 26, 2021
@JelleZijlstra
Copy link
Collaborator

Fixed by #2143

@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: duplicate This issue or pull request already exists T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants