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

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

Closed
aucampia opened this issue Jul 13, 2023 · 3 comments · Fixed by #3950
Labels
C: parser How we parse code. Or fail to parse it. T: bug Something isn't working

Comments

@aucampia
Copy link

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
@aucampia aucampia added the T: bug Something isn't working label Jul 13, 2023
@JelleZijlstra
Copy link
Collaborator

Thanks, cc @isidentical as this seems to be an interaction with the match-case support you added. I'll take a look too when I get a chance.

@JelleZijlstra JelleZijlstra added the C: parser How we parse code. Or fail to parse it. label Jul 13, 2023
@richardxia
Copy link

In case it's helpful, I hit the same error in a slightly different way, which leads to a much more confusing error message, since the parser fails on the line after the match keyword rather than the line containing the match keyword itself. Here's an example case and its corresponding error message:

class Foo:
    type = "Foo"


foo = Foo()
bar = Foo()

match (foo.type, bar.type):
    case _:
        pass

Black's output:

error: cannot format blacktest.py: Cannot parse: 9:0:     case _:

Oh no! 💥 💔 💥
1 file failed to reformat.

Changing the attribute name to something other than type avoids the issue, as does assigning the match argument tuple to a variable before the match statement.

@wind-shift
Copy link

Hello, apparently I hit exactly the same bug

from collections import namedtuple
from typing import Any


class AAA:
    def do_xxx(
        self: Any,
    ):
        # Custom modules
        Foo = namedtuple("Foo", "a,b,type")
        foo0 = Foo(0, 1, 1)
        foo = Foo(0,1,1)
        match (foo.a, foo.type):
            case (foo0.a, foo0.b):
                return
            case _:
                raise ValueError("still rejected")
error: cannot format black_minimal_example.py: Cannot parse: 13:8:         match (foo.a, foo.type):

Oh no! 💥 💔 💥
1 file failed to reformat.

Applying this change:

-        match (foo.a, foo.type):
+        match (foo.a, foo.b):

makes black works again.

It’s working in black 23.3.0, but not with 23.9.0

JelleZijlstra added a commit to JelleZijlstra/black that referenced this issue Oct 16, 2023
…match

Fixes psf#3790

Slightly hacky, but I think this is correct and it should also improve performance somewhat.
hauntsaninja pushed a commit that referenced this issue Oct 17, 2023
…match (#3950)

Fixes #3790

Slightly hacky, but I think this is correct and it should also improve performance somewhat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: parser How we parse code. Or fail to parse it. T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants