patrick-kidger / torchtyping

Type annotations and dynamic checking for a tensor's shape, dtype, names, etc.
Apache License 2.0
1.41k stars 34 forks source link

Empty tensor support #28

Closed smorad closed 3 years ago

smorad commented 3 years ago

How can I type check an empty tensor? E.g.

import torch
from torchtyping import TensorType, patch_typeguard
from typeguard import typechecked
patch_typeguard()

a = torch.zeros(3,4,0)

def my_fn(a: TensorType["batch", "temporal", -1]):
  return torch.cat((a, torch.zeros(3,4,10), dim=-1)

Using None, -1, or 0 for the last dimension gives me

TypeError: argument "a" must be of type TensorType['batch', 'temporal', -1], got type NoneType instead.
smorad commented 3 years ago

Whoops my mistake, this is already handled correctly! Issue was in my code.