@@ -339,8 +339,9 @@ def switch_backend(newbackend: str) -> None:
339
339
old_backend = dict .__getitem__ (rcParams , 'backend' )
340
340
341
341
module = importlib .import_module (cbook ._backend_module_name (newbackend ))
342
+ canvas_class = module .FigureCanvas
342
343
343
- required_framework = module . FigureCanvas .required_interactive_framework
344
+ required_framework = canvas_class .required_interactive_framework
344
345
if required_framework is not None :
345
346
current_framework = cbook ._get_running_interactive_framework ()
346
347
if (current_framework and required_framework
@@ -369,8 +370,6 @@ class backend_mod(matplotlib.backend_bases._Backend):
369
370
# update backend_mod accordingly; also, per-backend customization of
370
371
# draw_if_interactive is disabled.
371
372
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
374
373
375
374
def new_figure_manager_given_figure (num , figure ):
376
375
return canvas_class .new_manager (figure , num )
@@ -394,8 +393,7 @@ def draw_if_interactive() -> None:
394
393
395
394
# If the manager explicitly overrides pyplot_show, use it even if a global
396
395
# 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 )
399
397
# We can't compare directly manager_class.pyplot_show and FMB.pyplot_show because
400
398
# pyplot_show is a classmethod so the above constructs are bound classmethods, and
401
399
# thus always different (being bound to different classes). We also have to use
@@ -405,6 +403,10 @@ def draw_if_interactive() -> None:
405
403
if (show is None
406
404
or (manager_pyplot_show is not None
407
405
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" )
408
410
_pyplot_show = cast ('Any' , manager_class ).pyplot_show
409
411
backend_mod .show = _pyplot_show # type: ignore[method-assign]
410
412
0 commit comments