pyright icon indicating copy to clipboard operation
pyright copied to clipboard

invocation of (Iterable[T], Iterable[T]) -> Iterable[Tuple[T, T]] by Tuple[*Ts] explodes Ts to T

Open pinterior opened this issue 4 years ago • 2 comments

Describe the bug

kind of #4186, but not using standard library. (not a typeshed bug) invocation of (Iterable[T], Iterable[T]) -> Iterable[Tuple[T, T]] by Tuple[*Ts]s unexpectedly explodes Ts to T.

even if current type system is not enough to annotate programs like below, number of type parameter for i shoule be two.

To Reproduce

#!/usr/bin/env python3

from typing import Any, Iterable, Tuple, TypeVar
from typing_extensions import reveal_type, TypeVarTuple

T = TypeVar("T")
Ts = TypeVarTuple("Ts")

def f(a: Iterable[T], b: Iterable[T]):
   i = a.__iter__()
   j = b.__iter__()
   while True:
      try:
         yield (i.__next__(), j.__next__())
      except StopIteration:
         break

def g(a: "Tuple[*Ts]", b: "Tuple[*Ts]"):
   for i in f(a, b):
      yield i

reveal_type(f)
reveal_type(g)

for i in g((1, "foo"), (2, "bar")):
   reveal_type(i)
   x: Tuple[Any, Any] = i
   print(x)
information: Type of "f" is "(a: Iterable[T@f], b: Iterable[T@f]) -> Generator[tuple[T@f, T@f], None, None]"
information: Type of "g" is "(a: Tuple[*Ts@g], b: Tuple[*Ts@g]) -> Generator[tuple[*Ts@g, *Ts@g], None, None]"
information: Type of "i" is "tuple[int, str, int, str]"
error: Expression of type "tuple[int, str, int, str]" cannot be assigned to declared type "Tuple[Any, Any]"
    "tuple[int, str, int, str]" is incompatible with "Tuple[Any, Any]"
      Element size mismatch; expected 2 but received 4 (reportGeneralTypeIssues)
Runtime type is 'function'
Runtime type is 'function'
Runtime type is 'tuple'
(1, 2)
Runtime type is 'tuple'
('foo', 'bar')

Expected behavior

infer tuple of two types for i and no error reported

VS Code extension or command-line

command-line tool, 1.1.279

pinterior avatar Nov 13 '22 08:11 pinterior

This will be fixed in the next release.

erictraut avatar Nov 13 '22 18:11 erictraut

This is addressed in pyright 1.1.280, which I just published. It will also be included in a future version of pylance.

erictraut avatar Nov 16 '22 03:11 erictraut