10000 Simplify qt_compat, in particular post-removal of qt4 support. · sauerburger/matplotlib@3f6bc98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f6bc98

Browse files
committed
Simplify qt_compat, in particular post-removal of qt4 support.
Also note that `None` is never keyed off `_ETS`, which allows removing the `None: None` entry have some more simplifications. Also avoid the need for exception chaining suppression (`raise ... from None`). Also clarify the error message when no qt binding can be imported.
1 parent 60ae76b commit 3f6bc98

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

lib/matplotlib/backends/qt_compat.py

Lines changed: 12 additions & 21 deletions
143
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
- if any of PyQt6, PySide6, PyQt5, or PySide2 have already been
66
imported (checked in that order), use it;
77
- otherwise, if the QT_API environment variable (used by Enthought) is set, use
8-
it to determine which binding to use (but do not change the backend based on
9-
it; i.e. if the Qt5Agg backend is requested but QT_API is set to "pyqt4",
10-
then actually use Qt5 with PyQt5 or PySide2 (whichever can be imported);
8+
it to determine which binding to use;
119
- otherwise, use whatever the rcParams indicate.
1210
"""
1311

@@ -31,18 +29,12 @@
3129
QT_API_PYSIDE6 = "PySide6"
3230
QT_API_PYQT5 = "PyQt5"
3331
QT_API_PYSIDE2 = "PySide2"
34-
QT_API_PYQTv2 = "PyQt4v2"
35-
QT_API_PYSIDE = "PySide"
36-
QT_API_PYQT = "PyQt4" # Use the old sip v1 API (Py3 defaults to v2).
3732
QT_API_ENV = os.environ.get("QT_API")
3833
if QT_API_ENV is not None:
3934
QT_API_ENV = QT_API_ENV.lower()
40-
# Mapping of QT_API_ENV to requested binding. ETS does not support PyQt4v1.
41-
# (https://github.com/enthought/pyface/blob/master/pyface/qt/__init__.py)
42-
_ETS = {
35+
_ETS = { # Mapping of QT_API_ENV to requested binding.
4336
"pyqt6": QT_API_PYQT6, "pyside6": QT_API_PYSIDE6,
4437
"pyqt5": QT_API_PYQT5, "pyside2": QT_API_PYSIDE2,
45-
None: None
4638
}
4739
# First, check if anything is already imported.
4840
if sys.modules.get("PyQt6.QtCore"):
@@ -73,15 +65,12 @@
7365
# fully manually embedding Matplotlib in a Qt app without using pyplot).
7466
elif QT_API_ENV is None:
7567
QT_API = None
68+
elif QT_API_ENV in _ETS:
69+
QT_API = _ETS[QT_API_ENV]
7670
else:
77-
try:
78-
QT_API = _ETS[QT_API_ENV]
79-
except KeyError:
80-
raise RuntimeError(
81-
"The environment variable QT_API has the unrecognized value "
82-
f"{QT_API_ENV!r}; "
83-
f"valid values are {set(k for k in _ETS if k is not None)}"
84-
) from None
71+
raise RuntimeError(
72+
"The environment variable QT_API has the unrecognized value {!r}; "
73+
"valid values are {}".format(QT_API_ENV, ", ".join(_ETS)))
8574

8675

8776
def _setup_pyqt5plus():
@@ -139,7 +128,9 @@ def _isdeleted(obj):
139128
continue
140129
break
141130
else:
142-
raise ImportError("Failed to import any qt binding")
131+
raise ImportError(
132+
"Failed to import any of the following Qt binding modules: {}"
133+
.format(", ".join(_ETS.values())))
134
else: # We should not get there.
144135
raise AssertionError(f"Unexpected QT_API: {QT_API}")
145136

@@ -186,7 +177,7 @@ def _devicePixelRatioF(obj):
186177
except AttributeError:
187178
pass
188179
try:
189-
# Not available on Qt4 or some older Qt5.
180+
# Not available on older Qt5.
190181
# self.devicePixelRatio() returns 0 in rare cases
191182
return obj.devicePixelRatio() or 1
192183
except AttributeError:
@@ -200,7 +191,7 @@ def _setDevicePixelRatio(obj, val):
200191
This can be replaced by the direct call when we require Qt>=5.6.
201192
"""
202193
if hasattr(obj, 'setDevicePixelRatio'):
203-
# Not available on Qt4 or some older Qt5.
194+
# Not available on older Qt5.
204195
obj.setDevicePixelRatio(val)
205196

206197

0 commit comments

Comments
 (0)
0