diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 976e997a472b..cc18b6b21bf0 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -339,8 +339,9 @@ def switch_backend(newbackend: str) -> None: old_backend = dict.__getitem__(rcParams, 'backend') module = importlib.import_module(cbook._backend_module_name(newbackend)) + canvas_class = module.FigureCanvas - required_framework = module.FigureCanvas.required_interactive_framework + required_framework = canvas_class.required_interactive_framework if required_framework is not None: current_framework = cbook._get_running_interactive_framework() if (current_framework and required_framework @@ -369,8 +370,6 @@ class backend_mod(matplotlib.backend_bases._Backend): # update backend_mod accordingly; also, per-backend customization of # draw_if_interactive is disabled. if new_figure_manager is None: - # Only try to get the canvas class if have opted into the new scheme. - canvas_class = backend_mod.FigureCanvas def new_figure_manager_given_figure(num, figure): return canvas_class.new_manager(figure, num) @@ -394,8 +393,7 @@ def draw_if_interactive() -> None: # If the manager explicitly overrides pyplot_show, use it even if a global # show is already present, as the latter may be here for backcompat. - manager_class = getattr(getattr(backend_mod, "FigureCanvas", None), - "manager_class", None) + manager_class = getattr(canvas_class, "manager_class", None) # We can't compare directly manager_class.pyplot_show and FMB.pyplot_show because # pyplot_show is a classmethod so the above constructs are bound classmethods, and # thus always different (being bound to different classes). We also have to use @@ -405,6 +403,10 @@ def draw_if_interactive() -> None: if (show is None or (manager_pyplot_show is not None and manager_pyplot_show != base_pyplot_show)): + if not manager_pyplot_show: + raise ValueError( + f"Backend {newbackend} defines neither FigureCanvas.manager_class nor " + f"a toplevel show function") _pyplot_show = cast('Any', manager_class).pyplot_show backend_mod.show = _pyplot_show # type: ignore[method-assign]