mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Unpacked TypeVarTuple as a function argument is not compatible with empty tuple as its default

Open airallergy opened this issue 1 year ago • 0 comments

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

airallergy avatar Apr 10 '24 18:04 airallergy