python / python/mypy

Wrong return type detected for @overload function

Aperta
#9,987 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-overloads
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo la chiamata segnalata con gli overload PySide6 forniti e mypy 0.800, quindi esamina come mypy verifica le chiamate overload rispetto alle dichiarazioni di QtWidgets.pyi. Il lavoro è completato quando la chiamata (QIcon, str) viene inferita come restituita da QAction anziché None, con un test di regressione che copra l'esempio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.