Unpacked TypeVarTuple as a function argument is not compatible with empty tuple as its default
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy complains when default an unpacked TypeVarTuple (tuple[*Ts]) to an empty tuple, but accepts one as input, which seems a conflicting behaviour at least.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=9c2fa71781c45ed24b1901b8e9252e64
from collections.abc import Callable
from typing import TypeVarTuple
Ts = TypeVarTuple("Ts")
def foo1(func: Callable[[*Ts], None], args: tuple[*Ts] = ()) -> None: # error: Incompatible default for argument "args" (default has type "tuple[()]", argument has type "tuple[*Ts]") [assignment]
func(*args)
def foo2(func: Callable[[*Ts], None], args: tuple[*Ts]) -> None:
func(*args)
def bar() -> None: ...
foo2(bar, ()) # ok
Expected Behavior
args: tuple[*Ts] can have an empty tuple as default.
Actual Behavior
args: tuple[*Ts] cannot default to an empty tuple.
Your Environment
- Mypy version used: 1.9.0
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.11
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked mypy-play reproducer and the foo1/foo2 example using an unpacked TypeVarTuple. Trace the type-checking behavior for the empty default versus the accepted empty-tuple argument; done means foo1 accepts the default without an incompatible-default diagnostic while foo2 remains valid.
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
- 42/100