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

Skip to content

Commit db6198d

Browse files
committed
Clarify loading of backend FigureCanvas and show().
- FigureCanvas is now required to exist on backend modules (since the deprecation elapsed in 3.8) and we indeed already access that attribute directly when checking required_interactive_framework, so remove redundant getattrs. - Clarify the error message when manager_class is None *and* there's no global show(). (Previously an AttributeError would be raised.)
1 parent 9dfa263 commit db6198d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/matplotlib/pyplot.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ def switch_backend(newbackend: str) -> None:
339339
old_backend = dict.__getitem__(rcParams, 'backend')
340340

341341
module = importlib.import_module(cbook._backend_module_name(newbackend))
342+
canvas_class = module.FigureCanvas
342343

343-
required_framework = module.FigureCanvas.required_interactive_framework
344+
required_framework = canvas_class.required_interactive_framework
344345
if required_framework is not None:
345346
current_framework = cbook._get_running_interactive_framework()
346347
if (current_framework and required_framework
@@ -369,8 +370,6 @@ class backend_mod(matplotlib.backend_bases._Backend):
369370
# update backend_mod accordingly; also, per-backend customization of
370371
# draw_if_interactive is disabled.
371372
if new_figure_manager is None:
372-
# Only try to get the canvas class if have opted into the new scheme.
373-
canvas_class = backend_mod.FigureCanvas
374373

375374
def new_figure_manager_given_figure(num, figure):
376375
return canvas_class.new_manager(figure, num)
@@ -394,8 +393,7 @@ def draw_if_interactive() -> None:
394393

395394
# If the manager explicitly overrides pyplot_show, use it even if a global
396395
# show is already present, as the latter may be here for backcompat.
397-
manager_class = getattr(getattr(backend_mod, "FigureCanvas", None),
398-
"manager_class", None)
396+
manager_class = getattr(canvas_class, "manager_class", None)
399397
# We can't compare directly manager_class.pyplot_show and FMB.pyplot_show because
400398
# pyplot_show is a classmethod so the above constructs are bound classmethods, and
401399
# thus always different (being bound to different classes). We also have to use
@@ -405,6 +403,10 @@ def draw_if_interactive() -> None:
405403
if (show is None
406404
or (manager_pyplot_show is not None
407405
and manager_pyplot_show != base_pyplot_show)):
406+
if not manager_pyplot_show:
407+
raise ValueError(
408+
f"Backend {newbackend} defines neither FigureCanvas.manager_class nor "
409+
f"a toplevel show function")
408410
_pyplot_show = cast('Any', manager_class).pyplot_show
409411
backend_mod.show = _pyplot_show # type: ignore[method-assign]
410412

0 commit comments

Comments
 (0)