Skip to content

Fix testing of whether backends use the new pyplot_show API. #24527

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
Nov 23, 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
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/removals/XXXXX-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``backend_template.show``
~~~~~~~~~~~~~~~~~~~~~~~~~
... has been removed, in order to better demonstrate the new backend definition
API.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like both me and @timhoffm missed the name of this file...

16 changes: 3 additions & 13 deletions lib/matplotlib/backends/backend_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,13 @@ class GraphicsContextTemplate(GraphicsContextBase):
########################################################################


def show(*, block=None):
"""
For image backends - is not required.
For GUI backends - show() is usually the last line of a pyplot script and
tells the backend that it is time to draw. In interactive mode, this
should do nothing.
"""
for manager in Gcf.get_all_fig_managers():
# do something to display the GUI
pass


class FigureManagerTemplate(FigureManagerBase):
"""
Helper class for pyplot mode, wraps everything up into a neat bundle.

For non-interactive backends, the base class is sufficient.
For non-interactive backends, the base class is sufficient. For
interactive backends, see the documentation of the `.FigureManagerBase`
class for the list of methods that can/should be overridden.
"""


Expand Down
10 changes: 8 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,14 @@ def draw_if_interactive():
# show is already present, as the latter may be here for backcompat.
manager_class = getattr(getattr(backend_mod, "FigureCanvas", None),
"manager_class", None)
if (manager_class.pyplot_show != FigureManagerBase.pyplot_show
or show is 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, & thus always different (being bound to different classes).
manager_pyplot_show = vars(manager_class).get("pyplot_show")
base_pyplot_show = vars(FigureManagerBase).get("pyplot_show")
if (show is None
or (manager_pyplot_show is not None
and manager_pyplot_show != base_pyplot_show)):
backend_mod.show = manager_class.pyplot_show

_log.debug("Loaded backend %s version %s.",
Expand Down
16 changes: 13 additions & 3 deletions lib/matplotlib/tests/test_backend_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ def test_load_old_api(monkeypatch):

def test_show(monkeypatch):
mpl_test_backend = SimpleNamespace(**vars(backend_template))
mock_show = backend_template.FigureManagerTemplate.pyplot_show = \
MagicMock()
del mpl_test_backend.show
mock_show = MagicMock()
monkeypatch.setattr(
mpl_test_backend.FigureManagerTemplate, "pyplot_show", mock_show)
monkeypatch.setitem(sys.modules, "mpl_test_backend", mpl_test_backend)
mpl.use("module://mpl_test_backend")
plt.show()
mock_show.assert_called_with()


def test_show_old_global_api(monkeypatch):
mpl_test_backend = SimpleNamespace(**vars(backend_template))
mock_show = MagicMock()
monkeypatch.setattr(mpl_test_backend, "show", mock_show, raising=False)
monkeypatch.setitem(sys.modules, "mpl_test_backend", mpl_test_backend)
mpl.use("module://mpl_test_backend")
plt.show()
Expand Down