Skip to content

False positive [override] check, superclass signature wrong #16437

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

Open
pebenito opened this issue Nov 9, 2023 · 4 comments
Open

False positive [override] check, superclass signature wrong #16437

pebenito opened this issue Nov 9, 2023 · 4 comments
Labels
bug mypy got something wrong

Comments

@pebenito
Copy link

pebenito commented Nov 9, 2023

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
@pebenito pebenito added the bug mypy got something wrong label Nov 9, 2023
@pebenito
Copy link
Author

pebenito commented Nov 9, 2023

This is pure speculation, but the root cause of this might be the same as #16429.

@pebenito
Copy link
Author

@JelleZijlstra
Copy link
Member

I looked at this a little but couldn't find anything. I suspect mypy may be looking at a different set of types for PyQt6 than you're using.

I don't know what #16429 has to do with this issue.

@pebenito
Copy link
Author

pebenito commented Dec 4, 2023

I looked at this a little but couldn't find anything. I suspect mypy may be looking at a different set of types for PyQt6 than you're using.

I'm not sure I understand what you're saying but I checked PyQt5 and this method has the same signature. It reproduces on my Fedora systems and also in the Ubuntu VMs in GitHub actions, plus using tox. If mypy is using the wrong .pyi files, I can't see how I could tell with this particular method.

I don't know what #16429 has to do with this issue.

The only reason I suggested that is because the symptom is making stuff Optional when it's not. I don't mypy code at all, so this could simply be a coincidence.

pebenito added a commit to pebenito/setools that referenced this issue Feb 13, 2024
Mypy is flagging contextMenuEvent signature problems.  This method does not
have an optional event parameter.

See:
* python/mypy#16437
* https://doc.qt.io/qt-6/qwidget.html#contextMenuEvent
* https://wiki.python.org/moin/PyQt/Handling%20context%20menus

The mypy error looks like this:

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:

Signed-off-by: Chris PeBenito <[email protected]>
pebenito added a commit to pebenito/setools that referenced this issue Feb 13, 2024
Mypy is flagging contextMenuEvent signature problems.  This method does not
have an optional event parameter.

See:
* python/mypy#16437
* https://doc.qt.io/qt-6/qwidget.html#contextMenuEvent
* https://wiki.python.org/moin/PyQt/Handling%20context%20menus

The mypy error looks like this:

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:

Signed-off-by: Chris PeBenito <[email protected]>
pebenito added a commit to pebenito/setools that referenced this issue Feb 14, 2024
Mypy is flagging contextMenuEvent signature problems.  This method does not
have an optional event parameter.

See:
* python/mypy#16437
* https://doc.qt.io/qt-6/qwidget.html#contextMenuEvent
* https://wiki.python.org/moin/PyQt/Handling%20context%20menus

The mypy error looks like this:

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:

Signed-off-by: Chris PeBenito <[email protected]>
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

2 participants