python / python/mypy

False positive [override] check, superclass signature wrong

Open
#16,437 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy is using an incorrect method signature on [override] checks. The code looks like this:

from PyQt6 import QtCore, QtGui, QtWidgets

class SEToolsTreeWidget(QtWidgets.QTreeWidget):

    def contextMenuEvent(self, event: QtGui.QContextMenuEvent) -> None:
        ...

Full code: https://github.com/pebenito/setools/blob/pyqt-rework/setoolsgui/widgets/views/treewidget.py

When inspecting the PyQt6 definitions, the contextMenuEvent() parameter is not optional:

$ grep 'def contextMenuEvent' /usr/lib64/python3.11/site-packages/PyQt6/*.pyi
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, e: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, e: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, event: 'QGraphicsSceneContextMenuEvent') -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, event: 'QGraphicsSceneContextMenuEvent') -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, event: 'QGraphicsSceneContextMenuEvent') -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, event: 'QGraphicsSceneContextMenuEvent') -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, event: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, ev: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, event: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, contextMenuEvent: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, e: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, a0: QtGui.QContextMenuEvent) -> None: ...
/usr/lib64/python3.11/site-packages/PyQt6/QtWidgets.pyi:    def contextMenuEvent(self, e: QtGui.QContextMenuEvent) -> None: ...

Expected Behavior

No errors

Actual Behavior

setoolsgui/widgets/views/treewidget.py:15: error: Argument 1 of "contextMenuEvent" is incompatible with supertype "QAbstractScrollArea"; supertype defines the argument type as "QContextMenuEvent | None"  [override]
        def contextMenuEvent(self, event: QtGui.QContextMenuEvent) -> None:
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setoolsgui/widgets/views/treewidget.py:15: note: This violates the Liskov substitution principle
setoolsgui/widgets/views/treewidget.py:15: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
setoolsgui/widgets/views/treewidget.py:15: error: Argument 1 of "contextMenuEvent" is incompatible with supertype "QWidget"; supertype defines the argument type as "QContextMenuEvent | None"  [override]
        def contextMenuEvent(self, event: QtGui.QContextMenuEvent) -> None:
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example run: https://github.com/pebenito/setools/actions/runs/6804093923/job/18500854778#step:8:36

Your Environment

This is running in tox. I hit this both on my Fedora 38 system and also GitHub actions (Ubuntu). tox.ini if useful: https://github.com/pebenito/setools/blob/pyqt-rework/tox.ini

  • Mypy version used: 1.6.0, 1.6.1, 1.7.0
  • Mypy command-line flags: only -p to specify package
  • Mypy configuration options from mypy.ini (and other config files): from pyproject.toml
[tool.mypy]
no_implicit_optional = true
pretty = true

[[tool.mypy.overrides]]
module = ['networkx.*',
          'pytestqt.*']
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = ['tests-gui.*']
disable_error_code = ["union-attr"]
  • Python version used: 3.10-3.12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with setoolsgui/widgets/views/treewidget.py using the tox.ini environment and the pyproject.toml mypy settings. Start by tracing the override check for the PyQt6 contextMenuEvent signature; done means the shown valid override produces no false-positive errors while incompatible overrides remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.