Bad error with Protocol and class heirarchy with overloads

Đang mở
#17,060 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
devtools, tooling

Hướng nghiên cứu

Bắt đầu bằng cách chạy bản tái hiện PyQt6 được cung cấp với mypy 1.9.0 và kiểm tra cách các định nghĩa chỉ mục overloaded được so sánh giữa QAbstractItemModel, QFileSystemModel và protocol. So sánh kết quả với pyright và pytype như đã được báo cáo; hoàn thành khi incompatibility được xử lý chính xác hoặc diagnostic giải thích rõ vấn đề thực tế.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug

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
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.