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

Skip to content

Private helper to get requested backend without triggering resolution. #23263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 2 additions & 7 deletions lib/matplotlib/backends/qt_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down