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

Can't determine input argument type with structural pattern matching when class is set as attribute of another class #13455

Closed
jeremyn opened this issue Aug 19, 2022 · 5 comments
Labels
bug mypy got something wrong topic-type-alias TypeAlias and other type alias issues

Comments

@jeremyn
Copy link

jeremyn commented Aug 19, 2022

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:

from dataclasses import dataclass

@dataclass
class A:
    x: str

class B:
    A = A

match A("y"):
    case B.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

@jeremyn jeremyn added the bug mypy got something wrong label Aug 19, 2022
@AlexWaygood
Copy link
Member

If you declare B.A as an explicit type alias, everything works as expected:

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 AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2022
@AlexWaygood AlexWaygood added the topic-type-alias TypeAlias and other type alias issues label Aug 19, 2022
@jeremyn
Copy link
Author

jeremyn commented Aug 19, 2022

@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)

this error mode is probably quite rare and it is easy to work around anyway (just move the type alias to an outer scope).

but I'm not sure if he was talking about a workaround in the mypy code or a workaround for users like me.

@AlexWaygood AlexWaygood reopened this Aug 19, 2022
@AlexWaygood
Copy link
Member

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?

@jeremyn
Copy link
Author

jeremyn commented Aug 20, 2022

There are some other ways to go, sure. Thanks again for your help.

@jeremyn jeremyn closed this as completed Aug 20, 2022
@AlexWaygood
Copy link
Member

There are some other ways to go, sure. Thanks again for your help.

No worries. Sorry about the limitations here :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

No branches or pull requests

2 participants