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

Skip to content

Commit dea3d3a

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 dea3d3a

File tree

2 files changed

+70
-43
lines changed

2 files changed

+70
-43
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: 59 additions & 43 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,12 +994,8 @@ 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

9951001
# a map from key -> value, converter
@@ -1137,16 +1143,19 @@ def validate_webagg_address(s):
11371143
'mathtext.it': ['sans:italic', validate_font_properties],
11381144
'mathtext.bf': ['sans:bold', validate_font_properties],
11391145
'mathtext.sf': ['sans', validate_font_properties],
1140-
'mathtext.fontset': ['dejavusans', validate_fontset],
1141-
'mathtext.default': ['it', validate_mathtext_default],
1146+
'mathtext.fontset': [
1147+
'dejavusans',
1148+
['dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans', 'custom']],
1149+
'mathtext.default': [
1150+
'it',
1151+
['rm', 'cal', 'it', 'tt', 'sf', 'bf', 'default', 'bb', 'frak', 'scr', 'regular']],
11421152
'mathtext.fallback_to_cm': [True, validate_bool],
11431153

11441154
'image.aspect': ['equal', validate_aspect], # equal, auto, a number
11451155
'image.interpolation': ['antialiased', validate_string],
11461156
'image.cmap': ['viridis', validate_string], # gray, jet, etc.
11471157
'image.lut': [256, validate_int], # lookup table
1148-
'image.origin': ['upper',
1149-
ValidateInStrings('image.origin', ['upper', 'lower'])],
1158+
'image.origin': ['upper', ['upper', 'lower']],
11501159
'image.resample': [True, validate_bool],
11511160
# Specify whether vector graphics backends will combine all images on a
11521161
# set of axes into a single composite image
@@ -1160,8 +1169,8 @@ def validate_webagg_address(s):
11601169
'errorbar.capsize': [0, validate_float],
11611170

11621171
# 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
1172+
'xaxis.labellocation': ['center', ['left', 'center', 'right']], # alignment of x axis title
1173+
'yaxis.labellocation': ['center', ['bottom', 'center', 'top']], # alignment of y axis title
11651174

11661175
# axes props
11671176
'axes.axisbelow': ['line', validate_axisbelow],
@@ -1176,16 +1185,14 @@ def validate_webagg_address(s):
11761185

11771186
'axes.titlesize': ['large', validate_fontsize], # fontsize of the
11781187
# axes title
1179-
'axes.titlelocation': ['center', validate_axes_titlelocation], # alignment of axes title
1188+
'axes.titlelocation': ['center', ['left', 'center', 'right']], # alignment of axes title
11801189
'axes.titleweight': ['normal', validate_fontweight], # font weight of axes title
11811190
'axes.titlecolor': ['auto', validate_color_or_auto], # font color of axes title
11821191
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
11831192
'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:
1193+
'axes.grid.which': ['major', ['minor', 'both', 'major']], # set whether the grid is drawn on
1194+
# 'major' 'minor' or 'both' ticks
1195+
'axes.grid.axis': ['both', ['x', 'y', 'both']], # grid type:
11891196
# 'x', 'y', or 'both'
11901197
'axes.labelsize': ['medium', validate_fontsize], # fontsize of the
11911198
# x any y labels
@@ -1214,9 +1221,7 @@ def validate_webagg_address(s):
12141221
validate_cycler],
12151222
# If 'data', axes limits are set close to the data.
12161223
# 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'])],
1224+
'axes.autolimit_mode': ['data', ['data', 'round_numbers']],
12201225
'axes.xmargin': [0.05, _range_validators["0 <= x <= 1"]],
12211226
'axes.ymargin': [0.05, _range_validators["0 <= x <= 1"]],
12221227

@@ -1291,7 +1296,8 @@ def validate_webagg_address(s):
12911296
# fontsize of the xtick labels
12921297
'xtick.labelsize': ['medium', validate_fontsize],
12931298
'xtick.direction': ['out', validate_string], # direction of xticks
1294-
'xtick.alignment': ["center", _validate_alignment],
1299+
'xtick.alignment': ['center',
1300+
['center', 'top', 'bottom', 'baseline', 'center_baseline']],
12951301

12961302
'ytick.left': [True, validate_bool], # draw ticks on the left side
12971303
'ytick.right': [False, validate_bool], # draw ticks on the right side
@@ -1313,7 +1319,8 @@ def validate_webagg_address(s):
13131319
# fontsize of the ytick labels
13141320
'ytick.labelsize': ['medium', validate_fontsize],
13151321
'ytick.direction': ['out', validate_string], # direction of yticks
1316-
'ytick.alignment': ["center_baseline", _validate_alignment],
1322+
'ytick.alignment': ['center_baseline',
1323+
['center', 'top', 'bottom', 'baseline', 'center_baseline']],
13171324

13181325
'grid.color': ['#b0b0b0', validate_color], # grid color
13191326
'grid.linestyle': ['-', _validate_linestyle], # solid
@@ -1359,7 +1366,7 @@ def validate_webagg_address(s):
13591366
'savefig.dpi': ['figure', validate_dpi], # DPI
13601367
'savefig.facecolor': ['white', validate_color],
13611368
'savefig.edgecolor': ['white', validate_color],
1362-
'savefig.orientation': ['portrait', validate_orientation],
1369+
'savefig.orientation': ['portrait', ['landscape', 'portrait']],
13631370
'savefig.jpeg_quality': [95, validate_int],
13641371
# value checked by backend at runtime
13651372
'savefig.format': ['png', _update_savefig_format],
@@ -1389,7 +1396,7 @@ def validate_webagg_address(s):
13891396
'pdf.fonttype': [3, validate_fonttype], # 3 (Type3) or 42 (Truetype)
13901397

13911398
# choose latex application for creating pdf files (xelatex/lualatex)
1392-
'pgf.texsystem': ['xelatex', validate_pgf_texsystem],
1399+
'pgf.texsystem': ['xelatex', ['xelatex', 'lualatex', 'pdflatex']],
13931400
# use matplotlib rc settings for font configuration
13941401
'pgf.rcfonts': [True, validate_bool],
13951402
# provide a custom preamble for the latex process
@@ -1398,7 +1405,7 @@ def validate_webagg_address(s):
13981405
# write raster image data directly into the svg file
13991406
'svg.image_inline': [True, validate_bool],
14001407
# True to save all characters as paths in the SVG
1401-
'svg.fonttype': ['path', validate_svg_fonttype],
1408+
'svg.fonttype': ['path', ['none', 'path']],
14021409
'svg.hashsalt': [None, validate_string_or_None],
14031410

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

14341441
# Animation settings
1435-
'animation.html': ['none', validate_movie_html_fmt],
1442+
'animation.html': ['none', ['html5', 'jshtml', 'none']],
14361443
# Limit, in MB, of size of base64 encoded animation in HTML
14371444
# (i.e. IPython notebook)
14381445
'animation.embed_limit': [20, validate_float],
14391446
'animation.writer': ['ffmpeg', validate_movie_writer],
14401447
'animation.codec': ['h264', validate_string],
14411448
'animation.bitrate': [-1, validate_int],
14421449
# Controls image format when frames are written to disk
1443-
'animation.frame_format': ['png', validate_movie_frame_fmt],
1450+
'animation.frame_format': ['png', ['png', 'jpeg', 'tiff', 'raw', 'rgba']],
14441451
# Additional arguments for HTML writer
14451452
'animation.html_args': [[], validate_stringlist],
14461453
# Path to ffmpeg binary. If just binary name, subprocess uses $PATH.
@@ -1464,3 +1471,12 @@ def validate_webagg_address(s):
14641471
# altogether. For that use `matplotlib.style.use('classic')`.
14651472
'_internal.classic_mode': [False, validate_bool]
14661473
}
1474+
1475+
1476+
def _fix_validators():
1477+
for k, (default, conv) in defaultParams.items():
1478+
if isinstance(conv, list):
1479+
defaultParams[k] = (default, ValidateInStrings(k, conv))
1480+
1481+
1482+
_fix_validators()

0 commit comments

Comments
 (0)