当类型检查元组解包时,我得到这个程序的幽门错误(基本设置)。其思想是一个元组可以有2个或3个特定类型的元素。
# pylance typechecking "basic"
from typing import Tuple, Union
TT = Union[Tuple[str,str,float], Tuple[str,str]]
def f(v: TT):
if len(v) == 3:
a,b,c = v # pylance reportGeneralTypeIssues: Tuple size mismatch: expected 3 but received 2
elif len(v) == 2:
a,b = v # pylance reportGeneralTypeIssues: Tuple size mismatch: expected 2 but received 3我应该怎么做才能让校验员相信这是正确的(没有#type ignore)?
发布于 2021-05-18 21:55:29
我相信通过使用TypeGuards,Python3.10将会提供解决方案。参见PEP647,其中甚至包括一个元组中元素数量的示例。
https://stackoverflow.com/questions/66242131
复制相似问题