8000 Add helper function to check that an argument is in a list of strings. · matplotlib/matplotlib@e23abe9 · GitHub
[go: up one dir, main page]

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

File tree

25 files changed

+122
-193
lines changed

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:

0 commit comments

Comments
 (0)
0