python-qt-tools / PyQt5-stubs

Stubs for PyQt5
GNU General Public License v3.0
69 stars 31 forks source link

QMessageBox signature doesn't match. #214

Open th3w1zard1 opened 3 months ago

th3w1zard1 commented 3 months ago

Attempting to send certain kwargs to QMessageBox will exception, while the stubs say it does support it. For example:

filepath_str, _filter = QFileDialog.getSaveFileName(self, "Save As", "", self._saveFilter, "")
if not filepath_str or not filepath_str.strip() or not os.path.exists(filepath_str):
  QMessageBox(
      icon=QMessageBox.Critical,
      title="Failed to add resource",
      text=f"Could not add resource at {filepath_str}: Choose a valid file.",
      parent=None,
      flags=Qt.Window | Qt.Dialog | Qt.WindowStaysOnTopHint,
  ).exec_()

The above code fails on PyQt5 but mypy does not catch it. The following works however:

filepath_str, _filter = QFileDialog.getSaveFileName(self, "Save As", "", self._saveFilter, "")
if not filepath_str or not filepath_str.strip() or not os.path.exists(filepath_str):
  QMessageBox(
      QMessageBox.Critical,
      "Failed to add resource",
      f"Could not add resource at {filepath_str}: Choose a valid file.",
      parent=None,
      flags=Qt.Window | Qt.Dialog | Qt.WindowStaysOnTopHint,
  ).exec_()
th3w1zard1 commented 3 months ago

Python 3.8 supports function sigs like this:


def my_function(arg1, arg2, /, arg3): ...

if you need to support older Python versions, just use a sys.version_info check for < 3.8, and just prefix the title and text args with __

Probably a better way to do this but that's just off the top of my head.