如何在Python中正确调节Union类型?

时间:2019-03-28 06:27:50

标签: python mypy python-typing

我编写了一个python函数,用于处理int列表或int列表列表,即[1,2,3][[1,2],[3,4],如下所示:

from typing import Sequence, Union
IntSeq = Sequence[int]

def foo(a: Union[IntSeq, Sequence[IntSeq]]):
    if isinstance(a, Sequence) and isinstance(a[0], int):
        # here type of a equals IntSeq
        b: IntSeq = a

但是,mypy告诉我以下错误:

$ mypy bar.py
test.py:6: error: Incompatible types in assignment (expression has type "Union[Sequence[int], Sequence[Sequence[int]]]", variable has type "Sequence[int]")

如何消除此错误?

0 个答案:

没有答案