Bug Report
Related to #13612.
#13618 fixed the error reported in #13612, so mypy correctly infers the type of the unpacked value.
However, mypy does not correctly infer the type parameter of the class containing the generic attribute within the scope of the match statement, despite correct inference outside the statement.
To Reproduce
Extending the reprex from #13612:
from typing import Generic, TypeVar
T = TypeVar("T")
class A(Generic[T]):
x: T
__match_args__ = ("x",)
def __init__(self, x: T):
self.x = x
a = A("foo")
reveal_type(a) # A[builtins.str] (correct)
reveal_type(a.x) # builtins.str (correct)
match a:
case A(x) as a_match:
reveal_type(x) # builtins.str (correct)
reveal_type(a_match) # A[Any] **(incorrect! should be A[str])**
Expected Behavior
Within the match statement, A(x) should be inferred to be of type A[str].
Revealed type is "test_generic.A[builtins.str]"
Revealed type is "builtins.str"
Revealed type is "builtins.str"
Revealed type is "test_generic.A[builtins.str]"
I have found that pyright, or at least the pylance VS code extension, infers this correctly. See below:
Actual Behavior
The final line does not reveal A[str].
Revealed type is "test_generic.A[builtins.str]"
Revealed type is "builtins.str"
Revealed type is "builtins.str"
Revealed type is "test_generic.A[Any]"
Your Environment
- Mypy version used: 1.14.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini (and other config files): none
- Python version used: 3.12.8
Motivation
This becomes relevant when trying to match on a union of generic types, as it creates an unreachable error.
e.g.
type Union_AB[T] = A[T] | B[T]
def func(val: Union_AB[int]) -> None:
match val:
case A(x):
pass
case B(x): # error: Subclass of "A[int]" and "B[Any]" cannot exist: would have incompatible method signatures [unreachable]
pass
Bug Report
Related to #13612.
#13618 fixed the error reported in #13612, so mypy correctly infers the type of the unpacked value.
However, mypy does not correctly infer the type parameter of the class containing the generic attribute within the scope of the
matchstatement, despite correct inference outside the statement.To Reproduce
Extending the reprex from #13612:
Expected Behavior
Within the
matchstatement,A(x)should be inferred to be of typeA[str].I have found that
pyright, or at least thepylanceVS code extension, infers this correctly. See below:Actual Behavior
The final line does not reveal
A[str].Your Environment
mypy.ini(and other config files): noneMotivation
This becomes relevant when trying to match on a union of generic types, as it creates an
unreachableerror.e.g.