python / python/mypy

Bad error with Protocol and class heirarchy with overloads

Aperta
#17,060 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione PyQt6 fornita con mypy 1.9.0 e verifica come vengono confrontate le definizioni degli indici sovraccaricati tra QAbstractItemModel, QFileSystemModel e il protocollo. Confronta il risultato con pyright e pytype come riportato; il lavoro è concluso quando l'incompatibilità viene gestita correttamente oppure la diagnostica spiega chiaramente il problema effettivo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools, tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.