Python: 如何覆盖子类中实例属性的类型提示?

Python: how to override type hint on an instance attribute in a subclass?

在您深入之前,我的问题是:如何在子class 中使用类型提示来指定实例属性的不同类型?

如果您不清楚这意味着什么,请阅读下文,我在其中绘制了一个示例来澄清事情。


完整说明

我有一个摘要 class Foo,还有一个名为 SubclassOfFooFoo 的子 class。

Foo 有一个抽象方法 get_something return 是一个 Something.

类型的对象

Something 有一个名为 SubclassOfSomething 的子 class。 SubclassOfSomething 有一个额外的方法 something_special.

SubclassOfFooget_something 覆盖为 return 类型 SubclassOfSomething 的对象。然后,SubclassOfFoo 尝试使用 SubclassOfSomething 的方法 something_special

但是,目前我的 PyCharm 检查正在报告 Unresolved attribute reference 'something_special' for class 'Something'。我正在尝试找出解决此问题的正确方法。

这一切都非常令人困惑,所以我制作了一个不错的小代码片段来帮助解决这个问题:

from abc import ABC, abstractmethod


class Something:
    def __init__(self):
        self.attr = 0


class SubclassOfSomething(Something):
    def __init__(self):
        Something.__init__(self)

    def something_special(self):
        self.attr = 1


class Foo(ABC):
    def __init__(self):
        self.my_class = self.get_something()

    @abstractmethod
    def get_something(self) -> Something:
        pass


class SubclassOfFoo(Foo):
    def __init__(self):
        Foo.__init__(self)

    def get_something(self) -> SubclassOfSomething:
        return SubclassOfSomething()

    def do_something_special(self):
        self.my_class.something_special()

基本上,为了让一切顺利进行,我可以做以下几件事之一:

  1. 删除 Foo
  2. get_something 的 return 的类型提示
  3. SubclassOfFoo 中使用类型提示 self.my_class 来清理
  4. 使用泛型?

选项 1. 是我要避免的

方案二不错,就是想不通

选项 3. 也是一个选项。

我也对其他选择持开放态度,因为我确信有更好的方法。

你能帮我想出正确的处理方法吗?


我试过的

为了模拟选项 2,我尝试按照此处的建议使用 typing.Type

但是,这对我不起作用。

您也可以在 Something 上提供 something_special 方法,并提出 NotImplementedError

class Something:
    def __init__(self):
        self.attr = 0

    def something_special(self):
        raise NotImplementedError()

这解决了你的类型提示问题,虽然在功能上它会在同一点引发异常(如果你设法以某种方式获得 Something 并尝试调用 something_special,只是 NotImplementedError 而不是 AttributeError).

也许在某些情况下,您可能只想 pass,这取决于 something_special 的实际含义。

class Something:
    def __init__(self):
        self.attr = 0

    def validate(self):
        # doesn't want to perform validation
        pass


class SubclassOfSomething(Something):
    def __init__(self):
        Something.__init__(self)

    def validate(self):
        if self.attr < 0:
            raise ValueError()

重要的基础是确保你的 class 层次结构符合通用接口 - public 方法在子 class 上而不是在父级上违背了这一点并减少了多态性class 层次结构中的对象数。

您可以在 class 定义的开头给出 my_class 属性的类型提示:

class SubclassOfFoo(Foo):
    my_class: SubclassOfSomething  # <- here

    def get_something(self) -> SubclassOfSomething:
        return SubclassOfSomething()

    def do_something_special(self):
        self.my_class.something_special()

在那之后 PyCharm 检查中没有警告 Unresolved attribute reference 'something_special' for class 'Something' 因为现在 my_class 已知是 SubclassOfSomething 而不是 Something.

使用泛型:

from abc import ABC, abstractmethod
from typing import Generic, TypeVar


SomethingT = TypeVar('SomethingT', bound='Something')


...


class Foo(ABC, Generic[SomethingT]):
    my_class: SomethingT

    def __init__(self):
        self.my_class = self.get_something()

    @abstractmethod
    def get_something(self) -> SomethingT:
        pass


class SubclassOfFoo(Foo[SubclassOfSomething]):
    def __init__(self):
        super().__init__()

    def get_something(self) -> SubclassOfSomething:
        return SubclassOfSomething()

    def do_something_special(self):
        # inferred type of `self.my_class` will be `SubclassOfSomething`
        self.my_class.something_special()