【发布时间】:2021-05-06 17:57:08
【问题描述】:
我有一个显示各种网格的应用程序。网格具有不同种类的功能,所以我的设计是一个处理通用网格事物的基本网格类,以及将各种特征混合到一个具有基本网格的类中:
class BaseGrid(wx.grid.Grid):
def foo():
return 0
class Grid_Mixin1():
def feature():
self.foo()
class Grid_Mixin2():
def feature():
self.foo()
class SpecificGrid(Mixin1, BaseGrid):
...
问题是我正在尝试使用类型提示,而在 mixins 中,类型检查器不知道 self.foo() 会存在,从而引发未知成员错误。
我决定使用协议让静态类型检查器知道 mixin 符合什么:
from typing import Protocol
class MyProtocol(Protocol):
def foo(self):
...
class Grid_Mixin1(MyProtocol):
def feature():
self.foo()
class Grid_Mixin2(MyProtocol):
def feature():
self.foo()
现在当我尝试使用SpecificGrid 时,我得到了臭名昭著的
TypeError: 元类冲突:派生类的元类必须 是其所有基类的元类的(非严格)子类
我只能假设 wx.grid.Grid 是从它自己的元类派生的?从文档中对我来说并不明显,但这是我唯一的解释。我的评估是否正确?我能做些什么来解决这个问题?
【问题讨论】:
标签: python-3.x wxpython protocols static-analysis metaclass