Skip to content

Tuple size mismatch for TypeVarTuple and default arguments with TypeVars #7146

@cdce8p

Description

@cdce8p

Describe the bug
There seems to be an unaccounted edge case for capturing callable with default args with TypeVarTuples if both the function and the caller use TypeVars.

Similar to #3775 which was implemented in #6613.

  test.py:23:5 - error: Argument of type "*tuple[T@caller, T@caller]" cannot be assigned to parameter "args" of type "*Ts@run_callback" in function "run_callback"
    Type "*tuple[T@caller, T@caller]" cannot be assigned to type "*tuple[T@func, T@func, int | None]"
      "*tuple[T@caller, T@caller]" is incompatible with "*tuple[T@func, T@func, int | None]"
        Tuple size mismatch; expected 3 but received 2 (reportArgumentType)

Code or Screenshots

from collections.abc import Callable
from typing import TypeVar, TypeVarTuple

T = TypeVar("T")
Ts = TypeVarTuple("Ts")

def func(
    x: T,
    y: T,
    z: int | None = None,
) -> None: ...

def run_callback(callback: Callable[[*Ts], None], *args: *Ts) -> None:
    # ...
    callback(*args)


# ---
def caller(
    x: T,
    y: T,
) -> None:
    run_callback(func, x, y)  # Error: Tuple size mismatch

This should be fine. The error disappears if T is replaced with e.g. int.

VS Code extension or command-line
pyright 1.1.348

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions