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

Skip to content

Commit ed1d8dd

Browse files
authored
Merge pull request #12232 from anntzer/enumchecker
Add helper function to check that an argument is in a list of strings.
2 parents 49c9904 + c0f839b commit ed1d8dd

25 files changed

+121
-191
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
@@ -1328,8 +1328,7 @@ def set_adjustable(self, adjustable, share=False):
13281328
which the adjustments for aspect ratios are done sequentially
13291329
and independently on each Axes as it is drawn.
13301330
"""
1331-
if adjustable not in ('box', 'datalim'):
1332-
raise ValueError("argument must be 'box', or 'datalim'")
1331+
cbook._check_in_list(["box", "datalim"], adjustable=adjustable)
13331332
if share:
13341333
axes = set(self._shared_x_axes.get_siblings(self)
13351334
+ self._shared_y_axes.get_siblings(self))
@@ -2745,9 +2744,7 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
27452744
"""
27462745
if len(kwargs):
27472746
b = True
2748-
if axis not in ['x', 'y', 'both']:
2749-
raise ValueError("The argument 'axis' must be one of 'x', 'y' or "
2750-
"'both'.")
2747+
cbook._check_in_list(['x', 'y', 'both'], axis=axis)
27512748
if axis in ['x', 'both']:
27522749
self.xaxis.grid(b, which=which, **kwargs)
27532750
if axis in ['y', 'both']:
@@ -2963,8 +2960,7 @@ def tick_params(self, axis='both', **kwargs):
29632960
also be red. Gridlines will be red and translucent.
29642961
29652962
"""
2966-
if axis not in ['x', 'y', 'both']:
2967-
raise ValueError("axis must be one of 'x', 'y' or 'both'")
2963+
cbook._check_in_list(['x', 'y', 'both'], axis=axis)
29682964
if axis in ['x', 'both']:
29692965
xkw = dict(kwargs)
29702966
xkw.pop('left', None)

lib/matplotlib/axes/_subplots.py

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

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

lib/matplotlib/axis.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ def _set_labelrotation(self, labelrotation):
204204
else:
205205
mode = 'default'
206206
angle = labelrotation
207-
if mode not in ('auto', 'default'):
208-
raise ValueError("Label rotation mode must be 'default' or "
209-
"'auto', not '{}'.".format(mode))
207+
cbook._check_in_list(['auto', 'default'], labelrotation=mode)
210208
self._labelrotation = (mode, angle)
211209

212210
def apply_tickdir(self, tickdir):
@@ -1269,8 +1267,7 @@ def get_ticklabels(self, minor=False, which=None):
12691267
elif which == 'both':
12701268
return self.get_majorticklabels() + self.get_minorticklabels()
12711269
else:
1272-
raise ValueError("`which` must be one of ('minor', 'major', "
1273-
"'both') not " + str(which))
1270+
cbook._check_in_list(['major', 'minor', 'both'], which=which)
12741271
if minor:
12751272
return self.get_minorticklabels()
12761273
return self.get_majorticklabels()
@@ -1426,9 +1423,7 @@ def grid(self, b=None, which='major', **kwargs):
14261423
'grid will be enabled.')
14271424
b = True
14281425
which = which.lower()
1429-
if which not in ['major', 'minor', 'both']:
1430-
raise ValueError("The argument 'which' must be one of 'major', "
1431-
"'minor' or 'both'.")
1426+
cbook._check_in_list(['major', 'minor', 'both'], which=which)
14321427
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
14331428

14341429
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
@@ -2111,6 +2111,12 @@ def _unmultiplied_rgba8888_to_premultiplied_argb32(rgba8888):
21112111

21122112

21132113
def _check_and_log_subprocess(command, logger, **kwargs):
2114+
"""
2115+
Run *command* using `subprocess.check_output`. If it succeeds, return the
2116+
output (stdout and stderr); if not, raise an exception whose text includes
2117+
the failed command and captured output. Both the command and the output
2118+
are logged at DEBUG level on *logger*.
2119+
"""
21142120
logger.debug(command)
21152121
try:
21162122
report = subprocess.check_output(
@@ -2134,3 +2140,19 @@ def _check_not_matrix(**kwargs):
21342140
for k, v in kwargs.items():
21352141
if isinstance(v, np.matrix):
21362142
raise TypeError(f"Argument {k!r} cannot be a np.matrix")
2143+
2144+
2145+
def _check_in_list(values, **kwargs):
2146+
"""
2147+
For each *key, value* pair in *kwargs*, check that *value* is in *values*;
2148+
if not, raise an appropriate ValueError.
2149+
2150+
Examples
2151+
--------
2152+
>>> cbook._check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
2153+
"""
2154+
for k, v in kwargs.items():
2155+
if v not in values:
2156+
raise ValueError(
2157+
"{!r} is not a valid value for {}; supported values are {}"
2158+
.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
@@ -464,8 +464,8 @@ def set_offset_position(self, offset_position):
464464
----------
465465
offset_position : {'screen', 'data'}
466466
"""
467-
if offset_position not in ('screen', 'data'):
468-
raise ValueError("offset_position must be 'screen' or 'data'")
467+
cbook._check_in_list(['screen', 'data'],
468+
offset_position=offset_position)
469469
self._offset_position = offset_position
470470
self.stale = True
471471

