相关疑难解决方法(0)

如何注释可以实现为属性的属性?

我试图让 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().xFooWithProperty().x将返回0int,没有类型的错误。然而 mypy 抱怨:

error: Signature of "x" incompatible with supertype "FooInterface"

有没有办法告诉mypy一切正常?现在我发现唯一的办法就是标注x: typing.AnyFooInterface其废物x是INT的信息。

python properties typing mypy

5
推荐指数
1
解决办法
980
查看次数

标签 统计

mypy ×1

properties ×1

python ×1

typing ×1