mirror of
https://github.com/pyapp-kit/superqt.git
synced 2025-12-15 18:50:05 +01:00
feat: add error exceptions_as_dialog context manager to catch and show Exceptions (#191)
* feat: add error messagebox context * typing * Update src/superqt/utils/_errormsg_context.py Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com> * add tests * style: [pre-commit.ci] auto fixes [...] * docs: add docs * test button result * format doc * docs: update docs * docs * add dialog example * pass flags * skip mac ci pyside6 --------- Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import os
|
||||
import sys
|
||||
from unittest.mock import Mock
|
||||
|
||||
from qtpy.QtCore import QObject, Signal
|
||||
import pytest
|
||||
import qtpy
|
||||
from qtpy.QtCore import QObject, QTimer, Signal
|
||||
from qtpy.QtWidgets import QApplication, QErrorMessage, QMessageBox
|
||||
|
||||
from superqt.utils import signals_blocked
|
||||
from superqt.utils import exceptions_as_dialog, signals_blocked
|
||||
from superqt.utils._util import get_max_args
|
||||
|
||||
|
||||
@@ -91,3 +96,46 @@ def test_get_max_args_methods():
|
||||
assert get_max_args(A().fun1) == 0
|
||||
assert get_max_args(A().fun2) == 1
|
||||
assert get_max_args(A()) == 2
|
||||
|
||||
|
||||
MAC_CI_PYSIDE6 = bool(
|
||||
sys.platform == "darwin" and os.getenv("CI") and qtpy.API_NAME == "PySide6"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(MAC_CI_PYSIDE6, reason="still hangs on mac ci with pyside6")
|
||||
def test_exception_context(qtbot, qapp: QApplication) -> None:
|
||||
def accept():
|
||||
for wdg in qapp.topLevelWidgets():
|
||||
if isinstance(wdg, QMessageBox):
|
||||
wdg.button(QMessageBox.StandardButton.Ok).click()
|
||||
|
||||
with exceptions_as_dialog():
|
||||
QTimer.singleShot(0, accept)
|
||||
raise Exception("This will be caught and shown in a QMessageBox")
|
||||
|
||||
with pytest.raises(ZeroDivisionError), exceptions_as_dialog(ValueError):
|
||||
1 / 0 # noqa
|
||||
|
||||
with exceptions_as_dialog(msg_template="Error: {exc_value}"):
|
||||
QTimer.singleShot(0, accept)
|
||||
raise Exception("This message will be used as 'exc_value'")
|
||||
|
||||
err = QErrorMessage()
|
||||
with exceptions_as_dialog(use_error_message=err):
|
||||
QTimer.singleShot(0, err.accept)
|
||||
raise AssertionError("Uncheck the checkbox to ignore this in the future")
|
||||
|
||||
# tb formatting smoke test, and return value checking
|
||||
exc = ValueError("Bad Val")
|
||||
with exceptions_as_dialog(
|
||||
msg_template="{tb}",
|
||||
buttons=QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel,
|
||||
) as ctx:
|
||||
qtbot.addWidget(ctx.dialog)
|
||||
QTimer.singleShot(100, accept)
|
||||
raise exc
|
||||
|
||||
assert isinstance(ctx.dialog, QMessageBox)
|
||||
assert ctx.dialog.result() == QMessageBox.StandardButton.Ok
|
||||
assert ctx.exception is exc
|
||||
|
||||
Reference in New Issue
Block a user