Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d8505b

Browse files
committed
Private helper to get requested backend without triggering resolution.
... avoiding having to spell out the `dict.__getitem__` dance, which is really an implementation detail of rcParams.
1 parent 60ae76b commit 2d8505b

3 files changed

Lines changed: 10 additions & 11 deletions

File tree

lib/matplotlib/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,11 @@ def __getitem__(self, key):
678678

679679
return dict.__getitem__(self, key)
680680

681+
def _get_backend_or_none(self):
682+
"""Get the requested backend, if any, without triggering resolution."""
683+
backend = dict.__getitem__(self, "backend")
684+
return None if backend is rcsetup._auto_backend_sentinel else backend
685+
681686
def __repr__(self):
682687
class_name = self.__class__.__name__
683688
indent = len(class_name) + 1
@@ -1129,9 +1134,8 @@ def use(backend, *, force=True):
11291134
matplotlib.get_backend
11301135
"""
11311136
name = validate_backend(backend)
1132-
# we need to use the base-class method here to avoid (prematurely)
1133-
# resolving the "auto" backend setting
1134-
if dict.__getitem__(rcParams, 'backend') == name:
1137+
# don't (prematurely) resolve the "auto" backend setting
1138+
if rcParams._get_backend_or_none() == name:
11351139
# Nothing to do if the requested backend is already set
11361140
pass
11371141
else:

lib/matplotlib/backends/qt_compat.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,10 @@
5555
QT_API = QT_API_PYSIDE2
5656
# Otherwise, check the QT_API environment variable (from Enthought). This can
5757
# only override the binding, not the backend (in other words, we check that the
58-
# requested backend actually matches). Use dict.__getitem__ to avoid
58+
# requested backend actually matches). Use _get_backend_or_none to avoid
5959
# triggering backend resolution (which can result in a partially but
6060
# incompletely imported backend_qt5).
61-
elif (
62-
isinstance(dict.__getitem__(mpl.rcParams, "backend"), str) and
63-
dict.__getitem__(mpl.rcParams, "backend").lower() in [
64-
"qt5agg", "qt5cairo"
65-
]
66-
):
61+
elif (mpl.rcParams._get_backend_or_none() or "").lower().startswith("qt5"):
6762
if QT_API_ENV in ["pyqt5", "pyside2"]:
6863
QT_API = _ETS[QT_API_ENV]
6964
else:

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ def polar(*args, **kwargs):
21952195
# requested, ignore rcParams['backend'] and force selection of a backend that
21962196
# is compatible with the current running interactive framework.
21972197
if (rcParams["backend_fallback"]
2198-
and dict.__getitem__(rcParams, "backend") in (
2198+
and rcParams._get_backend_or_none() in (
21992199
set(_interactive_bk) - {'WebAgg', 'nbAgg'})
22002200
and cbook._get_running_interactive_framework()):
22012201
dict.__setitem__(rcParams, "backend", rcsetup._auto_backend_sentinel)

0 commit comments

Comments
 (0)