【发布时间】:2021-02-25 23:51:55
【问题描述】:
如何在 python 中验证方法的返回类型?
假设myfunc 返回一个元组列表,我想要这样的东西:
def myfunc() :
return [(1,2), (3,4)]
我尝试使用typing、TypeVal
from typing import TypeVar, Type
def myfunc() -> list[tuple]:
...
或类似的东西:
list_tup = list[tuple]
def myfunc(t: Type[T]) -> list_tup :
...
回溯(最近一次调用最后一次):文件“”,第 1 行,在 TypeError 中:'type' 对象不可下标
什么是正确的语法或如何验证自定义返回类型?
【问题讨论】:
标签: python validation return-type typing