Mypy 错误 - 赋值中的类型不兼容

Mypy error - incompatible types in assignment

我的函数看起来像这个简化的代码示例:

def my_func() -> dict:
    result = {"success": False}

    if condition:
        result["success"] = True
        return result
    else:
        result["message"] = "error message"
    return result

当我 运行 Mypy(版本 0.52)时出现此错误:

error: Incompatible types in assignment (expression has type "str", target has type "bool")

错误指向我的代码示例中的倒数第二行。 为什么 mypy 返回此错误?我的代码是否无效(以任何方式)或者这是一些 mypy 错误?

问题是 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,但仅此而已。这不是完全类型安全的,但至少你仍然可以对你的字典进行一些检查。

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

您可能会发现类型注释有点难输入 long/annoying,因此您可以使用类型别名提高可读性(并可选择使用变量注释语法),如下所示:

ResultJson = Dict[str, Union[str, bool]]

def my_func(condition: bool) -> ResultJson
    result: ResultJson = {"success": False}
    # ...snip...

如果您同意放弃与旧版本 Python 的兼容性,您可以使用 Python 3.10 中引入的一些新语法稍微缩小别名:

# Python 3.10 lets you use 'dict' instead of Dict
ResultJson = dict[str, str | bool]

def my_func(condition: bool) -> ResultJson
    result: ResultJson = {"success": False}
    # ...snip...

选项 3 是最安全的类型:您可以使用 TypedDict:

将特定类型分配给字典中的不同字段
from typing import Optional, TypedDict

# You can also use the older:
# 
#     ResultJson = TypedDict("ResultJson", {...})
#
# ...syntax if you need to support versions of Python
# older then 3.6 or need to support keys that are not
# valid Python identifiers.
class ResultJson(TypedDict):
    success: bool
    message: Optional[str]

def my_func(condition: bool) -> ResultJson:
    result: ResultJson = {"success": False, "message": None}

    if condition:
        result["success"] = True
    else:
        result["message"] = "error message"
    return result

主要警告是 typed_dict 是在 Python 3.8 中添加的。如果您需要支持旧版本的 Python,请 pip-install typing_extensions 包并改用 from typing_extensions import TypedDict