python / python/mypy

Wrong return type detected for @overload function

Offen
#9,987 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-overloads
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

In the scenario where a @overload function is called, mypy doesn't reliably detect the correct return value.

I'm using the following code to call PySide6 functions:

gearIcon = QIcon(str('...'))
menuSettings: QMenu = self.menuBar().addMenu('&Tools')
actionSettings = menuSettings.addAction(gearIcon, '&Settings')
actionSettings.triggered.connect(self.showSettingsDialog)

mypy assumes addAction to return None and notes the following error:

error: "None" has no attribute "triggered"

The QtWidgets.pyi includes the following type declaration:

@typing.overload
def addAction(self, arg__1:PySide6.QtGui.QAction) -> None: ...
@typing.overload
def addAction(self, arg__1:PySide6.QtGui.QIcon, arg__2:str, arg__3:object, arg__4:typing.Optional[PySide6.QtGui.QKeySequence]=...) -> None: ...
@typing.overload
def addAction(self, arg__1:str, arg__2:object, arg__3:typing.Optional[PySide6.QtGui.QKeySequence]=...) -> None: ...
@typing.overload
def addAction(self, icon:PySide6.QtGui.QIcon, text:str) -> PySide6.QtGui.QAction: ...
@typing.overload
def addAction(self, icon:PySide6.QtGui.QIcon, text:str, receiver:PySide6.QtCore.QObject, member:bytes, shortcut:PySide6.QtGui.QKeySequence=...) -> PySide6.QtGui.QAction: ...
@typing.overload
def addAction(self, text:str) -> PySide6.QtGui.QAction: ...
@typing.overload
def addAction(self, text:str, receiver:PySide6.QtCore.QObject, member:bytes, shortcut:PySide6.QtGui.QKeySequence=...) -> PySide6.QtGui.QAction: ...

The overloaded addAction function that "best" (and exactly) matches the arguments (QIcon, str) is defined to return QAction. The None returning variants don't match the argument list, so they should not be considered.

The documentation states:

When you call an overloaded function, mypy will infer the correct return type by picking the best matching variant, after taking into consideration both the argument types and arity.
...
If there are multiple equally good matching variants, mypy will select the variant that was defined first. 

Your Environment

  • Mypy version used: 0.800 (same behavior in 0.790)
  • Python version used: 3.9.1

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie damit, den gemeldeten Aufruf mit den bereitgestellten PySide6-Overloads und mypy 0.800 zu reproduzieren. Untersuchen Sie anschließend, wie mypy überladene Aufrufe anhand der Deklarationen in QtWidgets.pyi prüft. Als erledigt gilt die Aufgabe, wenn der (QIcon, str)-Aufruf so abgeleitet wird, dass er QAction statt None zurückgibt, und ein Regressionstest das Beispiel abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.