Skip to content

Black 23.7.0 thinks accessing an attribute named type inside brackets in a switch statement is invalid syntax #3790

@aucampia

Description

@aucampia

Describe the bug

When black encounters access of a .type attribute inside brackets, inside a switch statement, it thinks that it is invalid syntax, even though python 3.11 executes it just fine.

To Reproduce

For example, take this code:

class SomeClass:
    type = ""


match (SomeClass.type):
    case _:
        print("Hello there ...")

And run it with these arguments:

$ pipx run --python python3.11 --spec=black==23.7.0 black --target-version py311 --diff file.py

The resulting error is:

error: cannot format file.py: Cannot parse: 5:0: match (SomeClass.type):

Expected behavior

It should leave the file unchanged or format it, as black 23.3.0 leaves it unchanged, and it is indeed valid syntax:

$ python3.11 --version
Python 3.11.4
$ python3.11 file.py
Hello there ...
$ pipx run --python python3.11 --spec=black==23.3.0 black --target-version py311 --diff file.py
⚠️  black is already on your PATH and installed at /home/iwana/.local/bin/black. Downloading and running anyway.
All done! ✨ 🍰 ✨
1 file would be left unchanged.

Environment

  • Black's version: 23.3.0
  • OS and Python version: Linux/Python 3.11.4

Metadata

Metadata

Assignees

No one assigned

    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