Search code examples
pyqtmypy

Why is mypy giving me an error in this pyqt code?


In a class inherited from QTableView I have this function:

def edit(
        self,
        index: QModelIndex,
        trigger: QAbstractItemView.EditTrigger,
        event: Optional[QEvent],
    ) -> bool:
    [...]

mypy reports this an error, saying:

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

It looks to me as if my subclass exactly matches the second @overload definition. Why is mypy flagging this?


Solution

  • Thanks to @STerliakov who pointed me in the right direction. For anyone else finding this question the fix I used is below. Note that the ellipses ("...") are literally three dots rather than a placeholder for some unspecified code.

        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]