You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
fromdataclassesimportdataclass@dataclassclassA:
x: strclassB:
A=AmatchA("y"):
caseB.A(z):
# mypy: error: Cannot determine type of "z"# correctly prints: "x: y"print(f"x: {z}")
case A works fine, as does defining A directly inside B instead of setting A = 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:
@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?
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?
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: