-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Comments
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. |
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 class Foo:
type = "Foo"
foo = Foo()
bar = Foo()
match (foo.type, bar.type):
case _:
pass Black's output:
Changing the attribute name to something other than |
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")
Applying this change: - match (foo.a, foo.type):
+ match (foo.a, foo.b): makes black works again. It’s working in black |
…match Fixes psf#3790 Slightly hacky, but I think this is correct and it should also improve performance somewhat.
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:
And run it with these arguments:
The resulting error is:
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:
Environment
The text was updated successfully, but these errors were encountered: