Skip to content

Cannot infer type of generic type parm in match case statement #18470

Open
@pbabics

Description

Bug Report
Hello,

I've been trying to create a Generic function with a match statement, as seen below, but I am getting errors about expected_result_type not being a type, although from the typing, it most certainly is.

This might be related to #18432

To Reproduce

from collections.abc import Callable
from typing import Any


def print_test[T](fn: Callable[[], Any], expected_result_type: type[T]) -> T:
    value = fn()

    match value:
        case expected_result_type():
            return value
        case str():
            print("Got ", value)
        case _:
            print("Unexpected result ", value)


print_test(lambda: 4, float)

Expected Behavior

No errors

Actual Behavior

test.py:9: error: Expected type in class pattern; found "type[T`-1]"  [misc]
test.py:10: error: Statement is unreachable  [unreachable]

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.13.0

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions