且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MyPy 给出错误“缺少返回语句";即使所有案例都经过测试

更新时间:2022-05-02 09:09:01

这个问题确实没有问题 - mypy 目前确实如此.枚举支持是初步的,有点临时.您正在寻找的检查类型可能会在未来实施.

There really is no question in this question - mypy indeed behaves this way at the moment. The enum support is baked in, and is preliminary and somewhat ad-hoc. The kind of checking you are looking for might be implemented in the future.

然而,这段代码很脆弱;如果颜色会改变,它会无声地破碎.请记住,Python 不是编译语言 - 类型检查器传递是可选的,其他人可能不会使用它.

However, this code is fragile; if Color will change, it will silently break. Remember that Python is not a compiled language - the typechecker pass is optional, and someone else might not use it.

IMO 的正确方法是在末尾添加 assert False.这也会使 mypy 静音.

The right way IMO is to add assert False at the end. This will also silence mypy.