Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

python - Mypy error - incompatible types in assignment

My function looks like this simplified code sample:

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

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

When I run Mypy (version 0.52) I get this error:

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

and the error is pointing to the second last line in my code sample. Why mypy is returning this error? is my code invalid (in any way) or is this some mypy bug?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is that mypy inferred that the type of your result variable is Dict[str, bool] due to how you first initialized it on line 2.

Consequently, when you try and insert a str later, mypy (rightfully) complains. You have several options for fixing your code, which I'll list in order of least to most type-safe.

Option 1 is to declare your dictionary such that its values are of type Any -- that is, your values will not be type-checked at all:

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

Note that we needed to annotate your second line to give mypy a hint about what the type of result should be, to help its inference process.

If you're using Python 3.6+, you can annotate that line using the following alternate syntax, which uses variable annotations (which are new as of Python 3.6):

result: Dict[str, Any] = {"success": False}

Option 2 is slightly more type-safe -- declare your values to be either strs or bools, but nothing else, using Union. This isn't fully typesafe, but at least you can still have some checks on your 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

You may perhaps find that type annotation to be a little long/annoying to type, so you could use type aliases for readability (and optionally use the variable annotation syntax), like so:

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

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

Option 3 is the most type-safe, although it does require you to use the experimental 'TypedDict' type, which lets you assign specific types to different fields in your dict. That said, use this type at your own risk -- AFAIK it hasn't yet been added to PEP 484, which means other type-checking tools (like Pycharm's checker) aren't obligated to understand this. Mypy itself only recently added support for TypedDict, and so may still be buggy:

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

Be sure to install the mypy_extensions package if you want to use this option.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...