Signature of "method" incompatible with supertype "Class" [override] #1219
-
I am using I've imported from __future__ import annotations
@overload
def addTab(
self,
__arg__2: str,
) -> int:
...
def addTab(
self,
__widget: PySide6.QtWidgets.QWidget,
__label: str,
) -> int:
...
The documentation for PySide6 seems to indicate my stubs are correct, but I could be wrong. In my source, I have a class that subclasses from qtpy import QtWidgets
class MyTabWidget(QtWidgets.QTabWidget):
def addTab(
self,
widget: QtWidgets.QWidget,
icon: QtGui.QIcon | QtGui.QPixmap | None,
label: str,
) -> int:
# Implementation here However, I suspect the problem might be due to Issue #11004, but I'm not sure. Can anyone tell me if I am doing something wrong or if this is a bug I should |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The mypy error is trying to say that A simple solution would be to define your def addTab(
self,
widget: QtWidgets.QWidget,
icon: QtGui.QIcon | QtGui.QPixmap | str | None,
label: str = "",
) -> int:
if isinstance(icon, str):
label = icon
icon = None
... It doesn't error in some cases where you would want an error though, such as @overload
def addTab(
self,
__widget: PySide6.QtWidgets.QWidget,
__label: str,
) -> int:
...
@overload
def addTab(
self,
__widget: PySide6.QtWidgets.QWidget,
__icon: QtGui.QIcon | QtGui.QPixmap | str | None,
__label: str,
) -> int:
...
def addTab(
self,
widget: QtWidgets.QWidget,
icon: QtGui.QIcon | QtGui.QPixmap | str | None,
label: str = "",
) -> int:
if isinstance(icon, str):
label = icon
icon = None
... In general, overriding overloaded methods isn't pretty. Are you sure that overriding this method is a good idea? Why not use "has-a" instead of "is-a" here, and just define your own method that calls |
Beta Was this translation helpful? Give feedback.
-
Ya, I forgot you get the script $ pyside6-genpyi all generates the stubs, but even those use:
so it seems you're right and overloads are going to be a bit messy...
Bingo. It's a library. I'll definitely copy/paste the overloads. |
Beta Was this translation helpful? Give feedback.
-
I found the actual answer is this: class MyTabWidget(QtWidgets.QTabWidget):
@typing.overload
def addTab(
self,
widget: QtWidgets.QWidget,
arg__2: str,
/,
) -> int: # noqa: D102 # Overloads don't get docstrings
...
@typing.overload
def addTab(
self,
widget: QtWidgets.QWidget,
icon: QtGui.QIcon | QtGui.QPixmap,
label: str,
/,
) -> int: # noqa: D102 # Overloads don't get docstrings
...
def addTab(
self,
widget: QtWidgets.QWidget,
icon: QtGui.QIcon | QtGui.QPixmap | str,
label: str = '',
/,
) -> int:
# Implementation goes here... For details, see Overloaded function implementation does not accept all possible arguments of signature #13077 |
Beta Was this translation helpful? Give feedback.
I found the actual answer is this: