From eef76b1ee941c18aa41f404cd13eb8f76006de91 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 2 Jun 2025 17:00:07 -0400 Subject: [PATCH] Make NavigationToolbar.configure_subplots return value consistent When the subplot tool already exists, then it is shown, but never returned. This seems to be an accident. --- lib/matplotlib/backend_bases.py | 2 +- lib/matplotlib/backend_bases.pyi | 2 +- lib/matplotlib/tests/test_backend_qt.py | 4 +++- lib/matplotlib/tests/test_backend_tk.py | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 2158990f578a..1992cc90ca26 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3244,7 +3244,7 @@ def _update_view(self): def configure_subplots(self, *args): if hasattr(self, "subplot_tool"): self.subplot_tool.figure.canvas.manager.show() - return + return self.subplot_tool # This import needs to happen here due to circular imports. from matplotlib.figure import Figure with mpl.rc_context({"toolbar": "none"}): # No navbar for the toolfig. diff --git a/lib/matplotlib/backend_bases.pyi b/lib/matplotlib/backend_bases.pyi index 2d7b283bb4b8..0603988399f1 100644 --- a/lib/matplotlib/backend_bases.pyi +++ b/lib/matplotlib/backend_bases.pyi @@ -475,7 +475,7 @@ class NavigationToolbar2: def release_zoom(self, event: Event) -> None: ... def push_current(self) -> None: ... subplot_tool: widgets.SubplotTool - def configure_subplots(self, *args): ... + def configure_subplots(self, *args: Any) -> widgets.SubplotTool: ... def save_figure(self, *args) -> str | None | object: ... def update(self) -> None: ... def set_history_buttons(self) -> None: ... diff --git a/lib/matplotlib/tests/test_backend_qt.py b/lib/matplotlib/tests/test_backend_qt.py index 60f8a4f49bb8..a17e98d70484 100644 --- a/lib/matplotlib/tests/test_backend_qt.py +++ b/lib/matplotlib/tests/test_backend_qt.py @@ -213,7 +213,9 @@ def set_device_pixel_ratio(ratio): def test_subplottool(): fig, ax = plt.subplots() with mock.patch("matplotlib.backends.qt_compat._exec", lambda obj: None): - fig.canvas.manager.toolbar.configure_subplots() + tool = fig.canvas.manager.toolbar.configure_subplots() + assert tool is not None + assert tool == fig.canvas.manager.toolbar.configure_subplots() @pytest.mark.backend('QtAgg', skip_on_importerror=True) diff --git a/lib/matplotlib/tests/test_backend_tk.py b/lib/matplotlib/tests/test_backend_tk.py index 1210c8c9993e..1f96ad1308cb 100644 --- a/lib/matplotlib/tests/test_backend_tk.py +++ b/lib/matplotlib/tests/test_backend_tk.py @@ -168,7 +168,9 @@ def test_never_update(): plt.show(block=False) plt.draw() # Test FigureCanvasTkAgg. - fig.canvas.toolbar.configure_subplots() # Test NavigationToolbar2Tk. + tool = fig.canvas.toolbar.configure_subplots() # Test NavigationToolbar2Tk. + assert tool is not None + assert tool == fig.canvas.toolbar.configure_subplots() # Tool is reused internally. # Test FigureCanvasTk filter_destroy callback fig.canvas.get_tk_widget().after(100, plt.close, fig)