Skip to content

Deduplicate hatch validate #27108

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 7 additions & 10 deletions lib/matplotlib/hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np

from matplotlib import _api
from matplotlib.path import Path


Expand Down Expand Up @@ -180,20 +179,18 @@ def __init__(self, hatch, density):


def _validate_hatch_pattern(hatch):
valid_hatch_patterns = set(r'-+|/\xXoO.*')
valid_hatch_patterns = set(r'-+|/\\xXoO.*')
if hatch is not None:
if not isinstance(hatch, str):
raise ValueError("Hatch pattern must be a string")
invalids = set(hatch).difference(valid_hatch_patterns)
if invalids:
valid = ''.join(sorted(valid_hatch_patterns))
invalids = ''.join(sorted(invalids))
_api.warn_deprecated(
'3.4',
removal='3.9', # one release after custom hatches (#20690)
message=f'hatch must consist of a string of "{valid}" or '
'None, but found the following invalid values '
f'"{invalids}". Passing invalid values is deprecated '
'since %(since)s and will become an error %(removal)s.'
)
Comment on lines -189 to -196
Copy link
Member

Choose a reason for hiding this comment

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

I do not think this should be removed. #20690 is not yet implemented (the idea is to completely rework the hatch handling), so no need to raise an error yet.

message = f"""Unknown hatch symbol(s): {invalids}.
Hatch must consist of a string of {valid}"""
raise ValueError(message)
return hatch


def get_path(hatchpattern, density=6):
Expand Down
15 changes: 2 additions & 13 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import numpy as np

from matplotlib.hatch import _validate_hatch_pattern
from matplotlib import _api, cbook
from matplotlib.cbook import ls_mapper
from matplotlib.colors import Colormap, is_color_like
Expand Down Expand Up @@ -592,19 +593,7 @@ def _validate_int_greaterequal0(s):
raise RuntimeError(f'Value must be >=0; got {s}')


def validate_hatch(s):
r"""
Validate a hatch pattern.
A hatch pattern string can have any sequence of the following
characters: ``\ / | - + * . x o O``.
"""
if not isinstance(s, str):
raise ValueError("Hatch pattern must be a string")
_api.check_isinstance(str, hatch_pattern=s)
unknown = set(s) - {'\\', '/', '|', '-', '+', '*', '.', 'x', 'o', 'O'}
if unknown:
raise ValueError("Unknown hatch symbol(s): %s" % list(unknown))
return s
validate_hatch = _validate_hatch_pattern


validate_hatchlist = _listify_validator(validate_hatch)
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,9 @@ def generate_validator_testcases(valid):
},
{'validator': validate_hatch,
'success': (('--|', '--|'), ('\\oO', '\\oO'),
('/+*/.x', '/+*/.x'), ('', '')),
('/+*/.xX', '/+*/.xX'), ('', '')),
'fail': (('--_', ValueError),
(8, ValueError),
('X', ValueError)),
(8, ValueError),),
},
{'validator': validate_colorlist,
'success': (('r,g,b', ['r', 'g', 'b']),
Expand Down