-2

我有带有这些类型注释的 python 函数

def func_1() -> Optional[Sequence[str, List[str], str]]:
    # do stuff

def func_2(a: str, b: List[str], c: str) -> None:
    # do other stuff

为什么下面的代码在我调用 func_2 的那一行给出警告“预期类型列表 [str,Any]' 得到 'str'”?

result = func_1()
func_2(result[0], result[1], result[2])

这是否意味着我的类型检查器有问题,或者我的代码有问题?

4

1 回答 1

1

Sequence并不意味着与这样的多种类型一起使用。Sequence并且List并不意味着具有结构(意思是,元素 1 总是int,元素 2 总是str并且总是正好长度 2)。

您应该改用Tuple.

另见:https ://stackoverflow.com/a/40181387/4597523

于 2021-03-15T20:58:19.017 回答