【问题标题】:From classmethod to staticmethod: mypy complains for mixin but not inheritance从 classmethod 到 staticmethod:mypy 抱怨 mixin 而不是继承
【发布时间】:2021-08-07 19:44:56
【问题描述】:

当我使用继承时,当我将 classmethod 更改为 staticmethod 时,mypy 不会抱怨:

class A:
    @classmethod
    def f(cls, a: int) -> int:
        return a

class B(A):
    @staticmethod
    def f(a: int) -> int:
        return a+1

但是,如果我使用 mixin 来做同样的事情:

class C:
    @staticmethod
    def f(a: int) -> int:
        return a+1

class D(C, A):
    pass

mypy 给出以下错误(在class D(C, A): 行):

Definition of "f" in base class "C" is incompatible with definition in base class "A"

为什么会这样?

【问题讨论】:

    标签: python-3.x mixins type-hinting mypy python-typing


    【解决方案1】:

    实际上这是在 mypy 0.710 中修复的一个错误,可能是由 #6720 修复的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 2013-01-26
      • 2016-10-30
      • 1970-01-01
      • 2022-12-28
      相关资源
      最近更新 更多