Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad error with Protocol and class heirarchy with overloads #17060

Open
shaolo1 opened this issue Mar 23, 2024 · 0 comments
Open

Bad error with Protocol and class heirarchy with overloads #17060

shaolo1 opened this issue Mar 23, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@shaolo1
Copy link

shaolo1 commented Mar 23, 2024

I'm trying to implement a protocol that mimics QFileSystemModel so that I can use that protocol to define a new custom class. The protocol definition is generating a mypy error ( error: Definition of "index" in base class "QAbstractItemModel" is incompatible with definition in base class "FileSystemModelProtocol")
I've checked with pyright and pytype and neither report an error.

To Reproduce

import typing
from PyQt6 import QtCore, QtGui


class FileSystemModelProtocol(typing.Protocol):
    """Define the subset of QFileSystemModel methods used...for defining a class with the same behavior"""
    @typing.overload
    def index(self, row: int, column: int, parent: QtCore.QModelIndex = ...) -> QtCore.QModelIndex: ...
    @typing.overload
    def index(self, row: typing.Optional[str], column: int = ...) -> QtCore.QModelIndex: ...
    def index(self, row: typing.Optional[int | str] = ..., column: int = ..., parent: QtCore.QModelIndex = ...) -> QtCore.QModelIndex: ...


class CustomFileSystemModel(QtGui.QFileSystemModel, FileSystemModelProtocol):  # error: Definition of "index" in base class "QAbstractItemModel" is incompatible with definition in base class "FileSystemModelProtocol"
    pass


class TableModel(QtCore.QAbstractTableModel, FileSystemModelProtocol):
    def index_from_str(self, path: str) -> QtCore.QModelIndex:
        return QtCore.QModelIndex()  # TODO: lookup with path

    def index(self, row: typing.Optional[int | str] = None, column: int = 0, parent: QtCore.QModelIndex = QtCore.QModelIndex()) -> QtCore.QModelIndex:
        if isinstance(row, str):
            return self.index_from_str(row)
        if isinstance(row, int):
            return super().index(row, column, parent)
        return QtCore.QModelIndex()


c = CustomFileSystemModel()
t = TableModel()

Expected Behavior

No errors, or a better explanation of what is wrong.

Actual Behavior

error: Definition of "index" in base class "QAbstractItemModel" is incompatible with definition in base class "FileSystemModelProtocol" [misc]

Your Environment

  • Mypy version used:
  • mypy 1.9.0 (compiled: yes)
  • Mypy command-line flags:
  • none
  • Mypy configuration options from mypy.ini (and other config files):
  • none
  • Python version used:
    Python 3.11.7
    PyQt6==6.6.1
@shaolo1 shaolo1 added the bug mypy got something wrong label Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant