python / python/mypy

Wrong return type detected for @overload function

Abierto
#9,987 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-overloads
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza reproduciendo la llamada reportada con las sobrecargas de PySide6 proporcionadas y mypy 0.800; después, inspecciona cómo mypy comprueba las llamadas sobrecargadas frente a las declaraciones de QtWidgets.pyi. Se considerará terminado cuando la llamada (QIcon, str) se infiera como que devuelve QAction en lugar de None, con una prueba de regresión que cubra el ejemplo.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.