0

Having

# python3.9
TypeA = tuple[int, float, int, str]
TypeB = tuple[int, str, set]
T = TypeVar("T", TypeA, TypeB)


def f(x: int) -> int:
    return x * x


def yielding_function(payload: Iterable[T]) -> Iterator[T]:
    for x0, *other in payload:
        yield f(x0), *other

causes PyLance type error

Expression of type "tuple[int, *tuple[float* | int* | str* | set[Unknown]*, ...]]" cannot be assigned to yield type "T@yielding_function"
  Type "tuple[int, *tuple[float* | int* | str* | set[Unknown]*, ...]]" cannot be assigned to type "T@yielding_function"

What is the correct type hint for yielding_function? Or what is robust way of defining thereof? I wish that input and output would have the same type, so using unions is not possible here.

vahvero
  • 525
  • 11
  • 24
  • You need `TypeVarTuple` ([PEP-646](https://peps.python.org/pep-0646/)). [Not supported by mypy](https://github.com/python/mypy/issues/12280), don't know about py;ance support. – STerliakov Sep 18 '22 at 11:51
  • I gather then that this is not possible to annotate in python3.9? I'd need to upgrade to 3.11. – vahvero Sep 19 '22 at 07:58

0 Answers0