python / python/mypy

TypeVarTuple instantiation with the empty tuple in Generic not being recognized as such

Open
#19,533 0 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 does not recognize that a TypeVarTuple was instantiated with the empty tuple in a generic.

To Reproduce

from typing import Generic, reveal_type, TypeVarTuple

Ts = TypeVarTuple('Ts')

class A(Generic[*Ts]):

    def args(self) -> tuple[*Ts]:
        raise NotImplementedError

class B(A[int, *Ts]):
    pass

class C(B[*tuple[()]]):
    pass

reveal_type(A.args)
reveal_type(B.args)
reveal_type(C.args)

Expected Behavior

I'd expect that the return type of C.args be tuple[int]. And this, indeed, is what pyright infers:

test.py:18:13 - information: Type of "A.args" is "(self: A[Unknown]) -> tuple[Unknown]"
test.py:19:13 - information: Type of "B.args" is "(self: B[Unknown]) -> tuple[int, Unknown]"
test.py:20:13 - information: Type of "C.args" is "(self: C) -> tuple[int]"

Actual Behavior

Mypy however infers tuple[int, *tuple[Any, ...]]:

test.py:18: note: Revealed type is "def [Ts] (self: test8.A[Unpack[Ts`1]]) -> tuple[Unpack[Ts`1]]"
test.py:19: note: Revealed type is "def [Ts] (self: test8.A[builtins.int, Unpack[Ts`1]]) -> tuple[builtins.int, Unpack[Ts`1]]"
test.py:20: note: Revealed type is "def (self: test8.A[builtins.int, Unpack[builtins.tuple[Any, ...]]]) -> tuple[builtins.int, Unpack[builtins.tuple[Any, ...]]]"

Your Environment

  • Mypy version used: 1.17.0 (compiled: yes)
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.13.5

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

Reproduce the example in test.py with mypy 1.17.0 and inspect the TypeVarTuple handling involved in Generic inheritance and empty-tuple instantiation. Add a regression test based on the example, with completion shown by C.args being revealed as returning tuple[int] rather than including an unconstrained tuple.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.