我正在尝试使用 mypy 评估以下语句:
a = [1,2,3]
b = map(float, a)
这会返回错误
Argument 1 to "map" has incompatible type "Type[float]"; expected "Callable[[str], str]"
但是在运行时执行没有问题。出现这个问题的原因是什么?
最佳答案
我也无法复制此内容(Python 版本 3.9.0、mypy 版本 0.910),因此首先要尝试的就是升级到最新版本。
不完全相同的问题,但 mypy github 上的这些问题可能是一个有趣的引用:
https://github.com/python/mypy/issues/6697 (开放)
https://github.com/python/mypy/issues/1855 (已关闭)
基于这些,看起来以下内容可能会在没有 mypy 错误的情况下通过:
b = map(lambda x: float(x), a)
关于python - 将值列表映射到 float 时 mypy 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70213133/