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

Cannot infer generic parameter types within match statements #18432

Open
msto opened this issue Jan 9, 2025 · 1 comment
Open

Cannot infer generic parameter types within match statements #18432

msto opened this issue Jan 9, 2025 · 1 comment
Labels
bug mypy got something wrong

Comments

@msto
Copy link

msto commented Jan 9, 2025

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:

Screenshot 2025-01-09 at 4 25 24 PM

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
@msto msto added the bug mypy got something wrong label Jan 9, 2025
@sterliakov
Copy link
Contributor

That happens here and is not caused by some weird type erasure:

mypy/mypy/checkpattern.py

Lines 546 to 556 in 1affabe

if isinstance(type_info, TypeInfo):
any_type = AnyType(TypeOfAny.implementation_artifact)
args: list[Type] = []
for tv in type_info.defn.type_vars:
if isinstance(tv, TypeVarTupleType):
args.append(
UnpackType(self.chk.named_generic_type("builtins.tuple", [any_type]))
)
else:
args.append(any_type)
typ: Type = Instance(type_info, args)

When we encounter a class pattern, there is no relationship between its constructor (the primary way to infer type args as of now) and the pattern, so the best we can do is to fill all typevars with any.

Solving all typevars may be impossible at all: what if one of generics defines a type of some attribute not listed in __match_args__ and not captured as a kwarg? So defaulting all of them to Any is reasonable.

If a pattern matches, we may want to use that information to resolve some of generics. I don't know how complicated that would be since it's similar to regular inference, but can't rely on __init__ callable.

However, we are already able to do similar backpropagation of generics to solve generic protocols. So it may be feasible to do something similar here: collect all constraints from the pattern, map them to type variables, solve the resulting system, and apply the result to the typeinfo falling back to Any for unresolved vars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants