【发布时间】:2016-09-30 03:46:44
【问题描述】:
我正在使用 Python 3.5.1 和新发布的MyPy v0.4.1 静态类型分析器。
我有一些更复杂的代码,我已将其简化为重现错误所需的最简单的 Python 类:
class MyObject(object):
def __init__(self, value: int=5) -> None:
self.value = value
def __eq__(self, other: MyObject) -> bool:
return self.value == other.value
运行类型检查器mypy test.py 会产生以下错误:
test.py: note: In class "MyObject":
test.py:5: error: Argument 1 of "__eq__" incompatible with supertype "object"
我基于these docs 的理论是对象上的__eq__ 和__ne__ 已经定义了类型,这与我的子类对这些类型的重新定义相冲突。我的问题是如何定义这些类型以确保 __eq__ 使用我选择的类型进行类型检查。
【问题讨论】:
标签: python python-3.x types typechecking mypy