Python2 TorchScript type annotation tuple vs Tuple

I have a torch script that accepts a tuple[Tensor],

@torch.jit.script
def smt_pred(confs, child, child_sizes, threshold, obj,
             height, width, a):
    # type: (Tuple[Tensor],Tuple[Tensor],Tuple[Tensor],Tensor,Tensor,Tensor,Tensor,Tensor) -> Tuple[Tensor,Tensor]

When I call the code in Python2 I get the error

RuntimeError: smt_pred() Expected a value of type ‘Tuple[Tensor]’ for argument ‘confs’ but instead found type ‘tuple’.

tuple is same as Tuple and I could not find how to annotate confs to be tuple of tensors. Is it possible in Python2?


Changing Tuple to List works in Python2, so this is only about Tuple

Added an issue here because List works (and I just have to pass list(confs) as a workaround)

Copied from the issue:

I’m guessing that you’re passing in a tuple that doesn’t look like (torch.ones(2, 2),). For a Tuple the type needs to be fixed and completely specified and the elements can be different types. In TorchScript, a, b, and c below are all different types

# type is `Tuple[int, int, int]
a = (1, 2, 3)

# type is `Tuple[str, int, int]
b = ('hi', 2, 3)

# type is `Tuple[int]`
c = (2,)

For lists, the element types must all be the same, which is why it can be any length but you only need to specify List[Tensor] or List[int]. You can read more about it here.