python / python/mypy

Bad error with Protocol and class heirarchy with overloads

Offen
#17,060 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die bereitgestellte PyQt6-Reproduktion mit mypy 1.9.0 auszuführen, und untersuche, wie die überladenen Indexdefinitionen zwischen QAbstractItemModel, QFileSystemModel und dem Protokoll verglichen werden. Vergleiche das Ergebnis mit pyright und pytype wie berichtet; fertig ist die Aufgabe, wenn die Inkompatibilität korrekt behandelt wird oder die Diagnose das tatsächliche Problem klar erklärt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools, tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.