From c486c95f3bb8eb0852fd7202345e635ae127c439 Mon Sep 17 00:00:00 2001 From: hannah Date: Tue, 26 Sep 2023 03:18:05 -0400 Subject: [PATCH 1/3] clarify that path.sketch/validate_sketch is looking for string list of three elements --- lib/matplotlib/mpl-data/matplotlibrc | 16 ++++++++-------- lib/matplotlib/rcsetup.py | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index a097c98429d1..f606bab8c304 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -669,14 +669,14 @@ #path.snap: True # When True, rectilinear axis-aligned paths will be snapped # to the nearest pixel when certain criteria are met. # When False, paths will never be snapped. -#path.sketch: None # May be None, or a 3-tuple of the form: - # (scale, length, randomness). - # - *scale* is the amplitude of the wiggle - # perpendicular to the line (in pixels). - # - *length* is the length of the wiggle along the - # line (in pixels). - # - *randomness* is the factor by which the length is - # randomly scaled. +#path.sketch: None # May be None, or a triplet of the form: + # path.sketch: scale, length, randomness + # - *scale* is the amplitude of the wiggle + # perpendicular to the line (in pixels). + # - *length* is the length of the wiggle along the + # line (in pixels). + # - *randomness* is the factor by which the length is + # randomly scaled. #path.effects: diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index fa18677f5895..57a3859a90e7 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -562,7 +562,7 @@ def validate_sketch(s): try: return tuple(_listify_validator(validate_float, n=3)(s)) except ValueError: - raise ValueError("Expected a (scale, length, randomness) triplet") + raise ValueError("Expected a 'scale, length, randomness' triplet") def _validate_greaterthan_minushalf(s): From 36a3f7f63253a54786374f39f7e2955e8c8ee793 Mon Sep 17 00:00:00 2001 From: hannah Date: Tue, 26 Sep 2023 15:54:34 -0400 Subject: [PATCH 2/3] added tests for the sketch param validator --- lib/matplotlib/tests/test_rcparams.py | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 515068c462d4..37dd107382e0 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -27,6 +27,7 @@ validate_int, validate_markevery, validate_stringlist, + validate_sketch, _validate_linestyle, _listify_validator) @@ -628,3 +629,38 @@ def test_rcparams_legend_loc_from_file(tmpdir, value): with mpl.rc_context(fname=rc_path): assert mpl.rcParams["legend.loc"] == value + + +@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3']) +def test_validate_sketch(value): + mpl.rcParams["path.sketch"] = value + assert mpl.rcParams["path.sketch"] == (1, 2, 3) + assert validate_sketch(value) == (1, 2, 3) + + +@pytest.mark.parametrize("value", [1, '1', '(1, 2, 3)']) +def test_validate_sketch_error(value): + with pytest.raises(ValueError, match="'scale, length, randomness'"): + validate_sketch(value) + with pytest.raises(ValueError, match="'scale, length, randomness'"): + mpl.rcParams["path.sketch"] = value + + +def test_rcparams_path_sketch_from_file(tmpdir): + rc_path = tmpdir.join("matplotlibrc") + rc_path.write("path.sketch: 1, 2, 3") + + with mpl.rc_context(fname=rc_path): + assert mpl.rcParams["path.sketch"] == (1, 2, 3) + + +def test_rcparams_path_sketch_from_file_error(tmpdir, caplog): + # rcParams parser doesn't read a tuple rcfile entry + rc_path = tmpdir.join("matplotlibrc") + rc_path.write("path.sketch: (1, 2, 3)") + + with mpl.rc_context(fname=rc_path): + with caplog.at_level("WARNING"): + assert mpl.rcParams['path.sketch'] is None + assert ("Expected a 'scale, length, randomness' triplet" + in caplog.text) From eeda7055df59d07a5b10c491d1c93009a10e7bcb Mon Sep 17 00:00:00 2001 From: hannah Date: Thu, 28 Sep 2023 01:31:24 -0400 Subject: [PATCH 3/3] support tuple input for sketch Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/mpl-data/matplotlibrc | 4 ++-- lib/matplotlib/rcsetup.py | 9 ++++++--- lib/matplotlib/tests/test_rcparams.py | 26 +++++++------------------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index f606bab8c304..9bd8a622092e 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -669,8 +669,8 @@ #path.snap: True # When True, rectilinear axis-aligned paths will be snapped # to the nearest pixel when certain criteria are met. # When False, paths will never be snapped. -#path.sketch: None # May be None, or a triplet of the form: - # path.sketch: scale, length, randomness +#path.sketch: None # May be None, or a tuple of the form: + # path.sketch: (scale, length, randomness) # - *scale* is the amplitude of the wiggle # perpendicular to the line (in pixels). # - *length* is the length of the wiggle along the diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 57a3859a90e7..38d4606024d3 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -555,14 +555,17 @@ def validate_bbox(s): def validate_sketch(s): + if isinstance(s, str): - s = s.lower() + s = s.lower().strip() + if s.startswith("(") and s.endswith(")"): + s = s[1:-1] if s == 'none' or s is None: return None try: return tuple(_listify_validator(validate_float, n=3)(s)) - except ValueError: - raise ValueError("Expected a 'scale, length, randomness' triplet") + except ValueError as exc: + raise ValueError("Expected a (scale, length, randomness) tuple") from exc def _validate_greaterthan_minushalf(s): diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 37dd107382e0..65cd823f13a9 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -631,36 +631,24 @@ def test_rcparams_legend_loc_from_file(tmpdir, value): assert mpl.rcParams["legend.loc"] == value -@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3']) +@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3', '(1, 2, 3)']) def test_validate_sketch(value): mpl.rcParams["path.sketch"] = value assert mpl.rcParams["path.sketch"] == (1, 2, 3) assert validate_sketch(value) == (1, 2, 3) -@pytest.mark.parametrize("value", [1, '1', '(1, 2, 3)']) +@pytest.mark.parametrize("value", [1, '1', '1 2 3']) def test_validate_sketch_error(value): - with pytest.raises(ValueError, match="'scale, length, randomness'"): + with pytest.raises(ValueError, match="scale, length, randomness"): validate_sketch(value) - with pytest.raises(ValueError, match="'scale, length, randomness'"): + with pytest.raises(ValueError, match="scale, length, randomness"): mpl.rcParams["path.sketch"] = value -def test_rcparams_path_sketch_from_file(tmpdir): +@pytest.mark.parametrize("value", ['1, 2, 3', '(1,2,3)']) +def test_rcparams_path_sketch_from_file(tmpdir, value): rc_path = tmpdir.join("matplotlibrc") - rc_path.write("path.sketch: 1, 2, 3") - + rc_path.write(f"path.sketch: {value}") with mpl.rc_context(fname=rc_path): assert mpl.rcParams["path.sketch"] == (1, 2, 3) - - -def test_rcparams_path_sketch_from_file_error(tmpdir, caplog): - # rcParams parser doesn't read a tuple rcfile entry - rc_path = tmpdir.join("matplotlibrc") - rc_path.write("path.sketch: (1, 2, 3)") - - with mpl.rc_context(fname=rc_path): - with caplog.at_level("WARNING"): - assert mpl.rcParams['path.sketch'] is None - assert ("Expected a 'scale, length, randomness' triplet" - in caplog.text)