【发布时间】:2017-07-08 16:08:07
【问题描述】:
尝试使用 mypy 检查以下代码时:
import itertools
from typing import Sequence, Union, List
DigitsSequence = Union[str, Sequence[Union[str, int]]]
def normalize_input(digits: DigitsSequence) -> List[str]:
try:
new_digits = list(map(str, digits)) # <- Line 17
if not all(map(str.isdecimal, new_digits)):
raise TypeError
except TypeError:
print("Digits must be an iterable containing strings.")
return []
return new_digits
mypy 抛出以下错误:
calculate.py:17: 错误:无法推断“map”的类型参数 1
为什么会出现这个错误?我该如何解决?
谢谢:)
编辑:它实际上是 mypy 中的 bug,现在已修复。
【问题讨论】:
-
考虑到
stris basicallySequence[str],您可以将Union[str, Sequence[Union[str, int]]]重写为Sequence[Union[str, int]],然后错误消失。
标签: python python-3.x python-3.6 mypy type-annotation