From d8f301644fa839c0f0ae2a438d42dbc55b4dc223 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 10 May 2024 14:04:38 -0400 Subject: [PATCH 1/2] Backport PR #28205: TST: Fix tests with older versions of ipython --- .github/workflows/tests.yml | 2 +- lib/matplotlib/testing/__init__.py | 18 +++++++----------- lib/matplotlib/testing/__init__.pyi | 3 +-- lib/matplotlib/tests/test_backend_macosx.py | 2 +- lib/matplotlib/tests/test_backend_qt.py | 2 +- 5 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a24f8cdc2f5b..126693beafa7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -60,7 +60,7 @@ jobs: - os: ubuntu-20.04 python-version: 3.9 # One CI run tests ipython/matplotlib-inline before backend mapping moved to mpl - extra-requirements: '-r requirements/testing/extra.txt "ipython<8.24" "matplotlib-inline<0.1.7"' + extra-requirements: '-r requirements/testing/extra.txt "ipython==7.19" "matplotlib-inline<0.1.7"' CFLAGS: "-fno-lto" # Ensure that disabling LTO works. # https://github.com/matplotlib/matplotlib/pull/26052#issuecomment-1574595954 # https://www.riverbankcomputing.com/pipermail/pyqt/2023-November/045606.html diff --git a/lib/matplotlib/testing/__init__.py b/lib/matplotlib/testing/__init__.py index 779149dec2dc..c3af36230d4b 100644 --- a/lib/matplotlib/testing/__init__.py +++ b/lib/matplotlib/testing/__init__.py @@ -179,11 +179,7 @@ def _has_tex_package(package): return False -def ipython_in_subprocess( - requested_backend_or_gui_framework, - expected_backend_old_ipython, # IPython < 8.24 - expected_backend_new_ipython, # IPython >= 8.24 -): +def ipython_in_subprocess(requested_backend_or_gui_framework, all_expected_backends): import pytest IPython = pytest.importorskip("IPython") @@ -194,12 +190,12 @@ def ipython_in_subprocess( requested_backend_or_gui_framework == "osx"): pytest.skip("Bug using macosx backend in IPython 8.24.0 fixed in 8.24.1") - if IPython.version_info[:2] >= (8, 24): - expected_backend = expected_backend_new_ipython - else: - # This code can be removed when Python 3.12, the latest version supported by - # IPython < 8.24, reaches end-of-life in late 2028. - expected_backend = expected_backend_old_ipython + # This code can be removed when Python 3.12, the latest version supported + # by IPython < 8.24, reaches end-of-life in late 2028. + for min_version, backend in all_expected_backends.items(): + if IPython.version_info[:2] >= min_version: + expected_backend = backend + break code = ("import matplotlib as mpl, matplotlib.pyplot as plt;" "fig, ax=plt.subplots(); ax.plot([1, 3, 2]); mpl.get_backend()") diff --git a/lib/matplotlib/testing/__init__.pyi b/lib/matplotlib/testing/__init__.pyi index b0399476b6aa..1f52a8ccb8ee 100644 --- a/lib/matplotlib/testing/__init__.pyi +++ b/lib/matplotlib/testing/__init__.pyi @@ -49,6 +49,5 @@ def _check_for_pgf(texsystem: str) -> bool: ... def _has_tex_package(package: str) -> bool: ... def ipython_in_subprocess( requested_backend_or_gui_framework: str, - expected_backend_old_ipython: str, - expected_backend_new_ipython: str, + all_expected_backends: dict[tuple[int, int], str], ) -> None: ... diff --git a/lib/matplotlib/tests/test_backend_macosx.py b/lib/matplotlib/tests/test_backend_macosx.py index a4350fe3b6c6..3041bda9f423 100644 --- a/lib/matplotlib/tests/test_backend_macosx.py +++ b/lib/matplotlib/tests/test_backend_macosx.py @@ -48,4 +48,4 @@ def new_choose_save_file(title, directory, filename): def test_ipython(): from matplotlib.testing import ipython_in_subprocess - ipython_in_subprocess("osx", "MacOSX", "macosx") + ipython_in_subprocess("osx", {(8, 24): "macosx", (7, 0): "MacOSX"}) diff --git a/lib/matplotlib/tests/test_backend_qt.py b/lib/matplotlib/tests/test_backend_qt.py index a105b88c7449..12d3238e0af4 100644 --- a/lib/matplotlib/tests/test_backend_qt.py +++ b/lib/matplotlib/tests/test_backend_qt.py @@ -376,4 +376,4 @@ def custom_handler(signum, frame): def test_ipython(): from matplotlib.testing import ipython_in_subprocess - ipython_in_subprocess("qt", "QtAgg", "qtagg") + ipython_in_subprocess("qt", {(8, 24): "qtagg", (8, 15): "QtAgg", (7, 0): "Qt5Agg"}) From 196c8db2074c9a3d91a9e144c21f7ef204905988 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 10 May 2024 14:18:56 -0400 Subject: [PATCH 2/2] TST: Followup corrections to #28205 Make the changes suggested by @ianthomas23, and also mark the ipython tests as using their backend. While the backend is already checked for availability at the top of the respective files, that only checks whether it can be imported, not whether it can be set as the Matplotlib backend. Adding the marker causes our pytest configuration to actually check and skip the test if unavailable (e.g., on Linux without `(WAYLAND_)DISPLAY` set fails to set an interactive backend). --- lib/matplotlib/testing/__init__.py | 2 +- lib/matplotlib/tests/test_backend_inline.py | 2 ++ lib/matplotlib/tests/test_backend_macosx.py | 1 + lib/matplotlib/tests/test_backend_qt.py | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/testing/__init__.py b/lib/matplotlib/testing/__init__.py index c3af36230d4b..8e60267ed608 100644 --- a/lib/matplotlib/testing/__init__.py +++ b/lib/matplotlib/testing/__init__.py @@ -210,4 +210,4 @@ def ipython_in_subprocess(requested_backend_or_gui_framework, all_expected_backe capture_output=True, ) - assert proc.stdout.strip() == f"Out[1]: '{expected_backend}'" + assert proc.stdout.strip().endswith(f"'{expected_backend}'") diff --git a/lib/matplotlib/tests/test_backend_inline.py b/lib/matplotlib/tests/test_backend_inline.py index 6f0d67d51756..4112eb213e2c 100644 --- a/lib/matplotlib/tests/test_backend_inline.py +++ b/lib/matplotlib/tests/test_backend_inline.py @@ -1,6 +1,7 @@ import os from pathlib import Path from tempfile import TemporaryDirectory +import sys import pytest @@ -12,6 +13,7 @@ pytest.importorskip('matplotlib_inline') +@pytest.mark.skipif(sys.version_info[:2] <= (3, 9), reason="Requires Python 3.10+") def test_ipynb(): nb_path = Path(__file__).parent / 'test_inline_01.ipynb' diff --git a/lib/matplotlib/tests/test_backend_macosx.py b/lib/matplotlib/tests/test_backend_macosx.py index 3041bda9f423..7431481de8ae 100644 --- a/lib/matplotlib/tests/test_backend_macosx.py +++ b/lib/matplotlib/tests/test_backend_macosx.py @@ -46,6 +46,7 @@ def new_choose_save_file(title, directory, filename): assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test" +@pytest.mark.backend('macosx') def test_ipython(): from matplotlib.testing import ipython_in_subprocess ipython_in_subprocess("osx", {(8, 24): "macosx", (7, 0): "MacOSX"}) diff --git a/lib/matplotlib/tests/test_backend_qt.py b/lib/matplotlib/tests/test_backend_qt.py index 12d3238e0af4..5eb1ea77554d 100644 --- a/lib/matplotlib/tests/test_backend_qt.py +++ b/lib/matplotlib/tests/test_backend_qt.py @@ -374,6 +374,7 @@ def custom_handler(signum, frame): signal.signal(signal.SIGINT, original_handler) +@pytest.mark.backend('QtAgg', skip_on_importerror=True) def test_ipython(): from matplotlib.testing import ipython_in_subprocess ipython_in_subprocess("qt", {(8, 24): "qtagg", (8, 15): "QtAgg", (7, 0): "Qt5Agg"})