我的函数看起来像这个简化的代码示例:
def my_func() -> dict:
result = {"success": False}
if condition:
result["success"] = True
return result
else:
result["message"] = "error message"
return result
Run Code Online (Sandbox Code Playgroud)
当我运行Mypy(版本0.52)时,我收到此错误:
error: Incompatible types in assignment (expression has type "str", target has type "bool")
Run Code Online (Sandbox Code Playgroud)
并且错误指向我的代码示例中的倒数第二行.为什么mypy会返回此错误?是我的代码无效(以任何方式)或这是一些mypy bug?
Mic*_*x2a 15
问题是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
Run Code Online (Sandbox Code Playgroud)
请注意,我们需要注释您的第二行,以便为mypy提供有关result应该是什么类型的提示,以帮助其推理过程.
如果您使用的是Python 3.6+,则可以使用以下备用语法对该行进行注释,该语法使用变量注释(从Python 3.6开始是新的):
result: Dict[str, Any] = {"success": False}
Run Code Online (Sandbox Code Playgroud)
选项2稍微更加类型安全 - 将您的值声明为strs或bool,但没有别的,使用Union.这不是完全类型安全的,但至少你仍然可以对你的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
Run Code Online (Sandbox Code Playgroud)
您可能会发现类型注释有点长/令人讨厌,因此您可以使用类型别名来提高可读性(并且可选地使用变量注释语法),如下所示:
ResultJson = Dict[str, Union[str, bool]]
def my_func(condition: bool) -> ResultJson
result: ResultJson = {"success": False}
# ...snip...
Run Code Online (Sandbox Code Playgroud)
选项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
Run Code Online (Sandbox Code Playgroud)
mypy_extensions如果要使用此选项,请务必安装该软件包.