Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b32a06a

Browse files
committed
Deprecate most ValidateInStrings validators.
We can just list the allowed string values in the defaultParams dict (which is actually clearer) and then dynamically generate the corresponding validators; no need to expose them as separate public API (which is not particularly useful anyways because how can you know that it's `validate_fontset` for `mathtext.fontset`, but `validate_mathtext_default` for `mathtext.default`?).
1 parent a0eb716 commit b32a06a

File tree

2 files changed

+68
-44
lines changed

2 files changed

+68
-44
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,14 @@ The *clear_temp* parameter and attribute of `.FileMovieWriter` is
211211
deprecated. In the future, files placed in a temporary directory (using
212212
``frame_prefix=None``, the default) will be cleared; files placed elsewhere
213213
will not.
214+
215+
Deprecated rcParams validators
216+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217+
The following validators, defined in `.rcsetup`, are deprecated:
218+
``validate_fontset``, ``validate_mathtext_default``, ``validate_alignment``,
219+
``validate_svg_fontset``, ``validate_pgf_texsystem``,
220+
``validate_movie_frame_fmt``, ``validate_axis_locator``,
221+
``validate_movie_html_fmt``, ``validate_grid_axis``,
222+
``validate_axes_titlelocation``. To test whether an rcParam value would
223+
be acceptable, one can test e.g. ``rc = RcParams(); rc[k] = v`` raises an
224+
exception.

lib/matplotlib/rcsetup.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@
5050

5151

5252
class ValidateInStrings:
53-
def __init__(self, key, valid, ignorecase=False):
53+
def __init__(self, key, valid, ignorecase=False, *,
54+
_deprecated_since=None):
5455
"""*valid* is a list of legal strings."""
5556
self.key = key
5657
self.ignorecase = ignorecase
58+
self._deprecated_since = _deprecated_since
5759

5860
def func(s):
5961
if ignorecase:
@@ -63,6 +65,10 @@ def func(s):
6365
self.valid = {func(k): k for k in valid}
6466

6567
def __call__(self, s):
68+
if self._deprecated_since:
69+
name, = (k for k, v in globals().items() if v is self)
70+
cbook.warn_deprecated(
71+
self._deprecated_since, name=name, obj_type="function")
6672
if self.ignorecase:
6773
s = s.lower()
6874
if s in self.valid:
@@ -365,7 +371,7 @@ def validate_color(s):
365371
validate_colorlist = _listify_validator(
366372
validate_color, allow_stringlist=True, doc='return a list of colorspecs')
367373
validate_orientation = ValidateInStrings(
368-
'orientation', ['landscape', 'portrait'])
374+
'orientation', ['landscape', 'portrait'], _deprecated_since="3.3")
369375

370376

371377
def validate_aspect(s):
@@ -421,15 +427,15 @@ def validate_font_properties(s):
421427

422428
validate_fontset = ValidateInStrings(
423429
'fontset',
424-
['dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans', 'custom'])
430+
['dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans', 'custom'],
431+
_deprecated_since="3.3")
425432
validate_mathtext_default = ValidateInStrings(
426-
'default', "rm cal it tt sf bf default bb frak scr regular".split())
427-
428-
433+
'default', "rm cal it tt sf bf default bb frak scr regular".split(),
434+
_deprecated_since="3.3")
429435
_validate_alignment = ValidateInStrings(
430436
'alignment',
431-
['center', 'top', 'bottom', 'baseline',
432-
'center_baseline'])
437+
['center', 'top', 'bottom', 'baseline', 'center_baseline'],
438+
_deprecated_since="3.3")
433439

434440

435441
def validate_whiskers(s):
@@ -648,7 +654,8 @@ def validate_markevery(s):
648654
'upper center',
649655
'center'], ignorecase=True)
650656

651-
validate_svg_fonttype = ValidateInStrings('svg.fonttype', ['none', 'path'])
657+
validate_svg_fonttype = ValidateInStrings(
658+
'svg.fonttype', ['none', 'path'], _deprecated_since="3.3")
652659

