-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: main
Are you sure you want to change the base?
Deduplicate hatch validate #27108
Changes from 2 commits
ec34a50
69b248b
659e535
29fd872
30a4fc4
d3736ad
862d581
39eaaed
a1097f9
9ba660f
400ce9d
dca91b6
b44c281
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
import numpy as np | ||
|
||
from matplotlib import _api | ||
from matplotlib.path import Path | ||
|
||
|
||
|
@@ -186,14 +185,11 @@ def _validate_hatch_pattern(hatch): | |
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.' | ||
) | ||
message = f"""Unknown hatch symbol(s): {invalids}. | ||
Hatch must consist of a string of {valid}""" | ||
raise ValueError(message) | ||
else: | ||
raise ValueError("Hatch pattern must be a string") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The referenced issue RE: That said, it has been warning for a while now, and there is no real motion on that issue, I guess we may be able to expire this deprecation even without that. Also the original wording included the valid value of Thoughts @timhoffm? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, I had already started working on the project before and mistakenly picked an older version of the code to make changes (from last year). I will rectify this and restore all the functions that I accidentally removed. I will resubmit my pull request. I'm new to the project and hope to be able to contribute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can update your pull request by pushing additional commits (or force-pushing a rebased branch) to the same branch, please do not open new pull requests, this helps keep the conversation in one place so we can more easily see that things are addressed. |
||
|
||
|
||
def get_path(hatchpattern, density=6): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
|
||
import numpy as np | ||
|
||
from hatch import _validate_hatch_pattern | ||
from matplotlib import _api, cbook | ||
from matplotlib.cbook import ls_mapper | ||
from matplotlib.colors import Colormap, is_color_like | ||
|
@@ -179,7 +180,7 @@ def _make_type_validator(cls, *, allow_none=False): | |
|
||
def validator(s): | ||
if (allow_none and | ||
(s is None or cbook._str_lower_equal(s, "none"))): | ||
(s is None or isinstance(s, str) and s.lower() == "none")): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use the cbook method here, this seems unrelated to the stated change |
||
return None | ||
if cls is str and not isinstance(s, str): | ||
raise ValueError(f'Could not convert {s!r} to str') | ||
|
@@ -438,19 +439,6 @@ def validate_ps_distiller(s): | |
return ValidateInStrings('ps.usedistiller', ['ghostscript', 'xpdf'])(s) | ||
|
||
|
||
def _validate_papersize(s): | ||
# Re-inline this validator when the 'auto' deprecation expires. | ||
s = ValidateInStrings("ps.papersize", | ||
["figure", "auto", "letter", "legal", "ledger", | ||
*[f"{ab}{i}" for ab in "ab" for i in range(11)]], | ||
ignorecase=True)(s) | ||
if s == "auto": | ||
_api.warn_deprecated("3.8", name="ps.papersize='auto'", | ||
addendum="Pass an explicit paper type, figure, or omit " | ||
"the *ps.papersize* rcParam entirely.") | ||
return s | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removal seems unrelated to the hatch changes. And the replacement does not provide the warning that we do want to have there. |
||
# A validator dedicated to the named line styles, based on the items in | ||
# ls_mapper, and a list of possible strings read from Line2D.set_linestyle | ||
_validate_named_linestyle = ValidateInStrings( | ||
|
@@ -589,22 +577,11 @@ 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 | ||
def validate_hatch(hatch): | ||
_validate_hatch_pattern(hatch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we do just import this, I don't see much reason to define a method that just calls it... I guess these are "public", (and documented), so that could be a reason, but I would spell this as validate_hatch = _validate_hatch_pattern |
||
|
||
|
||
validate_hatchlist = _listify_validator(validate_hatch) | ||
validate_hatchlist = _listify_validator(_validate_hatch_pattern) | ||
validate_dashlist = _listify_validator(validate_floatlist) | ||
|
||
|
||
|
@@ -615,7 +592,7 @@ def _validate_minor_tick_ndivs(n): | |
two major ticks. | ||
""" | ||
|
||
if cbook._str_lower_equal(n, 'auto'): | ||
if isinstance(n, str) and n.lower() == 'auto': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, cbook method is fine and this is an unrelated change |
||
return n | ||
try: | ||
n = _validate_int_greaterequal0(n) | ||
|
@@ -751,51 +728,6 @@ def visit_Attribute(self, node): | |
self.generic_visit(node) | ||
|
||
|
||
# A validator dedicated to the named legend loc | ||
_validate_named_legend_loc = ValidateInStrings( | ||
'legend.loc', | ||
[ | ||
"best", | ||
"upper right", "upper left", "lower left", "lower right", "right", | ||
"center left", "center right", "lower center", "upper center", | ||
"center"], | ||
ignorecase=True) | ||
|
||
|
||
def _validate_legend_loc(loc): | ||
""" | ||
Confirm that loc is a type which rc.Params["legend.loc"] supports. | ||
|
||
.. versionadded:: 3.8 | ||
|
||
Parameters | ||
---------- | ||
loc : str | int | (float, float) | str((float, float)) | ||
The location of the legend. | ||
|
||
Returns | ||
------- | ||
loc : str | int | (float, float) or raise ValueError exception | ||
The location of the legend. | ||
""" | ||
if isinstance(loc, str): | ||
try: | ||
return _validate_named_legend_loc(loc) | ||
except ValueError: | ||
pass | ||
try: | ||
loc = ast.literal_eval(loc) | ||
except (SyntaxError, ValueError): | ||
pass | ||
if isinstance(loc, int): | ||
if 0 <= loc <= 10: | ||
return loc | ||
if isinstance(loc, tuple): | ||
if len(loc) == 2 and all(isinstance(e, Real) for e in loc): | ||
return loc | ||
raise ValueError(f"{loc} is not a valid legend location.") | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why were these removed?
|
||
def validate_cycler(s): | ||
"""Return a Cycler object from a string repr or the object itself.""" | ||
if isinstance(s, str): | ||
|
@@ -1094,10 +1026,8 @@ def _convert_validator_spec(key, conv): | |
"axes.ymargin": _validate_greaterthan_minushalf, # margin added to yaxis | ||
"axes.zmargin": _validate_greaterthan_minushalf, # margin added to zaxis | ||
|
||
"polaraxes.grid": validate_bool, # display polar grid or not | ||
"axes3d.grid": validate_bool, # display 3d grid | ||
"axes3d.automargin": validate_bool, # automatically add margin when | ||
# manually setting 3D axis limits | ||
"polaraxes.grid": validate_bool, # display polar grid or not | ||
"axes3d.grid": validate_bool, # display 3d grid | ||
|
||
"axes3d.xaxis.panecolor": validate_color, # 3d background pane | ||
"axes3d.yaxis.panecolor": validate_color, # 3d background pane | ||
|
@@ -1122,7 +1052,11 @@ def _convert_validator_spec(key, conv): | |
|
||
# legend properties | ||
"legend.fancybox": validate_bool, | ||
"legend.loc": _validate_legend_loc, | ||
"legend.loc": _ignorecase([ | ||
"best", | ||
"upper right", "upper left", "lower left", "lower right", "right", | ||
"center left", "center right", "lower center", "upper center", | ||
"center"]), | ||
|
||
# the number of points in the legend line | ||
"legend.numpoints": validate_int, | ||
|
@@ -1134,7 +1068,6 @@ def _convert_validator_spec(key, conv): | |
"legend.labelcolor": _validate_color_or_linecolor, | ||
# the relative size of legend markers vs. original | ||
"legend.markerscale": validate_float, | ||
# using dict in rcParams not yet supported, so make sure it is bool | ||
"legend.shadow": validate_bool, | ||
# whether or not to draw a frame around legend | ||
"legend.frameon": validate_bool, | ||
|
@@ -1229,7 +1162,6 @@ def _convert_validator_spec(key, conv): | |
"figure.autolayout": validate_bool, | ||
"figure.max_open_warning": validate_int, | ||
"figure.raise_window": validate_bool, | ||
"macosx.window_mode": ["system", "tab", "window"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why was this removed? |
||
|
||
"figure.subplot.left": validate_float, | ||
"figure.subplot.right": validate_float, | ||
|
@@ -1262,7 +1194,9 @@ def _convert_validator_spec(key, conv): | |
"tk.window_focus": validate_bool, # Maintain shell focus for TkAgg | ||
|
||
# Set the papersize/type | ||
"ps.papersize": _validate_papersize, | ||
"ps.papersize": _ignorecase(["auto", "letter", "legal", "ledger", | ||
*[f"{ab}{i}" | ||
for ab in "ab" for i in range(11)]]), | ||
"ps.useafm": validate_bool, | ||
# use ghostscript or xpdf to distill ps output | ||
"ps.usedistiller": validate_ps_distiller, | ||
|
There was a problem hiding this comment.
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.