-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Match statement with tuple of union type fails #15426
Comments
Related: #12364 |
I think this is by design. This should probably be considered an enhancement request, not a bug. When mypy encounters a match statement, it evaluates the type of the subject expression. In this case of I recently added support in pyright for the specific case where narrowing affects only one element of a tuple and leaves the remaining elements unaffected. Your Mypy maintainers might want to implement a similar strategy here, since I've found this case to be quite common. It's relatively easy to implement, and it doesn't suffer from combinatoric explosions. |
I'd vote for this as well. Surprised to find this limitation today. My case is something like this: from enum import Enum, auto
class SomeEnum(Enum):
A = auto()
B = auto()
class Base:
pass
class Foo(Base):
pass
class Bar(Base):
pass
tag, value = SomeEnum.A, Foo()
match tag, value:
case SomeEnum.A, Foo():
print("Want to handle this case")
case SomeEnum.B, Bar():
print("This one too")
case _:
raise NotImplementedError(f"Can't handle {tag} with {value}") so I do not really care about the irrefutable pattern and it's "exploded" complex type. I care only about bound combinations. pyright works like a charm... |
Maybe mypy has gotten a little better in recent versions with
https://mypy-play.net/?mypy=latest&python=3.12&gist=22b719a18f14f6937dc75c484faa0ce3 |
Bug Report
I am typechecking a match statement with a tuple of union types and it fails.
To Reproduce
Playground: https://mypy-play.net/?mypy=latest&python=3.10&gist=992b588ec7e4807f3105db45fa9ef0a8
My union type:
(Problem persists even if I do:
)
This works fine:
This causes an error:
Actual Behavior
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: