Skip to content

TypeVarTuple inference yields Never when splitting *Ts into (T, *Rest) with protocol #20771

@CaselIT

Description

@CaselIT

Bug Report

Mypy is not able to understand the type of the first argument of a typevartuple when using [T, *tuple[Any, ...]

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.14&gist=e4de7c80a62cdb96a7d6c34992b92a02

from typing import Any, Protocol, assert_type

class WithTuple[*Ts](Protocol):
    tup: tuple[*Ts]


def get_tuple[*Ts](e: WithTuple[*Ts]) -> tuple[*Ts]:
    return e.tup


def get_first[T](e: WithTuple[T, *tuple[Any, ...]]) -> T:
    return e.tup[0]


class X:
    tup: tuple[int, str]


assert_type(get_tuple(X()), tuple[int, str])
assert_type(get_first(X()), int)

Expected Behavior

No error, int is correctly inferred

Actual Behavior

main.py:20: error: Expression is of type "Never", not "int" [assert-type]
main.py:20: error: Argument 1 to "get_first" has incompatible type "X"; expected "WithTuple[Never, *tuple[Any, ...]]" [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.19.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.13-3.14

This may be releated to #16567 ?

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