Skip to content

Crash with experimental TypeVarTuple support #15241

Closed
@erictraut

Description

@erictraut

I'm investigating a bug reported against pyright, and I was curious how mypy would handle the code. I tried it with mypy's experimental TypeVarTuple and Unpack support (--enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack) using mypy 1.3.0. The result was an "INTERNAL ERROR". That's not surprising given that the TypeVarTuple support is still under development. I'm reporting the crash in case those who are working on TypeVarTuple support in mypy find it useful.

This piece of code incorporates multiple variadic type variables, recursive function calls, tuple unpacking, nested calls that require bidirectional type inference, and higher-order functions. That makes it a potentially interesting test case.

Here's the code with the * replaced with Unpack (since mypy doesn't appear to support the new * syntax currently).

from typing import TypeVar, TypeVarTuple, Unpack, Callable

X = TypeVar("X")
Y = TypeVar("Y")
Xs = TypeVarTuple("Xs")
Ys = TypeVarTuple("Ys")

def nil() -> tuple[()]:
    return ()

def cons(
    f: Callable[[X], Y],
    g: Callable[[Unpack[Xs]], tuple[Unpack[Ys]]],
) -> Callable[[X, Unpack[Xs]], tuple[Y, Unpack[Ys]]]:
    def wrapped(x: X, *xs: Unpack[Xs]) -> tuple[Y, Unpack[Ys]]:
        y, ys = f(x), g(*xs)
        return y, *ys

    return wrapped

def star(f: Callable[[X], Y]) -> Callable[[Unpack[tuple[X, ...]]], tuple[Y, ...]]:
    def wrapped(*xs: X):
        if not xs:
            return nil()
        return cons(f, star(f))(*xs)

    return wrapped

Here's the stack trace for the crash:

Traceback (most recent call last):
  File "mypy/checkexpr.py", line 4890, in accept
  File "mypy/nodes.py", line 1889, in accept
  File "mypy/checkexpr.py", line 429, in visit_call_expr
  File "mypy/checkexpr.py", line 549, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 1209, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 1292, in check_call
  File "mypy/checkexpr.py", line 1483, in check_callable_call
  File "mypy/checkexpr.py", line 2101, in check_argument_types
AssertionError: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions