Skip to content

Commit 5487b2e

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

File tree

23 files changed

+113
-166
lines changed

23 files changed

+113
-166
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: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,8 @@ 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']
205+
**{'label rotation mode': mode})
207206
self._labelrotation = (mode, angle)
208207

209208
def apply_tickdir(self, tickdir):
@@ -1195,8 +1194,7 @@ def get_ticklabels(self, minor=False, which=None):
11951194
elif which == 'both':
11961195
return self.get_majorticklabels() + self.get_minorticklabels()
11971196
else:
1198-
raise ValueError("`which` must be one of ('minor', 'major', "
1199-
"'both') not " + str(which))
1197+
cbook._check_in_list(['major', 'minor', 'both'], which=which)
12001198
if minor:
12011199
return self.get_minorticklabels()
12021200
return self.get_majorticklabels()
@@ -1352,9 +1350,7 @@ def grid(self, b=None, which='major', **kwargs):
13521350
'grid will be enabled.')
13531351
b = True
13541352
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'.")
1353+
cbook._check_in_list(['major', 'minor', 'both'], which=which)
13581354
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
13591355

13601356
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,12 @@ def _unmultiplied_rgba8888_to_premultiplied_argb32(rgba8888):
21052105

21062106

21072107
def _check_and_log_subprocess(command, logger, **kwargs):
2108+
"""
2109+
Run *command* using `subprocess.check_output`. If it succeeds, return the
2110+
output (stdout and stderr); if not, raise an exception whose text includes
2111+
the failed command and captured output. Both the command and the output
2112+
are logged at DEBUG level on *logger*.
2113+
"""
21082114
logger.debug(command)
21092115
try:
21102116
report = subprocess.check_output(
@@ -2128,3 +2134,15 @@ def _check_not_matrix(**kwargs):
21282134
for k, v in kwargs.items():
21292135
if isinstance(v, np.matrix):
21302136
raise TypeError(f"Argument {k!r} cannot be a np.matrix")
2137+
2138+
2139+
def _check_in_list(values, **kwargs):
2140+
"""
2141+
For each *key, value* pair in *kwargs*, check that *value* is in *values*;
2142+
if not, raise an appropriate ValueError.
2143+
"""
2144+
for k, v in kwargs.items():
2145+
if v not in values:
2146+
raise ValueError(
2147+
"{!r} is not a valid value for {}; supported values are {}"
2148+
.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/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):

lib/matplotlib/projections/polar.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
import numpy as np
55

6+
from matplotlib import cbook, rcParams
67
from matplotlib.axes import Axes
78
import matplotlib.axis as maxis
89
import matplotlib.markers as mmarkers
910
import matplotlib.patches as mpatches
1011
import matplotlib.path as mpath
11-
from matplotlib import rcParams
1212
import matplotlib.ticker as mticker
1313
import matplotlib.transforms as mtransforms
1414
import matplotlib.spines as mspines
@@ -950,9 +950,7 @@ def _set_lim_and_transforms(self):
950950
self._r_label_position + self.transData)
951951

952952
def get_xaxis_transform(self, which='grid'):
953-
if which not in ['tick1', 'tick2', 'grid']:
954-
raise ValueError(
955-
"'which' must be one of 'tick1', 'tick2', or 'grid'")
953+
cbook._check_in_list(['tick1', 'tick2', 'grid'], which=which)
956954
return self._xaxis_transform
957955

958956
def get_xaxis_text1_transform(self, pad):
@@ -967,8 +965,7 @@ def get_yaxis_transform(self, which='grid'):
967965
elif which == 'grid':
968966
return self._yaxis_transform
969967
else:
970-
raise ValueError(
971-
"'which' must be one of 'tick1', 'tick2', or 'grid'")
968+
cbook._check_in_list(['tick1', 'tick2', 'grid'], which=which)
972969

973970
def get_yaxis_text1_transform(self, pad):
974971
thetamin, thetamax = self._realViewLim.intervalx
@@ -1127,8 +1124,9 @@ def set_theta_direction(self, direction):
11271124
elif direction in ('counterclockwise', 'anticlockwise', 1):
11281125
mtx[0, 0] = 1
11291126
else:
1130-
raise ValueError(
1131-
"direction must be 1, -1, clockwise or counterclockwise")
1127+
cbook._check_in_list(
1128+
[-1, 1, 'clockwise', 'counterclockwise', 'anticlockwise'],
1129+
direction=direction)
11321130
self._direction.invalidate()
11331131

11341132
def get_theta_direction(self):

lib/matplotlib/quiver.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,7 @@ def __init__(self, ax, *args,
460460
if pivot.lower() == 'mid':
461461
pivot = 'middle'
462462
self.pivot = pivot.lower()
463-
if self.pivot not in self._PIVOT_VALS:
464-
raise ValueError(
465-
'pivot must be one of {keys}, you passed {inp}'.format(
466-
keys=self._PIVOT_VALS, inp=pivot))
463+
cbook._check_in_list(self._PIVOT_VALS, pivot=self.pivot)
467464

468465
self.transform = kw.pop('transform', ax.transData)
469466
kw.setdefault('facecolors', self.color)

lib/matplotlib/scale.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,19 @@ def __init__(self, axis, **kwargs):
257257
base = kwargs.pop('basex', 10.0)
258258
subs = kwargs.pop('subsx', None)
259259
nonpos = kwargs.pop('nonposx', 'clip')
260+
cbook._check_in_list(['mask', 'clip'], nonposx=nonpos)
260261
else:
261262
base = kwargs.pop('basey', 10.0)
262263
subs = kwargs.pop('subsy', None)
263264
nonpos = kwargs.pop('nonposy', 'clip')
265+
cbook._check_in_list(['mask', 'clip'], nonposy=nonpos)
264266

265267
if len(kwargs):
266268
raise ValueError(("provided too many kwargs, can only pass "
267269
"{'basex', 'subsx', nonposx'} or "
268270
"{'basey', 'subsy', nonposy'}. You passed ") +
269271
"{!r}".format(kwargs))
270272

271-
if nonpos not in ['mask', 'clip']:
272-
raise ValueError("nonposx, nonposy kwarg must be 'mask' or 'clip'")
273273
if base <= 0 or base == 1:
274274
raise ValueError('The log base cannot be <= 0 or == 1')
275275

@@ -532,9 +532,7 @@ def __init__(self, axis, nonpos='mask'):
532532
values beyond ]0, 1[ can be masked as invalid, or clipped to a number
533533
very close to 0 or 1
534534
"""
535-
if nonpos not in ['mask', 'clip']:
536-
raise ValueError("nonposx, nonposy kwarg must be 'mask' or 'clip'")
537-
535+
cbook._check_in_list(['mask', 'clip'], nonpos=nonpos)
538536
self._transform = LogitTransform(nonpos)
539537

540538
def get_transform(self):

0 commit comments

Comments
 (0)