我编写了一个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]")
如何消除此错误?