Ily*_*rov 5 python typechecking mypy
我有一个MutableSequence
像这样继承的类:
class QqTag(MutableSequence):
def __init__(self):
self._children = []
def __getitem__(self, idx: int) -> 'QqTag':
return self._children[idx]
Run Code Online (Sandbox Code Playgroud)
mypy抱怨Signature of "__getitem__" incompatible with supertype "Sequence"
。
在中Sequence
,此方法定义为:
@abstractmethod
def __getitem__(self, index):
raise IndexError
Run Code Online (Sandbox Code Playgroud)
那么,这是什么问题?为什么mypy对我的实施不满意?
正如评论中提到的,也可以传递一个 typeof 切片。即,更改idx: int
为idx: Union[int, slice]
。
这将使 mypy 高兴(至少在我的机器上;):
class QqTag(MutableSequence):
def __init__(self):
self._children = []
def __getitem__(self, idx: Union[int, slice]) -> 'QqTag':
return self._children[idx]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
458 次 |
最近记录: |