python / python/mypy

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

Open
#20,771 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-pep-646
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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 ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in the linked mypy-play case, then compare the behavior with issue #16567. Trace TypeVarTuple inference when a protocol is matched against a split tuple; done means the example infers int for get_first(X()) and produces no errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.