653660

654661
def validate_hinting(s):
@@ -663,8 +670,9 @@ def validate_hinting(s):
663670
raise ValueError("hinting should be 'auto', 'native', 'either' or 'none'")
664671

665672

666-
validate_pgf_texsystem = ValidateInStrings('pgf.texsystem',
667-
['xelatex', 'lualatex', 'pdflatex'])
673+
validate_pgf_texsystem = ValidateInStrings(
674+
'pgf.texsystem', ['xelatex', 'lualatex', 'pdflatex'],
675+
_deprecated_since="3.3")
668676

669677

670678
def validate_movie_writer(s):
@@ -678,12 +686,13 @@ def validate_movie_writer(s):
678686

679687

680688
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
681-
['png', 'jpeg', 'tiff', 'raw', 'rgba'])
689+
['png', 'jpeg', 'tiff', 'raw', 'rgba'], _deprecated_since="3.3")
682690

683-
validate_axis_locator = ValidateInStrings('major', ['minor', 'both', 'major'])
691+
validate_axis_locator = ValidateInStrings(
692+
'major', ['minor', 'both', 'major'], _deprecated_since="3.3")
684693

685694
validate_movie_html_fmt = ValidateInStrings('animation.html',
686-
['html5', 'jshtml', 'none'])
695+
['html5', 'jshtml', 'none'], _deprecated_since="3.3")
687696

688697

689698
def validate_bbox(s):
@@ -736,7 +745,8 @@ def _validate_greaterequal0_lessequal1(s):
736745
}
737746

738747

739-
validate_grid_axis = ValidateInStrings('axes.grid.axis', ['x', 'y', 'both'])
748+
validate_grid_axis = ValidateInStrings(
749+
'axes.grid.axis', ['x', 'y', 'both'], _deprecated_since="3.3")
740750

741751

742752
def validate_hatch(s):
@@ -984,15 +994,13 @@ def validate_webagg_address(s):
984994
raise ValueError("'webagg.address' is not a valid IP address")
985995

986996

987-
validate_axes_titlelocation = ValidateInStrings('axes.titlelocation',
988-
['left', 'center', 'right'])
989-
_validate_xaxis_labellocation = ValidateInStrings('xaxis.labellocation',
990-
['left', 'center', 'right'])
991-
_validate_yaxis_labellocation = ValidateInStrings('yaxis.labellocation',
992-
['bottom', 'center', 'top'])
997+
validate_axes_titlelocation = ValidateInStrings(
998+
'axes.titlelocation', ['left', 'center', 'right'], _deprecated_since="3.3")
993999

9941000

995-
# a map from key -> value, converter
1001+
# A map of key -> [value, converter].
1002+
# Converters given as lists are converted to ValidateInStrings immediately
1003+
# below.
9961004
defaultParams = {
9971005
'backend': [_auto_backend_sentinel, validate_backend],
9981006
'backend_fallback': [True, validate_bool],
@@ -1137,16 +1145,19 @@ def validate_webagg_address(s):
11371145
'mathtext.it': ['sans:italic', validate_font_properties],
11381146
'mathtext.bf': ['sans:bold', validate_font_properties],
11391147
'mathtext.sf': ['sans', validate_font_properties],
1140-
'mathtext.fontset': ['dejavusans', validate_fontset],
1141-
'mathtext.default': ['it', validate_mathtext_default],
1148+
'mathtext.fontset': [
1149+
'dejavusans',
1150+
['dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans', 'custom']],
1151+
'mathtext.default': [
1152+
'it',
1153+
['rm', 'cal', 'it', 'tt', 'sf', 'bf', 'default', 'bb', 'frak', 'scr', 'regular']],
11421154
'mathtext.fallback_to_cm': [True, validate_bool],
11431155

11441156
'image.aspect': ['equal', validate_aspect], # equal, auto, a number
11451157
'image.interpolation': ['antialiased', validate_string],
11461158
'image.cmap': ['viridis', validate_string], # gray, jet, etc.
11471159
'image.lut': [256, validate_int], # lookup table
1148-
'image.origin': ['upper',
1149-
ValidateInStrings('image.origin', ['upper', 'lower'])],
1160+
'image.origin': ['upper', ['upper', 'lower']],
11501161
'image.resample': [True, validate_bool],
11511162
# Specify whether vector graphics backends will combine all images on a
11521163
# set of axes into a single composite image
@@ -1160,8 +1171,8 @@ def validate_webagg_address(s):
11601171
'errorbar.capsize': [0, validate_float],
11611172

11621173
# axis props
1163-
'xaxis.labellocation': ['center', _validate_xaxis_labellocation], # alignment of x axis title
1164-
'yaxis.labellocation': ['center', _validate_yaxis_labellocation], # alignment of y axis title
1174+
'xaxis.labellocation': ['center', ['left', 'center', 'right']], # alignment of x axis title
1175+
'yaxis.labellocation': ['center', ['bottom', 'center', 'top']], # alignment of y axis title
11651176

11661177
# axes props
11671178
'axes.axisbelow': ['line', validate_axisbelow],
@@ -1176,16 +1187,14 @@ def validate_webagg_address(s):
11761187

11771188
'axes.titlesize': ['large', validate_fontsize], # fontsize of the
11781189
# axes title
1179-
'axes.titlelocation': ['center', validate_axes_titlelocation], # alignment of axes title
1190+
'axes.titlelocation': ['center', ['left', 'center', 'right']], # alignment of axes title
11801191
'axes.titleweight': ['normal', validate_fontweight], # font weight of axes title
11811192
'axes.titlecolor': ['auto', validate_color_or_auto], # font color of axes title
11821193
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
11831194
'axes.grid': [False, validate_bool], # display grid or not
1184-
'axes.grid.which': ['major', validate_axis_locator], # set whether the gid are by
1185-
# default draw on 'major'
1186-
# 'minor' or 'both' kind of
1187-
# axis locator
1188-
'axes.grid.axis': ['both', validate_grid_axis], # grid type:
1195+
'axes.grid.which': ['major', ['minor', 'both', 'major']], # set whether the grid is drawn on
1196+
# 'major' 'minor' or 'both' ticks
1197+
'axes.grid.axis': ['both', ['x', 'y', 'both']], # grid type:
11891198
# 'x', 'y', or 'both'
11901199
'axes.labelsize': ['medium', validate_fontsize], # fontsize of the
11911200
# x any y labels
@@ -1214,9 +1223,7 @@ def validate_webagg_address(s):
12141223
validate_cycler],
12151224
# If 'data', axes limits are set close to the data.
12161225
# If 'round_numbers' axes limits are set to the nearest round numbers.
1217-
'axes.autolimit_mode': [
1218-
'data',
1219-
ValidateInStrings('autolimit_mode', ['data', 'round_numbers'])],
1226+
'axes.autolimit_mode': ['data', ['data', 'round_numbers']],
12201227
'axes.xmargin': [0.05, _range_validators["0 <= x <= 1"]],
12211228
'axes.ymargin': [0.05, _range_validators["0 <= x <= 1"]],
12221229

@@ -1291,7 +1298,8 @@ def validate_webagg_address(s):
12911298
# fontsize of the xtick labels
12921299
'xtick.labelsize': ['medium', validate_fontsize],
12931300
'xtick.direction': ['out', validate_string], # direction of xticks
1294-
'xtick.alignment': ["center", _validate_alignment],
1301+
'xtick.alignment': ['center',
1302+
['center', 'top', 'bottom', 'baseline', 'center_baseline']],
12951303

