Skip to content

Commit

Permalink
New tests support old and new ipython
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 committed Apr 10, 2024
1 parent acf737c commit 579f415
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
14 changes: 11 additions & 3 deletions lib/matplotlib/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,16 @@ def _has_tex_package(package):
return False


def ipython_in_subprocess(requested_backend_or_gui_framework, expected_backend):
pytest.importorskip("IPython")
def ipython_in_subprocess(
requested_backend_or_gui_framework,
expected_backend_old_ipython, # IPython < 8.24
expected_backend_new_ipython, # IPython >= 8.24
):
IPython = pytest.importorskip("IPython")
if IPython.version_info[:2] >= (8, 24):
expected_backend = expected_backend_new_ipython
else:
expected_backend = expected_backend_old_ipython

code = ("import matplotlib as mpl, matplotlib.pyplot as plt;"
"fig, ax=plt.subplots(); ax.plot([1, 3, 2]); mpl.get_backend()")
Expand All @@ -196,5 +204,5 @@ def ipython_in_subprocess(requested_backend_or_gui_framework, expected_backend):
check=True,
capture_output=True,
)

assert proc.stdout.strip() == f"Out[1]: '{expected_backend}'"
3 changes: 2 additions & 1 deletion lib/matplotlib/testing/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ 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: str,
expected_backend_old_ipython: str,
expected_backend_new_ipython: str,
) -> None: ...
9 changes: 7 additions & 2 deletions lib/matplotlib/tests/test_backend_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ def test_ipynb():
errors = [output for cell in nb.cells for output in cell.get("outputs", [])
if output.output_type == "error"]
assert not errors


import IPython
if IPython.version_info[:2] >= (8, 24):
expected_backend = "inline"
else:
expected_backend = "module://matplotlib_inline.backend_inline"
backend_outputs = nb.cells[2]["outputs"]
assert backend_outputs[0]["data"]["text/plain"] == "'inline'"
assert backend_outputs[0]["data"]["text/plain"] == f"'{expected_backend}'"

image = nb.cells[1]["outputs"][1]["data"]
assert image["text/plain"] == "<Figure size 300x200 with 1 Axes>"
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
ipython_in_subprocess("osx", "MacOSX", "macosx")
9 changes: 7 additions & 2 deletions lib/matplotlib/tests/test_backend_nbagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def test_ipynb():
errors = [output for cell in nb.cells for output in cell.get("outputs", [])
if output.output_type == "error"]
assert not errors


import IPython
if IPython.version_info[:2] >= (8, 24):
expected_backend = "notebook"
else:
expected_backend = "nbAgg"
backend_outputs = nb.cells[2]["outputs"]
assert backend_outputs[0]["data"]["text/plain"] == "'notebook'"
assert backend_outputs[0]["data"]["text/plain"] == f"'{expected_backend}'"
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,4 @@ def custom_handler(signum, frame):

def test_ipython():
from matplotlib.testing import ipython_in_subprocess
ipython_in_subprocess("qt", "qtagg")
ipython_in_subprocess("qt", "QtAgg", "qtagg")
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,4 @@ def test_figure(master):

def test_ipython():
from matplotlib.testing import ipython_in_subprocess
ipython_in_subprocess("tk", "tkagg")
ipython_in_subprocess("tk", "TkAgg", "tkagg")

0 comments on commit 579f415

Please sign in to comment.