我想要一个只写属性,当有人更改它时通知我。另外,我必须重新分配名称(此处
var
),以免在类的命名空间中出现额外的名称。这违反了 Mypy 规则。
这是简化版本:
class A:
def var(self, value):
print("value changed:", value)
var = property(fset=var)
obj = A()
obj.var = 10
obj.var = "hi"
Mypy 错误消息:
main.py:5: error: Incompatible types in assignment (expression has type "property", variable has type "Callable[[A, Any], Any]")
main.py:9: error: Cannot assign to a method
main.py:9: error: Incompatible types in assignment (expression has type "int", variable has type "Callable[[Any], Any]")
main.py:10: error: Cannot assign to a method
main.py:10: error: Incompatible types in assignment (expression has type "str", variable has type "Callable[[Any], Any]")
Found 5 errors in 1 file (checked 1 source file)
如何才能让 Mypy 满意而不用
# type: ignore
抑制错误?
我目前解决这个问题的尝试是:
class A:
def temp(self, value):
print("value changed:", value)
var = property(fset=temp)
del temp
obj = A()
obj.var = 10
obj.var = "hi"
还有什么更好的办法吗?
似乎没有其他办法,Mypy 不允许我在类主体中重新定义名称。类变量可以,但方法不行。
虽然我可以使用问题中展示的临时方法,但我更喜欢使用带有 getter 和 setter 的
@property
装饰器定义属性,并手动引发 AttributeError
:
class A:
@property
def var(self):
raise AttributeError("unreadable attribute 'var'")
@var.setter
def var(self, value):
print("value changed:", value)