-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Cannot infer type of generic attributes in match
statements when inheritance is involved
#13620
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
Comments
match
statements when inheritance is involvedmatch
statements when inheritance is involved
This is possibly a repro without pattern-matching: from typing import Generic, TypeVar
T = TypeVar("T")
class Foo(Generic[T]):
attr: T
def __init__(self, attr: T) -> None:
self.attr = attr
class Bar(Foo[T]): ...
x: Foo[str] = Bar("idk")
if isinstance(x, Bar):
reveal_type(x.attr) # Any (should be str!) https://mypy-play.net/?mypy=latest&python=3.10&gist=5ba0af842f3a9d75ab0498009e0d92cb |
I think it is something similar to #13607 🤔 |
There are couple of this with this example: from typing import Generic, TypeVar
T = TypeVar("T")
class Base(Generic[T]):
...
class A(Base[T]):
x: T
__match_args__ = ('x', )
def __init__(self, x: T):
self.x = x
a: Base[str] = A("foo")
reveal_type(a) # Base[str] (correct)
match a:
case A(b):
reveal_type(b) # Any (incorrect! Should be builtins.str) Right now it is unclear to me: how to properly handle |
@sobolevn my understanding is that it doesn't matter that ...but in the end the problem doesn't appear specific to The core problem is that |
Yeap, this is another problem :) |
Ok, the problem is in this line: Line 713 in 8147b0c
Ideally, we should use something like But, we need a completely new approach for this. It is not implemented at all. So, we take the subtype, go for all the route inside Then, we return the mapped instance and it will solve our problem. |
Ok, I found |
Bug Report
mypy
has trouble inferring the type of generic attributes inmatch
statements when inheritance is involved.This issue was encountered while investigating #13612
To Reproduce
Expected Behavior
The attribute on the last line above is revealed to be
str
Actual Behavior
It is revealed to be
Any
Your Environment
mypy-0.980+dev.b031f1c04e1ee4331e4d6957c7a9b727293328a9
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: