python / python/mypy

Wrong return type detected for @overload function

Open
#9,987 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported call using the supplied PySide6 overloads and mypy 0.800, then inspect how mypy checks overloaded calls against the QtWidgets.pyi declarations. Done means the (QIcon, str) call is inferred as returning QAction rather than None, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.