mat*_*att 5 python python-3.x mypy python-typing
我有一个做某事的父类和两个以正交方式包装方法的子类。当我尝试组合两个子类时,mypy 抱怨以下错误。
基类“Foo”中“run”的定义与基类“Bar”中的定义不兼容
这两个类如何“不兼容”以及如何修改我的代码以安抚 mypy?我可以不做就做吗
class Parent:
def run(self, a, b):
pass
class Foo(Parent):
def run(self, a, b, foo=None, **kwargs):
print('foo', foo)
super().run(a, b, **kwargs)
class Bar(Parent):
def run(self, a, b, bar=None, **kwargs):
print('bar', bar)
super().run(a, b, **kwargs)
class C(Foo, Bar):
pass
Run Code Online (Sandbox Code Playgroud)
小智 0
Mypy 抱怨只是因为您尝试从 Foo 和 Bar 继承 C,它们运行相同的方法,但参数不同。为了让 Mypy 满意,您必须使 Foo 和 Bar 类中的运行函数相同(匹配相同的函数签名),或者不从它们继承 C 类。
| 归档时间: |
|
| 查看次数: |
631 次 |
| 最近记录: |