pythonpython-typing

Type Hinting: Use of Elipsis


I'm trying to use Elipsis. Here's the declaration of the method

def func_test(
    self,
    top_k: int,
    tensor_1: torch.Tensor,
    tensor_2: torch.Tensor,
) -> list[list[int], ...]:

Pylance, in Visual Studio Code, is giving me the error

> "..." not allowed in this context

However, mypy docs state that it should be possible. The hack I used to solve this was to use the var Ellipsis, but I'm not sure whether this is the pythonic way.


Solution

  • You read into the documentation something it doesn't say. It only shows tuple, not any other generic type, using ....

    list takes exactly one argument: the element type.

    dict takes exactly two arguments: the key type and the value type.

    tuple can take an arbitrary number of elements, one type per element. A special case for the two-argument form is to supply ... for the second argument, which indicates a homogeneous tuple of arbitrary size, the type of each element specified by the first argument.