相关疑难解决方法(0)

即使测试了所有情况,MyPy 也会出现错误“缺少返回语句”

我收到一个 MyPy 错误“缺少返回语句”,即使我检查了函数内的所有可能情况。

例如,在下面的代码中,MyPy 仍然给我一个错误"9: error: Missing return statement",即使color只能是Color.RED, Color.GREEN, or Color.BLUE,我测试了所有这些情况!

class Color(enum.IntEnum):
    RED: int = 1
    GREEN: int = 2
    BLUE: int = 3


def test_enum(color: Color) -> str:
    if color == Color.RED:
        return "red"
    elif color == Color.GREEN:
        return "green"
    elif color == Color.BLUE:
        return "blue"
Run Code Online (Sandbox Code Playgroud)

python type-hinting mypy

6
推荐指数
1
解决办法
2009
查看次数

标签 统计

mypy ×1

python ×1

type-hinting ×1