将值列表映射到浮点时出现 mypy 错误

mypy error when mapping a list of values to float

我正在尝试使用 mypy 评估以下语句:

a = [1,2,3]

b = map(float, a)

这个returns错误

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)
a = [1,2,3]

b = map(lambda x: float(x), a)

这个问题与对结果变量和应用 map 的变量使用相同的名称有关,我正在按照

的方式做一些事情
a = b.split()
a = list(map(lambda x: float(x),a))

导致错误。在这里更改最后一行中的变量名称解决了这个问题。