将 comments 扩大到OP:
Mypy的旧版本存在某种与此相关的问题/错误,导致在项目的GitHub:Mypy disallows overriding an attribute with a property上进行了一些讨论,该问题本应在版本>;=v0.990上得到修复
还有一个让人感觉更接近当前的OP展会的讨论:overriding variable with property: error Signature incompatible with supertype
在第二种情况下,发生的情况是...
class Foo:
bar: str
...告诉Mypy .bar
将是一个可写属性,而只是在子类Bat
中声明@property
...
class Bat(Foo):
@property
def bar(self) -> str:
...会将该属性设置为只读.在本例中,最直接的解决方法可能是为.bar
创建一个setter.
以下代码:
class Foo:
bar: str
class Bat(Foo):
@property
def bar(self) -> str:
return "something"
@bar.setter
def bar(self, value: str):
# ... actual setter code probably
# for self._bar or something like that
pass
产生:
Success: no issues found in 1 source file