pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Add TypeVarTuple support for pattern matching with "*"

Open minmax opened this issue 7 months ago • 1 comments

pyright 1.1.371

from typing import TypeVar, TypeVarTuple, reveal_type

T = TypeVar("T")
TT = TypeVarTuple("TT")

def do(tup: tuple[T, *TT]) -> tuple[T, *TT]:
    match tup:
        case (first, *last_n):
            reveal_type(last_n)  # Type of "last_n" is "list[*TT@do]"

            return (first, *last_n) # Expression of type "tuple[T@do, *tuple[object, ...]]" is incompatible with return type "tuple[T@do, *TT@do]"
            # "tuple[T@do, *tuple[object, ...]]" is incompatible with "tuple[T@do, *TT@do]"
            # Tuple entry 2 is incorrect type
            # Type "*tuple[object, ...]" is incompatible with type "*TT@do" (reportReturnType)

Type list[*TT@do] of last_n variable looks weird to me, because of usage TypeVarTuple + list.

Also maybe is a way to "fix" error on return? But it looks like nothing better than tuple[T@do, *tuple[object, ...]] can be offered.

minmax avatar Jul 10 '24 09:07 minmax