12961304
'ytick.left': [True, validate_bool], # draw ticks on the left side
12971305
'ytick.right': [False, validate_bool], # draw ticks on the right side
@@ -1313,7 +1321,8 @@ def validate_webagg_address(s):
13131321
# fontsize of the ytick labels
13141322
'ytick.labelsize': ['medium', validate_fontsize],
13151323
'ytick.direction': ['out', validate_string], # direction of yticks
1316-
'ytick.alignment': ["center_baseline", _validate_alignment],
1324+
'ytick.alignment': ['center_baseline',
1325+
['center', 'top', 'bottom', 'baseline', 'center_baseline']],
13171326

13181327
'grid.color': ['#b0b0b0', validate_color], # grid color
13191328
'grid.linestyle': ['-', _validate_linestyle], # solid
@@ -1359,7 +1368,7 @@ def validate_webagg_address(s):
13591368
'savefig.dpi': ['figure', validate_dpi], # DPI
13601369
'savefig.facecolor': ['white', validate_color],
13611370
'savefig.edgecolor': ['white', validate_color],
1362-
'savefig.orientation': ['portrait', validate_orientation],
1371+
'savefig.orientation': ['portrait', ['landscape', 'portrait']],
13631372
'savefig.jpeg_quality': [95, validate_int],
13641373
# value checked by backend at runtime
13651374
'savefig.format': ['png', _update_savefig_format],
@@ -1389,7 +1398,7 @@ def validate_webagg_address(s):
13891398
'pdf.fonttype': [3, validate_fonttype], # 3 (Type3) or 42 (Truetype)
13901399

13911400
# choose latex application for creating pdf files (xelatex/lualatex)
1392-
'pgf.texsystem': ['xelatex', validate_pgf_texsystem],
1401+
'pgf.texsystem': ['xelatex', ['xelatex', 'lualatex', 'pdflatex']],
13931402
# use matplotlib rc settings for font configuration
13941403
'pgf.rcfonts': [True, validate_bool],
13951404
# provide a custom preamble for the latex process
@@ -1398,7 +1407,7 @@ def validate_webagg_address(s):
13981407
# write raster image data directly into the svg file
13991408
'svg.image_inline': [True, validate_bool],
14001409
# True to save all characters as paths in the SVG
1401-
'svg.fonttype': ['path', validate_svg_fonttype],
1410+
'svg.fonttype': ['path', ['none', 'path']],
14021411
'svg.hashsalt': [None, validate_string_or_None],
14031412

14041413
# set this when you want to generate hardcopy docstring
@@ -1432,15 +1441,15 @@ def validate_webagg_address(s):
14321441
'keymap.copy': [['ctrl+c', 'cmd+c'], validate_stringlist],
14331442

14341443
# Animation settings
1435-
'animation.html': ['none', validate_movie_html_fmt],
1444+
'animation.html': ['none', ['html5', 'jshtml', 'none']],
14361445
# Limit, in MB, of size of base64 encoded animation in HTML
14371446
# (i.e. IPython notebook)
14381447
'animation.embed_limit': [20, validate_float],
14391448
'animation.writer': ['ffmpeg', validate_movie_writer],
14401449
'animation.codec': ['h264', validate_string],
14411450
'animation.bitrate': [-1, validate_int],
14421451
# Controls image format when frames are written to disk
1443-
'animation.frame_format': ['png', validate_movie_frame_fmt],
1452+
'animation.frame_format': ['png', ['png', 'jpeg', 'tiff', 'raw', 'rgba']],
14441453
# Additional arguments for HTML writer
14451454
'animation.html_args': [[], validate_stringlist],
14461455
# Path to ffmpeg binary. If just binary name, subprocess uses $PATH.
@@ -1464,3 +1473,7 @@ def validate_webagg_address(s):
14641473
# altogether. For that use `matplotlib.style.use('classic')`.
14651474
'_internal.classic_mode': [False, validate_bool]
14661475
}
1476+
defaultParams = {
1477+
k: [default,
1478+
ValidateInStrings(k, conv) if isinstance(conv, list) else conv]
1479+
for k, (default, conv) in defaultParams.items()}

0 commit comments

Comments
 (0)