我试图让 mypy 对我的类型注释感到满意。这是最小的例子:
class FooInterface:
x: int
class FooWithAttribute(FooInterface):
x: int = 0
class FooWithProperty(FooInterface):
@property
def x(self) -> int:
return 0
Run Code Online (Sandbox Code Playgroud)
为了我的人的理解,一切都很好:既有FooWithAttribute().x和FooWithProperty().x将返回0是int,没有类型的错误。然而 mypy 抱怨:
error: Signature of "x" incompatible with supertype "FooInterface"
有没有办法告诉mypy一切正常?现在我发现唯一的办法就是标注x: typing.Any在FooInterface其废物x是INT的信息。