diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b0d1defdb4d6..c268a56724c9 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -678,6 +678,11 @@ def __getitem__(self, key): return dict.__getitem__(self, key) + def _get_backend_or_none(self): + """Get the requested backend, if any, without triggering resolution.""" + backend = dict.__getitem__(self, "backend") + return None if backend is rcsetup._auto_backend_sentinel else backend + def __repr__(self): class_name = self.__class__.__name__ indent = len(class_name) + 1 @@ -1129,9 +1134,8 @@ def use(backend, *, force=True): matplotlib.get_backend """ name = validate_backend(backend) - # we need to use the base-class method here to avoid (prematurely) - # resolving the "auto" backend setting - if dict.__getitem__(rcParams, 'backend') == name: + # don't (prematurely) resolve the "auto" backend setting + if rcParams._get_backend_or_none() == name: # Nothing to do if the requested backend is already set pass else: diff --git a/lib/matplotlib/backends/qt_compat.py b/lib/matplotlib/backends/qt_compat.py index 47c1cedff741..61903a0da4aa 100644 --- a/lib/matplotlib/backends/qt_compat.py +++ b/lib/matplotlib/backends/qt_compat.py @@ -55,15 +55,10 @@ QT_API = QT_API_PYSIDE2 # Otherwise, check the QT_API environment variable (from Enthought). This can # only override the binding, not the backend (in other words, we check that the -# requested backend actually matches). Use dict.__getitem__ to avoid +# requested backend actually matches). Use _get_backend_or_none to avoid # triggering backend resolution (which can result in a partially but # incompletely imported backend_qt5). -elif ( - isinstance(dict.__getitem__(mpl.rcParams, "backend"), str) and - dict.__getitem__(mpl.rcParams, "backend").lower() in [ - "qt5agg", "qt5cairo" - ] -): +elif (mpl.rcParams._get_backend_or_none() or "").lower().startswith("qt5"): if QT_API_ENV in ["pyqt5", "pyside2"]: QT_API = _ETS[QT_API_ENV] else: diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 8d8cfaa9326c..193491f8a9ef 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2195,7 +2195,7 @@ def polar(*args, **kwargs): # requested, ignore rcParams['backend'] and force selection of a backend that # is compatible with the current running interactive framework. if (rcParams["backend_fallback"] - and dict.__getitem__(rcParams, "backend") in ( + and rcParams._get_backend_or_none() in ( set(_interactive_bk) - {'WebAgg', 'nbAgg'}) and cbook._get_running_interactive_framework()): dict.__setitem__(rcParams, "backend", rcsetup._auto_backend_sentinel)