Skip to content

Mypy fails to solve tuple[int, *Ts, int] as an Iterable[T]. #19659

@randolf-scholz

Description

@randolf-scholz
from typing import Iterable

def upcast[T](arg: Iterable[T]) -> Iterable[T]: return arg

def f[*Ts](padded_tuple: tuple[int, *Ts, int]) -> None:
    as_iterable = upcast(padded_tuple)  # Argument 1 to "upcast" has incompatible type
    reveal_type(as_iterable)  # "typing.Iterable[builtins.int]" ❌

https://mypy-play.net/?mypy=latest&python=3.12&enable-incomplete-feature=PreciseTupleTypes&gist=ed0d06b3234441ebf6f31b98224618fc

For comparison, pyright predicts Code sample in pyright playground Iterable[int | Union[*Ts@demo]].

Since Union of TypeVarTuple is not specced yet, mypy should nevertheless probably fall back to something like Iterable[object] or Iterable[Any], but Iterable[int] is just wrong.

If the tuple is not padded, then mypy still falsely issues [arg-type], but at least the reveal_type is Iterable[Any]. https://mypy-play.net/?mypy=latest&python=3.12&enable-incomplete-feature=PreciseTupleTypes&gist=b3429973142b9a329d0fcc1dda8aabb2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-646PEP 646 (TypeVarTuple, Unpack)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions