在从 QTableView 继承的类中,我有这个函数:
def edit(
self,
index: QModelIndex,
trigger: QAbstractItemView.EditTrigger,
event: Optional[QEvent],
) -> bool:
[...]
mypy 报告此错误,并表示:
error: Signature of "edit" incompatible with supertype "QAbstractItemView" [override]
note: Superclass:
note: @overload
note: def edit(self, index: QModelIndex) -> None
note: @overload
note: def edit(self, index: QModelIndex, trigger: EditTrigger, event: QEvent | None) -> bool
note: Subclass:
note: def edit(self, index: QModelIndex, trigger: EditTrigger, event: QEvent | None) -> bool
在我看来,我的子类与第二个 @overload 定义完全匹配。为什么 mypy 会标记这个?
感谢@STerliakov 为我指明了正确的方向。对于发现这个问题的其他人,我使用的修复如下。请注意,省略号(“...”)实际上是三个点,而不是某些未指定代码的占位符。
from typing import overload
@overload
def edit(self, index: QModelIndex) -> None:
...
@overload
def edit(
self,
index: QModelIndex,
trigger: QAbstractItemView.EditTrigger,
event: Optional[QEvent]
) -> bool:
...
def edit(self, index, trigger, event):
[Function implementation code goes here]