繁体   English   中英

由于属性协方差,如何避免子类中的警告?

[英]How can I avoid a warning in a child class due to attribute covariance?

我试图避免在代码示例的最后一行中显示为注释的 linter 警告。

我理解它为什么会发生,我知道我可以忽略它,因为这是 Python。 但作为一种自我锻炼,我一直试图想出一种正确输入的方法来避免它,但一直无法找到解决方案。

这是一个代码示例:

class Content(ABC):
    def __init__(self, data: Dict):
        self._data: Dict = data


class AlertContent(Content):
    def __init__(self, alert: Alert):
        data: Dict = get_data_from_alert(alert)
        super().__init__(data)
        self.alert_priority: str = alert.priority


class Envelope(ABC):
    def __init__(self, content: Content):
        self._content: Content = content
    
    @property
    @abstractmethod
    def priority(self) -> str:
        raise NotImplementedError


class AlertEnvelope(Envelope):
    def __init__(self, content: AlertContent):
        super().__init__(content)

    @property
    @abstractmethod
    def priority(self) -> str:
        return self._content.alert_priority  # Warning: Unresolved attribute reference 'alert_priority' for class 'Content'

你有什么想法?

我认为这是一个风格问题,但有争议的是,由于准确的打字是你设计的一部分,你实际上并不想要super().__init__(content)因为你正在构建一个依赖于AlertContent具体接口,而父类显式地使用更抽象的类型对属性进行类型化,该类型没有实现该接口。

因此,

class AlertEnvelope(Envelope):
    def __init__(self, content: AlertContent):
        self._content: AlertContent = content

考虑到您的目标是什么,可能仍然是 DRY 代码。

我最终选择了以下解决方案:

class AlertEnvelope(Envelope):
    def __init__(self, content: AlertContent):
        super().__init__(content)

    # Inherited from Envelope
    # @property
    # def content(self) -> Content:
    #     return self._content

    def alert_content(self) -> AlertContent:
        return cast(AlertContent, self._content) # ** Reasoning below 

    @property
    @abstractmethod
    def priority(self) -> str:
        return self.alert_content.alert_priority

** 我选择解决方案的原因是:

  1. 尽管cast不会做任何事情(定义为pass ),但它是一种让 linters 知道输入中发生了某些变化的方式,因此您不会收到错误消息。 而且,作为pass ,它的成本几乎可以忽略不计。
  2. 任何阅读代码的人都可以明确表示正在发生的事情。
  3. 模仿你在 C++ 中做的方式,一种硬类型语言,这有点是我的目标之一,遵循硬类型方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM