From 331af2c2e1650139975c9234f29ddb6b73dd6f59 Mon Sep 17 00:00:00 2001 From: Evgenii Radchenko Date: Mon, 15 May 2023 14:07:43 +0300 Subject: [PATCH 1/3] Adds tests for nargs_err in legend, stem, pcolorfast and cycler. --- lib/matplotlib/tests/test_axes.py | 25 +++++++++++++++++++++++++ lib/matplotlib/tests/test_rcparams.py | 8 ++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 491623e7d03a..fb184b098ef2 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -641,6 +641,31 @@ def test_sticky_shared_axes(fig_test, fig_ref): ax0.pcolormesh(Z) +def test_nargs_stem(): + with pytest.raises(TypeError) as e: + # stem() takes 1-3 arguments. + plt.stem() + assert "stem() takes 1-3" in str(e.value) + + +def test_nargs_legend(): + with pytest.raises(TypeError) as e: + ax = plt.subplot() + # legend() takes 0-2 arguments. + ax.legend(['First'], ['Second'], 3) + assert "legend() takes 0-2" in str(e.value) + + +def test_nargs_pcolorfast(): + with pytest.raises(TypeError) as e: + ax = plt.subplot() + # pcolorfast() takes 1 or 3 arguments, + # not passing any arguments fails at C = args[-1] + # before nargs_err is raised. + ax.pcolorfast([(0, 1), (0, 2)], [[1, 2, 3], [1, 2, 3]]) + assert "pcolorfast() takes 1 or 3" in str(e.value) + + @image_comparison(['offset_points'], remove_text=True) def test_basic_annotate(): # Setup some data diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index bdaf5b593a5f..c85dfb795ed5 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -118,6 +118,14 @@ def test_rcparams_init(): mpl.RcParams({'figure.figsize': (3.5, 42, 1)}) +def test_nargs_cycler(): + from matplotlib.rcsetup import cycler as ccl + with pytest.raises(TypeError) as e: + # cycler() takes 0-2 arguments. + ccl(ccl(color=list('rgb')), 2, 3) + assert "cycler() takes 0-2" in str(e.value) + + def test_Bug_2543(): # Test that it possible to add all values to itself / deepcopy # https://github.com/matplotlib/matplotlib/issues/2543 From 56d62c60a9f100d710fa63ca277c047d0e345b88 Mon Sep 17 00:00:00 2001 From: Evgenii Radchenko Date: Mon, 15 May 2023 21:30:22 +0300 Subject: [PATCH 2/3] Changed assertions to pytest.raises matches. --- lib/matplotlib/tests/test_axes.py | 9 +++------ lib/matplotlib/tests/test_rcparams.py | 3 +-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index fb184b098ef2..d6ce2e930bd0 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -642,28 +642,25 @@ def test_sticky_shared_axes(fig_test, fig_ref): def test_nargs_stem(): - with pytest.raises(TypeError) as e: + with pytest.raises(TypeError, match='0 were given') as e: # stem() takes 1-3 arguments. plt.stem() - assert "stem() takes 1-3" in str(e.value) def test_nargs_legend(): - with pytest.raises(TypeError) as e: + with pytest.raises(TypeError, match='3 were given') as e: ax = plt.subplot() # legend() takes 0-2 arguments. ax.legend(['First'], ['Second'], 3) - assert "legend() takes 0-2" in str(e.value) def test_nargs_pcolorfast(): - with pytest.raises(TypeError) as e: + with pytest.raises(TypeError, match='2 were given') as e: ax = plt.subplot() # pcolorfast() takes 1 or 3 arguments, # not passing any arguments fails at C = args[-1] # before nargs_err is raised. ax.pcolorfast([(0, 1), (0, 2)], [[1, 2, 3], [1, 2, 3]]) - assert "pcolorfast() takes 1 or 3" in str(e.value) @image_comparison(['offset_points'], remove_text=True) diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index c85dfb795ed5..92bd079bffca 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -120,10 +120,9 @@ def test_rcparams_init(): def test_nargs_cycler(): from matplotlib.rcsetup import cycler as ccl - with pytest.raises(TypeError) as e: + with pytest.raises(TypeError, match='3 were given') as e: # cycler() takes 0-2 arguments. ccl(ccl(color=list('rgb')), 2, 3) - assert "cycler() takes 0-2" in str(e.value) def test_Bug_2543(): From b554c8c2cd86f4e0405a74c4809d98698bfef1bf Mon Sep 17 00:00:00 2001 From: Evgenii Radchenko Date: Mon, 15 May 2023 21:39:55 +0300 Subject: [PATCH 3/3] Got rid of unnecessary 'raise...as' statements. --- lib/matplotlib/tests/test_axes.py | 6 +++--- lib/matplotlib/tests/test_rcparams.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d6ce2e930bd0..4e3f4a23f859 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -642,20 +642,20 @@ def test_sticky_shared_axes(fig_test, fig_ref): def test_nargs_stem(): - with pytest.raises(TypeError, match='0 were given') as e: + with pytest.raises(TypeError, match='0 were given'): # stem() takes 1-3 arguments. plt.stem() def test_nargs_legend(): - with pytest.raises(TypeError, match='3 were given') as e: + with pytest.raises(TypeError, match='3 were given'): ax = plt.subplot() # legend() takes 0-2 arguments. ax.legend(['First'], ['Second'], 3) def test_nargs_pcolorfast(): - with pytest.raises(TypeError, match='2 were given') as e: + with pytest.raises(TypeError, match='2 were given'): ax = plt.subplot() # pcolorfast() takes 1 or 3 arguments, # not passing any arguments fails at C = args[-1] diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 92bd079bffca..ce20f57ef665 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -120,7 +120,7 @@ def test_rcparams_init(): def test_nargs_cycler(): from matplotlib.rcsetup import cycler as ccl - with pytest.raises(TypeError, match='3 were given') as e: + with pytest.raises(TypeError, match='3 were given'): # cycler() takes 0-2 arguments. ccl(ccl(color=list('rgb')), 2, 3)