Skip to content

Commit cb7f649

Browse files
authored
Merge pull request #25772 from ksunden/qt_import_error
Only print actually tested QT APIs when erroring
2 parents 2e2d2d5 + b64444c commit cb7f649

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/matplotlib/backends/qt_compat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def _isdeleted(obj):
133133
else:
134134
raise ImportError(
135135
"Failed to import any of the following Qt binding modules: {}"
136-
.format(", ".join(_ETS.values())))
136+
.format(", ".join([QT_API for _, QT_API in _candidates]))
137+
)
137138
else: # We should not get there.
138139
raise AssertionError(f"Unexpected QT_API: {QT_API}")
139140
_version_info = tuple(QtCore.QLibraryInfo.version().segments())

lib/matplotlib/tests/test_backends_interactive.py

+20
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,26 @@ def test_qt5backends_uses_qt5():
343343
_run_helper(_implcore, timeout=_test_timeout)
344344

345345

346+
def _impl_missing():
347+
import sys
348+
# Simulate uninstalled
349+
sys.modules["PyQt6"] = None
350+
sys.modules["PyQt5"] = None
351+
sys.modules["PySide2"] = None
352+
sys.modules["PySide6"] = None
353+
354+
import matplotlib.pyplot as plt
355+
with pytest.raises(ImportError, match="Failed to import any of the following Qt"):
356+
plt.switch_backend("qtagg")
357+
# Specifically ensure that Pyside6/Pyqt6 are not in the error message for qt5agg
358+
with pytest.raises(ImportError, match="^(?:(?!(PySide6|PyQt6)).)*$"):
359+
plt.switch_backend("qt5agg")
360+
361+
362+
def test_qt_missing():
363+
_run_helper(_impl_missing, timeout=_test_timeout)
364+
365+
346366
def _impl_test_cross_Qt_imports():
347367
import sys
348368
import importlib

0 commit comments

Comments
 (0)