Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ENH: Stricter validation of line style rcParams (and extended accepted types for grid.linestyle) #8040

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

Merged
merged 15 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from 7 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
67 changes: 40 additions & 27 deletions 67 lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import warnings
import re

from matplotlib.cbook import mplDeprecation
from matplotlib.cbook import mplDeprecation, ls_mapper
from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
from matplotlib.colors import is_color_like

Expand Down Expand Up @@ -530,22 +530,6 @@ def validate_ps_distiller(s):
'top', 'none'])
validate_fillstylelist = _listify_validator(validate_fillstyle)

validate_negative_linestyle = ValidateInStrings('negative_linestyle',
['solid', 'dashed'],
ignorecase=True)


def validate_negative_linestyle_legacy(s):
try:
res = validate_negative_linestyle(s)
return res
except ValueError:
dashes = validate_nseq_float(2)(s)
warnings.warn("Deprecated negative_linestyle specification; use "
"'solid' or 'dashed'",
mplDeprecation)
return (0, dashes) # (offset, (solid, blank))


def validate_corner_mask(s):
if s == 'legacy':
Expand Down Expand Up @@ -888,6 +872,36 @@ def validate_animation_writer_path(p):
modules["matplotlib.animation"].writers.set_dirty()
return p

# 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('linestyle',
Copy link
Member

Choose a reason for hiding this comment

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

same here. this should not appear in our public API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I'll do that! (I was hesitating because most of the other validators are public).

Speaking of public API, I think I've done something wrong (:sheep:) by genuinely getting rid off of validate_negative_linestyle and validate_negative_linestyle_legacy as they were both part of the public API. I should have used some deprecation warning instead, shouldn't I? Or at least just leave them (because if validate_linestyle is made private, there will be no alternative)?

list(six.iterkeys(ls_mapper)) +
list(six.itervalues(ls_mapper)) +
['None', 'none', ' ', ''],
ignorecase=False)

# A validator for all possible line styles, the named ones *and*
# the on-off ink sequences.
def validate_linestyle(ls):
Copy link
Member

Choose a reason for hiding this comment

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

can we make this a private function? I don't think it should be added to our public API.

# Named line style, like u'--' or u'solid'
if isinstance(ls, six.text_type):
return validate_named_linestyle(ls)

# On-off ink (in points) sequence *of even length*.
# Offset is set to None.
try:
if len(ls) % 2 != 0:
# Expecting a sequence of even length
raise ValueError
return (None, validate_nseq_float()(ls))
except (ValueError, TypeError):
# TypeError can be raised by wrong types passed to float()
# (called inside the instance of validate_nseq_float).
pass

raise ValueError("linestyle must be a string or " +
"an even-length sequence of floats.")