@@ -1490,7 +1490,8 @@ def __init__(self,
14901490
coord1 in positions]
14911491
self._is_horizontal = False
14921492
else:
1493-
raise ValueError("orientation must be 'horizontal' or 'vertical'")
1493+
cbook._check_in_list(['horizontal', 'vertical'],
1494+
orientation=orientation)
14941495

14951496
LineCollection.__init__(self,
14961497
segments,
@@ -1583,8 +1584,8 @@ def set_orientation(self, orientation=None):
15831584
elif orientation.lower() == 'vertical':
15841585
is_horizontal = False
15851586
else:
1586-
raise ValueError("orientation must be 'horizontal' or 'vertical'")
1587-
1587+
cbook._check_in_list(['horizontal', 'vertical'],
1588+
orientation=orientation)
15881589
if is_horizontal == self.is_horizontal():
15891590
return
15901591
self.switch_orientation()

lib/matplotlib/contour.py

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

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

lib/matplotlib/figure.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,23 +1524,17 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
15241524
sharex = "all" if sharex else "none"
15251525
if isinstance(sharey, bool):
15261526
sharey = "all" if sharey else "none"
1527-
share_values = ["all", "row", "col", "none"]
1528-
if sharex not in share_values:
1529-
# This check was added because it is very easy to type
1530-
# `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended.
1531-
# In most cases, no error will ever occur, but mysterious behavior
1532-
# will result because what was intended to be the subplot index is
1533-
# instead treated as a bool for sharex.
1534-
if isinstance(sharex, Integral):
1535-
cbook._warn_external("sharex argument to subplots() was an "
1536-
"integer. Did you intend to use "
1537-
"subplot() (without 's')?")
1538-
1539-
raise ValueError("sharex [%s] must be one of %s" %
1540-
(sharex, share_values))
1541-
if sharey not in share_values:
1542-
raise ValueError("sharey [%s] must be one of %s" %
1543-
(sharey, share_values))
1527+
# This check was added because it is very easy to type
1528+
# `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended.
1529+
# In most cases, no error will ever occur, but mysterious behavior
1530+
# will result because what was intended to be the subplot index is
1531+
# instead treated as a bool for sharex.
1532+
if isinstance(sharex, Integral):
1533+
cbook._warn_external(
1534+
"sharex argument to subplots() was an integer. Did you "
1535+
"intend to use subplot() (without 's')?")
1536+
cbook._check_in_list(["all", "row", "col", "none"],
1537+
sharex=sharex, sharey=sharey)
15441538
if subplot_kw is None:
15451539
subplot_kw = {}
15461540
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
@@ -3351,14 +3351,10 @@ def parse(self, s, dpi = 72, prop = None):
33513351
font_output = StandardPsFonts(prop)
33523352
else:
33533353
backend = self._backend_mapping[self._output]()
3354-
fontset = rcParams['mathtext.fontset']
3355-
fontset_class = self._font_type_mapping.get(fontset.lower())
3356-
if fontset_class is not None:
3357-
font_output = fontset_class(prop, backend)
3358-
else:
3359-
raise ValueError(
3360-
"mathtext.fontset must be either 'cm', 'dejavuserif', "
3361-
"'dejavusans', 'stix', 'stixsans', or 'custom'")
3354+
fontset = rcParams['mathtext.fontset'].lower()
3355+
cbook._check_in_list(self._font_type_mapping, fontset=fontset)
3356+
fontset_class = self._font_type_mapping[fontset]
3357+
font_output = fontset_class(prop, backend)
33623358

33633359
fontsize = prop.get_size_in_points()
33643360

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)