如何使用类型提示来注释仅写的属性

发布于 2025-02-02 15:36:03 字数 1220 浏览 4 评论 0原文

我想拥有一个仅写作的属性,当有人更改它时会通知我。另外,我不得不重新分配名称(此处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"

有什么更好的方法吗?

I would like to have a write-only property that notifies me when somebody changes it. Also I had to re-assign the name(here var) in order not to have an extra name in class's namespace. This goes against Mypy rules.

Here is the simplified version:

class A:
    def var(self, value):
        print("value changed:", value)

    var = property(fset=var)


obj = A()
obj.var = 10
obj.var = "hi"

Mypy error messages:

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)

How can I make Mypy happy without suppressing the error with # type: ignore?

My current attempt to solve it is:

class A:
    def temp(self, value):
        print("value changed:", value)

    var = property(fset=temp)
    del temp


obj = A()
obj.var = 10
obj.var = "hi"

Is there any better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2025-02-09 15:36:03

似乎没有其他方法,Mypy不允许我在班级主体中重新定义名称。可以使用类变量,但没有方法。

尽管我可以使用问题中显示的临时方法,但我更喜欢使用@property使用getter和setter定义属性,然后手动升高attributeError

class A:
    @property
    def var(self):
        raise AttributeError("unreadable attribute 'var'")

    @var.setter
    def var(self, value):
        print("value changed:", value)

Seems like there is no other way, Mypy doesn't let me re-define names in the class body. It's OK with class variables but not with methods.

Although I could use that temporary method that I showed in the question, I prefer defining a property using @property decorator with both getter and setter and manually raising AttributeError:

class A:
    @property
    def var(self):
        raise AttributeError("unreadable attribute 'var'")

    @var.setter
    def var(self, value):
        print("value changed:", value)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文