为什么此代码会生成类型注释警告?
Why does this code generate a Type Annotation Warning?
我有 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?
的那一行给出警告“Expected type list[str, Any]' got 'str' instead”
result = func_1()
func_2(result[0], result[1], result[2])
这是否意味着我的类型检查器有问题,或者我的代码有问题?
Sequence
不适用于像这样的多种类型。 Sequence
和 List
并不意味着具有结构(意思是,元素 1 总是 int
,元素 2 总是 str
并且总是正好是长度 2)。
您应该改用 Tuple
。
另见:
我有 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?
的那一行给出警告“Expected type list[str, Any]' got 'str' instead”result = func_1()
func_2(result[0], result[1], result[2])
这是否意味着我的类型检查器有问题,或者我的代码有问题?
Sequence
不适用于像这样的多种类型。 Sequence
和 List
并不意味着具有结构(意思是,元素 1 总是 int
,元素 2 总是 str
并且总是正好是长度 2)。
您应该改用 Tuple
。
另见: