Type Hinting torch.float 用于特殊形式的类型提示
pytorch 459
原文标题 :Type Hinting torch.float for special form type hint
我有一个函数可以接受包括类型torch.float
在内的类型typing.Union
。但是如果我使用带有torch.float
的typing.Union
类型作为参数,我会收到一个错误。这是一个例子:
from typing import Union
import torch
def fct(my_float_or_tensor: Union[torch.float, torch.Tensor]):
pass
我得到了错误
TypeError: Union[t0, t1, ...]: each t must be a type. Got torch.float32.
我究竟做错了什么?
有趣的是,特殊类型typing.Tuple
也会出现同样的问题,但如果我在类型提示时直接使用torch.float
则不会。