# a map from key -> value, converter
defaultParams = {
Expand All @@ -912,7 +926,7 @@ def validate_animation_writer_path(p):

# line props
'lines.linewidth': [1.5, validate_float], # line width in points
'lines.linestyle': ['-', six.text_type], # solid line
'lines.linestyle': ['-', validate_linestyle], # solid line
'lines.color': ['C0', validate_color], # first color in color cycle
'lines.marker': ['None', six.text_type], # marker name
'lines.markeredgewidth': [1.0, validate_float],
Expand Down Expand Up @@ -961,31 +975,31 @@ def validate_animation_writer_path(p):
'boxplot.flierprops.markerfacecolor': ['none', validate_color_or_auto],
'boxplot.flierprops.markeredgecolor': ['k', validate_color],
'boxplot.flierprops.markersize': [6, validate_float],
'boxplot.flierprops.linestyle': ['none', six.text_type],
'boxplot.flierprops.linestyle': ['none', validate_linestyle],
'boxplot.flierprops.linewidth': [1.0, validate_float],

'boxplot.boxprops.color': ['k', validate_color],
'boxplot.boxprops.linewidth': [1.0, validate_float],
'boxplot.boxprops.linestyle': ['-', six.text_type],
'boxplot.boxprops.linestyle': ['-', validate_linestyle],

'boxplot.whiskerprops.color': ['k', validate_color],
'boxplot.whiskerprops.linewidth': [1.0, validate_float],
'boxplot.whiskerprops.linestyle': ['-', six.text_type],
'boxplot.whiskerprops.linestyle': ['-', validate_linestyle],

'boxplot.capprops.color': ['k', validate_color],
'boxplot.capprops.linewidth': [1.0, validate_float],
'boxplot.capprops.linestyle': ['-', six.text_type],
'boxplot.capprops.linestyle': ['-', validate_linestyle],

'boxplot.medianprops.color': ['C1', validate_color],
'boxplot.medianprops.linewidth': [1.0, validate_float],
'boxplot.medianprops.linestyle': ['-', six.text_type],
'boxplot.medianprops.linestyle': ['-', validate_linestyle],

'boxplot.meanprops.color': ['C2', validate_color],
'boxplot.meanprops.marker': ['^', six.text_type],
'boxplot.meanprops.markerfacecolor': ['C2', validate_color],
'boxplot.meanprops.markeredgecolor': ['C2', validate_color],
'boxplot.meanprops.markersize': [6, validate_float],
'boxplot.meanprops.linestyle': ['--', six.text_type],
'boxplot.meanprops.linestyle': ['--', validate_linestyle],
'boxplot.meanprops.linewidth': [1.0, validate_float],

## font props
Expand Down Expand Up @@ -1051,8 +1065,7 @@ def validate_animation_writer_path(p):
'image.composite_image': [True, validate_bool],

# contour props
'contour.negative_linestyle': ['dashed',
validate_negative_linestyle_legacy],
'contour.negative_linestyle': ['dashed', validate_linestyle],
'contour.corner_mask': [True, validate_corner_mask],

# errorbar props
Expand Down Expand Up @@ -1215,7 +1228,7 @@ def validate_animation_writer_path(p):
'ytick.direction': ['out', six.text_type], # direction of yticks

'grid.color': ['#b0b0b0', validate_color], # grid color
'grid.linestyle': ['-', six.text_type], # solid
'grid.linestyle': ['-', validate_linestyle], # solid
'grid.linewidth': [0.8, validate_float], # in points
'grid.alpha': [1.0, validate_float],

Expand Down
23 changes: 22 additions & 1 deletion 23 lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
validate_nseq_float,
validate_cycler,
validate_hatch,
validate_hist_bins)
validate_hist_bins,
validate_linestyle)


mpl.rc('text', usetex=False)
Expand Down Expand Up @@ -333,6 +334,26 @@ def generate_validator_testcases(valid):
),
'fail': (('aardvark', ValueError),
)
},
{'validator': validate_linestyle,
'success': (('-', '-'), ('solid', 'solid'),
('--', '--'), ('dashed', 'dashed'),
('-.', '-.'), ('dashdot', 'dashdot'),
(':', ':'), ('dotted', 'dotted'),
('', ''), (' ', ' '),
('None', 'None'), ('none', 'none'),
(['1.23', '4.56'], (None, [1.23, 4.56])),
([1.23, 456], (None, [1.23, 456.0])),
([1, 2, 3, 4], (None, [1.0, 2.0, 3.0, 4.0])),
),
'fail': (('aardvark', ValueError), # not a valid string
('DoTtEd', ValueError), # validation is case-sensitive
((None, [1, 2]), ValueError), # (offset, dashes) is not OK
((0, [1, 2]), ValueError), # idem
((-1, [1, 2]), ValueError), # idem
([1, 2, 3], ValueError), # not a sequence of even length
(1.23, ValueError) # not a sequence
)
}
)

Expand Down
2 changes: 1 addition & 1 deletion 2 matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ backend : $TEMPLATE_BACKEND
# such as a PDF.

### CONTOUR PLOTS
#contour.negative_linestyle : dashed # dashed | solid
#contour.negative_linestyle : dashed # string or on-off ink sequence
#contour.corner_mask : True # True | False | legacy

### ERRORBAR PLOTS
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.