【发布时间】:2020-08-05 17:15:10
【问题描述】:
给定以下代码:
from typing import Optional, Dict
def foo(b: bool) -> Optional[Dict]:
return {} if b else None
def bar() -> None:
d = foo(False)
if not d:
return
filter(lambda x: d['k'], [])
mypy 0.770 在bar 的最后一行出现以下错误:Value of type "Optional[Dict[Any, Any]]" is not indexable。 map 也是如此。从 pydash 更改行以使用列表理解或 filter_ 或 map_ 可解决错误。
为什么 mypy 在使用标准 filter 时会抛出错误,即使有类型保护?
【问题讨论】:
标签: python python-3.x type-hinting mypy