问题在于 mypy 推断出您的 result 变量的类型是 Dict[str, bool],这是由于您在第 2 行首次对其进行初始化的方式。
因此,当您稍后尝试插入 str 时,mypy(理所当然地)抱怨。您有几个选项可以修复您的代码,我将按照类型安全从低到高的顺序列出这些选项。
选项 1 是声明您的字典,使其值的类型为 Any - 也就是说,您的值根本不会进行类型检查:
from typing import Any, Dict
def my_func(condition: bool) -> Dict[str, Any]:
result = {"success": False} # type: Dict[str, Any]
if condition:
result["success"] = True
else:
result["message"] = "error message"
return result
请注意,我们需要对您的第二行进行注释,以便向 mypy 提示 result 的类型应该是什么,以帮助其推理过程。
如果您使用的是 Python 3.6+,则可以使用以下替代语法来注释该行,该语法使用变量注释(自 Python 3.6 起新增):
result: Dict[str, Any] = {"success": False}
选项 2 的类型安全性稍高一些——使用 Union 将值声明为 strs 或 bools,但仅此而已。这不是完全类型安全的,但至少您仍然可以对您的 dict 进行 一些 检查。
from typing import Any, Dict
def my_func(condition: bool) -> Dict[str, Union[str, bool]]:
result = {"success": False} # type: Dict[str, Union[str, bool]]
if condition:
result["success"] = True
else:
result["message"] = "error message"
return result
您可能会发现类型注释有点长/令人讨厌,因此您可以使用类型别名来提高可读性(并且可以选择使用变量注释语法),如下所示:
ResultJson = Dict[str, Union[str, bool]]
def my_func(condition: bool) -> ResultJson
result: ResultJson = {"success": False}
# ...snip...
选项 3 是最安全的,尽管它确实要求您使用实验性的 'TypedDict' 类型,它允许您将特定类型分配给 dict 中的不同字段。也就是说,使用这种类型需要您自担风险——AFAIK 它尚未添加到 PEP 484,这意味着其他类型检查工具(如 Pycharm 的检查器)没有义务理解这一点。 Mypy 本身最近才添加了对 TypedDict 的支持,因此可能仍然存在问题:
from typing import Optional
from mypy_extensions import TypedDict
ResultJson = TypedDict('ReturnJson', {'success': bool, 'message': Optional[str]})
def my_func(condition: bool) -> ResultJson:
result = {"success": False, "message": None} # type: ResultJson
if condition:
result["success"] = True
else:
result["message"] = "error message"
return result
如果您想使用此选项,请务必安装 mypy_extensions 软件包。