-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Can't determine input argument type with structural pattern matching when class is set as attribute of another class #13455
Comments
If you declare from dataclasses import dataclass
from typing import TypeAlias
@dataclass
class A:
x: str
class B:
A: TypeAlias = A
match A("y"):
case B.A(z):
print(f"x: {z}") https://mypy-play.net/?mypy=latest&python=3.10&gist=1cffece9249ad85ed59efbbb4a4a6e44 |
@AlexWaygood Thanks for the response. Unfortunately in my actual code, the container class is a dataclass, so mypy raises a not supported error which led me to #12792. I'm not completely sure but based on that issue it looks like mypy just doesn't support TypeAlias in dataclasses, and there aren't any plans to ever support it, not even a wish list open issue. Is that right? Also @JukkaL said in #12792 (review)
but I'm not sure if he was talking about a workaround in the mypy code or a workaround for users like me. |
Right, yeah, this is an awkward corner-case. Type aliases inside dataclasses are difficult for a number of reasons. But you can't put the alias in the global scope here — you need the dotted lookup for pattern-matching. I guess my best suggestion here is... Don't do that? Would it be possible to make the container class not-a-dataclass? |
There are some other ways to go, sure. Thanks again for your help. |
No worries. Sorry about the limitations here :) |
Bug Report
mypy errors with "Cannot determine type" of an input argument when using structural pattern matching against a class set as an attribute of another class.
To Reproduce
Example:
case A
works fine, as does definingA
directly insideB
instead of settingA = A
.Expected Behavior
I don't get the mypy error above.
Actual Behavior
I get the error.
Your Environment
Mypy version used: 0.971 (compiled: yes)
Python version used: 3.10.4
Operating system and version: Ubuntu 22.04
The text was updated successfully, but these errors were encountered: