Skip to content

Commit e23abe9

Browse files
committed
Add helper function to check that an argument is in a list of strings.
1 parent c22847b commit e23abe9

25 files changed

+122
-193
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Changed exceptions
2+
``````````````````
3+
4+
- `mpl_toolkits.axes_grid1.axes_size.GetExtentHelper` now raises `ValueError`
5+
for invalid directions instead of `KeyError`.

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,7 @@ def set_adjustable(self, adjustable, share=False):
13211321
which the adjustments for aspect ratios are done sequentially
13221322
and independently on each Axes as it is drawn.
13231323
"""
1324-
if adjustable not in ('box', 'datalim'):
1325-
raise ValueError("argument must be 'box', or 'datalim'")
1324+
cbook._check_in_list(["box", "datalim"], adjustable=adjustable)
13261325
if share:
13271326
axes = set(self._shared_x_axes.get_siblings(self)
13281327
+ self._shared_y_axes.get_siblings(self))
@@ -2734,9 +2733,7 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
27342733
"""
27352734
if len(kwargs):
27362735
b = True
2737-
if axis not in ['x', 'y', 'both']:
2738-
raise ValueError("The argument 'axis' must be one of 'x', 'y' or "
2739-
"'both'.")
2736+
cbook._check_in_list(['x', 'y', 'both'], axis=axis)
27402737
if axis in ['x', 'both']:
27412738
self.xaxis.grid(b, which=which, **kwargs)
27422739
if axis in ['y', 'both']:
@@ -2952,8 +2949,7 @@ def tick_params(self, axis='both', **kwargs):
29522949
also be red. Gridlines will be red and translucent.
29532950
29542951
"""
2955-
if axis not in ['x', 'y', 'both']:
2956-
raise ValueError("axis must be one of 'x', 'y' or 'both'")
2952+
cbook._check_in_list(['x', 'y', 'both'], axis=axis)
29572953
if axis in ['x', 'both']:
29582954
xkw = dict(kwargs)
29592955
xkw.pop('left', None)

lib/matplotlib/axes/_subplots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22

3-
from matplotlib import docstring
3+
from matplotlib import cbook, docstring
44
import matplotlib.artist as martist
55
from matplotlib.axes._axes import Axes
66
from matplotlib.gridspec import GridSpec, SubplotSpec

lib/matplotlib/axis.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ def _set_labelrotation(self, labelrotation):
201201
else:
202202
mode = 'default'
203203
angle = labelrotation
204-
if mode not in ('auto', 'default'):
205-
raise ValueError("Label rotation mode must be 'default' or "
206-
"'auto', not '{}'.".format(mode))
204+
cbook._check_in_list(['auto', 'default'], labelrotation=mode)
207205
self._labelrotation = (mode, angle)
208206

209207
def apply_tickdir(self, tickdir):
@@ -1195,8 +1193,7 @@ def get_ticklabels(self, minor=False, which=None):
11951193
elif which == 'both':
11961194
return self.get_majorticklabels() + self.get_minorticklabels()
11971195
else:
1198-
raise ValueError("`which` must be one of ('minor', 'major', "
1199-
"'both') not " + str(which))
1196+
cbook._check_in_list(['major', 'minor', 'both'], which=which)
12001197
if minor:
12011198
return self.get_minorticklabels()
12021199
return self.get_majorticklabels()
@@ -1352,9 +1349,7 @@ def grid(self, b=None, which='major', **kwargs):
13521349
'grid will be enabled.')
13531350
b = True
13541351
which = which.lower()
1355-
if which not in ['major', 'minor', 'both']:
1356-
raise ValueError("The argument 'which' must be one of 'major', "
1357-
"'minor' or 'both'.")
1352+
cbook._check_in_list(['major', 'minor', 'both'], which=which)
13581353
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
13591354

13601355
if which in ['minor', 'both']:

lib/matplotlib/backends/backend_ps.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -838,12 +838,9 @@ def _print_ps(self, outfile, format, *args,
838838
(papertype, ', '.join(papersize)))
839839

840840
orientation = orientation.lower()
841-
if orientation == 'landscape':
842-
isLandscape = True
843-
elif orientation == 'portrait':
844-
isLandscape = False
845-
else:
846-
raise RuntimeError('Orientation must be "portrait" or "landscape"')
841+
cbook._check_in_list(['landscape', 'portrait'],
842+
orientation=orientation)
843+
isLandscape = (orientation == 'landscape')
847844

848845
self.figure.set_dpi(72) # Override the dpi kwarg
849846

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import numpy as np
2121
import tornado
2222

23+
from matplotlib import backend_bases, cbook, _png
2324
from matplotlib.backends import backend_agg
2425
from matplotlib.backend_bases import _Backend
25-
from matplotlib import backend_bases, _png
2626

2727
_log = logging.getLogger(__name__)
2828

@@ -163,10 +163,8 @@ def set_image_mode(self, mode):
163163
Note: diff images may not contain transparency, therefore upon
164164
draw this mode may be changed if the resulting image has any
165165
transparent component.
166-
167166
"""
168-
if mode not in ['full', 'diff']:
169-
raise ValueError('image mode must be either full or diff.')
167+
cbook._check_in_list(['full', 'diff'], mode=mode)
170168
if self._current_image_mode != mode:
171169
self._current_image_mode = mode
172170
self.handle_send_image_mode(None)

lib/matplotlib/cbook/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,12 @@ def _unmultiplied_rgba8888_to_premultiplied_argb32(rgba8888):
21062106

21072107

21082108
def _check_and_log_subprocess(command, logger, **kwargs):
2109+
"""
2110+
Run *command* using `subprocess.check_output`. If it succeeds, return the
2111+
output (stdout and stderr); if not, raise an exception whose text includes
2112+
the failed command and captured output. Both the command and the output
2113+
are logged at DEBUG level on *logger*.
2114+
"""
21092115
logger.debug(command)
21102116
try:
21112117
report = subprocess.check_output(
@@ -2129,3 +2135,19 @@ def _check_not_matrix(**kwargs):
21292135
for k, v in kwargs.items():
21302136
if isinstance(v, np.matrix):
21312137
raise TypeError(f"Argument {k!r} cannot be a np.matrix")
2138+
2139+
2140+
def _check_in_list(values, **kwargs):
2141+
"""
2142+
For each *key, value* pair in *kwargs*, check that *value* is in *values*;
2143+
if not, raise an appropriate ValueError.
2144+
2145+
Examples
2146+
--------
2147+
>>> cbook._check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
2148+
"""
2149+
for k, v in kwargs.items():
2150+
if v not in values:
2151+
raise ValueError(
2152+
"{!r} is not a valid value for {}; supported values are {}"
2153+
.format(v, k, ', '.join(map(repr, values))))

lib/matplotlib/collections.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ def set_offset_position(self, offset_position):
463463
----------
464464
offset_position : {'screen', 'data'}
465465
"""
466-
if offset_position not in ('screen', 'data'):
467-
raise ValueError("offset_position must be 'screen' or 'data'")
466+
cbook._check_in_list(['screen', 'data'],
467+
offset_position=offset_position)
468468
self._offset_position = offset_position
469469
self.stale = True
470470

@@ -1361,7 +1361,8 @@ def __init__(self,
13611361
coord1 in positions]
13621362
self._is_horizontal = False
13631363
else:
1364-
raise ValueError("orientation must be 'horizontal' or 'vertical'")
1364+
cbook._check_in_list(['horizontal', 'vertical'],
1365+
orientation=orientation)
13651366

13661367
LineCollection.__init__(self,
13671368
segments,
@@ -1454,8 +1455,8 @@ def set_orientation(self, orientation=None):
14541455
elif orientation.lower() == 'vertical':
14551456
is_horizontal = False
14561457
else:
1457-
raise ValueError("orientation must be 'horizontal' or 'vertical'")
1458-
1458+
cbook._check_in_list(['horizontal', 'vertical'],
1459+
orientation=orientation)
14591460
if is_horizontal == self.is_horizontal():
14601461
return
14611462
self.switch_orientation()

lib/matplotlib/contour.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,7 @@ def __init__(self, ax, *args,
843843
else:
844844
self.logscale = False
845845

846-
if self.origin not in [None, 'lower', 'upper', 'image']:
847-
raise ValueError("If given, *origin* must be one of [ 'lower' |"
848-
" 'upper' | 'image']")
846+
cbook._check_in_list([None, 'lower', 'upper', 'image'], origin=origin)
849847
if self.extent is not None and len(self.extent) != 4:
850848
raise ValueError("If given, *extent* must be '[ *None* |"
851849
" (x0,x1,y0,y1) ]'")

lib/matplotlib/figure.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,23 +1492,17 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
14921492
sharex = "all" if sharex else "none"
14931493
if isinstance(sharey, bool):
14941494
sharey = "all" if sharey else "none"
1495-
share_values = ["all", "row", "col", "none"]
1496-
if sharex not in share_values:
1497-
# This check was added because it is very easy to type
1498-
# `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended.
1499-
# In most cases, no error will ever occur, but mysterious behavior
1500-
# will result because what was intended to be the subplot index is
1501-
# instead treated as a bool for sharex.
1502-
if isinstance(sharex, Integral):
1503-
cbook._warn_external("sharex argument to subplots() was an "
1504-
"integer. Did you intend to use "
1505-
"subplot() (without 's')?")
1506-
1507-
raise ValueError("sharex [%s] must be one of %s" %
1508-
(sharex, share_values))
1509-
if sharey not in share_values:
1510-
raise ValueError("sharey [%s] must be one of %s" %
1511-
(sharey, share_values))
1495+
# This check was added because it is very easy to type
1496+
# `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended.
1497+
# In most cases, no error will ever occur, but mysterious behavior
1498+
# will result because what was intended to be the subplot index is
1499+
# instead treated as a bool for sharex.
1500+
if isinstance(sharex, Integral):
1501+
cbook._warn_external(
1502+
"sharex argument to subplots() was an integer. Did you "
1503+
"intend to use subplot() (without 's')?")
1504+
cbook._check_in_list(["all", "row", "col", "none"],
1505+
sharex=sharex, sharey=sharey)
15121506
if subplot_kw is None:
15131507
subplot_kw = {}
15141508
if gridspec_kw is None:

lib/matplotlib/font_manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,7 @@ def set_style(self, style):
759759
"""
760760
if style is None:
761761
style = rcParams['font.style']
762-
if style not in ('normal', 'italic', 'oblique'):
763-
raise ValueError("style must be normal, italic or oblique")
762+
cbook._check_in_list(['normal', 'italic', 'oblique'], style=style)
764763
self._slant = style
765764
set_slant = set_style
766765

@@ -770,8 +769,7 @@ def set_variant(self, variant):
770769
"""
771770
if variant is None:
772771
variant = rcParams['font.variant']
773-
if variant not in ('normal', 'small-caps'):
774-
raise ValueError("variant must be normal or small-caps")
772+
cbook._check_in_list(['normal', 'small-caps'], variant=variant)
775773
self._variant = variant
776774

777775
def set_weight(self, weight):

lib/matplotlib/lines.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,8 +1083,7 @@ def set_drawstyle(self, drawstyle):
10831083
"""
10841084
if drawstyle is None:
10851085
drawstyle = 'default'
1086-
if drawstyle not in self.drawStyles:
1087-
raise ValueError('Unrecognized drawstyle {!r}'.format(drawstyle))
1086+
cbook._check_in_list(self.drawStyles, drawstyle=drawstyle)
10881087
if self._drawstyle != drawstyle:
10891088
self.stale = True
10901089
# invalidate to trigger a recache of the path
@@ -1181,13 +1180,9 @@ def set_linestyle(self, ls):
11811180
if ls in [' ', '', 'none']:
11821181
ls = 'None'
11831182

1183+
cbook._check_in_list([*self._lineStyles, *ls_mapper_r], ls=ls)
11841184
if ls not in self._lineStyles:
1185-
try:
1186-
ls = ls_mapper_r[ls]
1187-
except KeyError:
1188-
raise ValueError("Invalid linestyle {!r}; see docs of "
1189-
"Line2D.set_linestyle for valid values"
1190-
.format(ls))
1185+
ls = ls_mapper_r[ls]
11911186
self._linestyle = ls
11921187
else:
11931188
self._linestyle = '--'
@@ -1362,9 +1357,7 @@ def set_dash_joinstyle(self, s):
13621357
For examples see :doc:`/gallery/lines_bars_and_markers/joinstyle`.
13631358
"""
13641359
s = s.lower()
1365-
if s not in self.validJoin:
1366-
raise ValueError('set_dash_joinstyle passed "%s";\n' % (s,)
1367-
+ 'valid joinstyles are %s' % (self.validJoin,))
1360+
cbook._check_in_list(self.validJoin, s=s)
13681361
if self._dashjoinstyle != s:
13691362
self.stale = True
13701363
self._dashjoinstyle = s
@@ -1379,10 +1372,7 @@ def set_solid_joinstyle(self, s):
13791372
For examples see :doc:`/gallery/lines_bars_and_markers/joinstyle`.
13801373
"""
13811374
s = s.lower()
1382-
if s not in self.validJoin:
1383-
raise ValueError('set_solid_joinstyle passed "%s";\n' % (s,)
1384-
+ 'valid joinstyles are %s' % (self.validJoin,))
1385-
1375+
cbook._check_in_list(self.validJoin, s=s)
13861376
if self._solidjoinstyle != s:
13871377
self.stale = True
13881378
self._solidjoinstyle = s
@@ -1412,9 +1402,7 @@ def set_dash_capstyle(self, s):
14121402
s : {'butt', 'round', 'projecting'}
14131403
"""
14141404
s = s.lower()
1415-
if s not in self.validCap:
1416-
raise ValueError('set_dash_capstyle passed "%s";\n' % (s,)
1417-
+ 'valid capstyles are %s' % (self.validCap,))
1405+
cbook._check_in_list(self.validCap, s=s)
14181406
if self._dashcapstyle != s:
14191407
self.stale = True
14201408
self._dashcapstyle = s
@@ -1428,9 +1416,7 @@ def set_solid_capstyle(self, s):
14281416
s : {'butt', 'round', 'projecting'}
14291417
"""
14301418
s = s.lower()
1431-
if s not in self.validCap:
1432-
raise ValueError('set_solid_capstyle passed "%s";\n' % (s,)
1433-
+ 'valid capstyles are %s' % (self.validCap,))
1419+
cbook._check_in_list(self.validCap, s=s)
14341420
if self._solidcapstyle != s:
14351421
self.stale = True
14361422
self._solidcapstyle = s

lib/matplotlib/mathtext.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,14 +3349,10 @@ def parse(self, s, dpi = 72, prop = None):
33493349
font_output = StandardPsFonts(prop)
33503350
else:
33513351
backend = self._backend_mapping[self._output]()
3352-
fontset = rcParams['mathtext.fontset']
3353-
fontset_class = self._font_type_mapping.get(fontset.lower())
3354-
if fontset_class is not None:
3355-
font_output = fontset_class(prop, backend)
3356-
else:
3357-
raise ValueError(
3358-
"mathtext.fontset must be either 'cm', 'dejavuserif', "
3359-
"'dejavusans', 'stix', 'stixsans', or 'custom'")
3352+
fontset = rcParams['mathtext.fontset'].lower()
3353+
cbook._check_in_list(self._font_type_mapping, fontset=fontset)
3354+
fontset_class = self._font_type_mapping[fontset]
3355+
font_output = fontset_class(prop, backend)
33603356

33613357
fontsize = prop.get_size_in_points()
33623358

lib/matplotlib/projections/geo.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from matplotlib import rcParams
3+
from matplotlib import cbook, rcParams
44
from matplotlib.axes import Axes
55
import matplotlib.axis as maxis
66
from matplotlib.patches import Circle
@@ -118,9 +118,7 @@ def _get_affine_transform(self):
118118
.translate(0.5, 0.5)
119119

120120
def get_xaxis_transform(self, which='grid'):
121-
if which not in ['tick1', 'tick2', 'grid']:
122-
raise ValueError(
123-
"'which' must be one of 'tick1', 'tick2', or 'grid'")
121+
cbook._check_in_list(['tick1', 'tick2', 'grid'], which=which)
124122
return self._xaxis_transform
125123

126124
def get_xaxis_text1_transform(self, pad):
@@ -130,9 +128,7 @@ def get_xaxis_text2_transform(self, pad):
130128
return self._xaxis_text2_transform, 'top', 'center'
131129

132130
def get_yaxis_transform(self, which='grid'):
133-
if which not in ['tick1', 'tick2', 'grid']:
134-
raise ValueError(
135-
"'which' must be one of 'tick1', 'tick2', or 'grid'")
131+
cbook._check_in_list(['tick1', 'tick2', 'grid'], which=which)
136132
return self._yaxis_transform
137133

138134
def get_yaxis_text1_transform(self, pad):

0 commit comments

Comments
 (0)