跳到内容

invalid-hash-return-type (PLE0309)

源自 Pylint 代码检查工具。

作用

检查返回非整数值的 __hash__ 实现。

为什么这不好?

__hash__ 方法应该返回一个整数。返回不同的类型可能会导致意外行为。

注意:boolint 的子类,所以从技术上讲,__hash__ 返回 TrueFalse 是有效的。但是,为了与其他规则保持一致,当 __hash__ 返回 bool 时,Ruff 仍然会发出诊断信息。

示例

class Foo:
    def __hash__(self):
        return "2"

建议改为

class Foo:
    def __hash__(self):
        return 2

参考