Skip to content

Commit e94dfed

Browse files
committed
Improve argument checking
1 parent 9bb0c9e commit e94dfed

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

lib/mpl_toolkits/axes_grid1/axes_divider.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __init__(self, fig, pos, horizontal, vertical,
3939
aspect : bool
4040
Whether overall rectangular area is reduced so that the relative
4141
part of the horizontal and vertical scales have the same scale.
42-
anchor : {'C', 'SW', 'S', 'SE', 'E', 'NE', 'N', 'NW', 'W'}
42+
anchor : (float, float) or {'C', 'SW', 'S', 'SE', 'E', 'NE', 'N', \
43+
'NW', 'W'}
4344
Placement of the reduced rectangle, when *aspect* is True.
4445
"""
4546

@@ -48,6 +49,7 @@ def __init__(self, fig, pos, horizontal, vertical,
4849
self._horizontal = horizontal
4950
self._vertical = vertical
5051
self._anchor = anchor
52+
self.set_anchor(anchor)
5153
self._aspect = aspect
5254
self._xrefindex = 0
5355
self._yrefindex = 0
@@ -106,7 +108,8 @@ def set_anchor(self, anchor):
106108
"""
107109
Parameters
108110
----------
109-
anchor : (float, float) or {'C', 'SW', 'S', 'SE', 'E', 'NE', ...}
111+
anchor : (float, float) or {'C', 'SW', 'S', 'SE', 'E', 'NE', 'N', \
112+
'NW', 'W'}
110113
Either an (*x*, *y*) pair of relative coordinates (0 is left or
111114
bottom, 1 is right or top), 'C' (center), or a cardinal direction
112115
('SW', southwest, is bottom left, etc.).
@@ -115,8 +118,10 @@ def set_anchor(self, anchor):
115118
--------
116119
.Axes.set_anchor
117120
"""
118-
if len(anchor) != 2:
121+
if isinstance(anchor, str):
119122
_api.check_in_list(mtransforms.Bbox.coefs, anchor=anchor)
123+
elif not isinstance(anchor, (tuple, list)) or len(anchor) != 2:
124+
raise TypeError("anchor must be str or 2-tuple")
120125
self._anchor = anchor
121126

122127
def get_anchor(self):
@@ -511,20 +516,14 @@ def append_axes(self, position, size, pad=None, add_to_figure=True, *,
511516
**kwargs
512517
All extra keywords arguments are passed to the created axes.
513518
"""
514-
_api.check_in_list(["left", "right", "bottom", "top"],
515-
position=position)
516-
if position == "left":
517-
ax = self.new_horizontal(
518-
size, pad, pack_start=True, axes_class=axes_class, **kwargs)
519-
elif position == "right":
520-
ax = self.new_horizontal(
521-
size, pad, pack_start=False, axes_class=axes_class, **kwargs)
522-
elif position == "bottom":
523-
ax = self.new_vertical(
524-
size, pad, pack_start=True, axes_class=axes_class, **kwargs)
525-
else: # "top"
526-
ax = self.new_vertical(
527-
size, pad, pack_start=False, axes_class=axes_class, **kwargs)
519+
create_axes, pack_start = _api.check_getitem({
520+
"left": (self.new_horizontal, True),
521+
"right": (self.new_horizontal, False),
522+
"bottom": (self.new_vertical, True),
523+
"top": (self.new_vertical, False),
524+
}, position=position)
525+
ax = create_axes(
526+
size, pad, pack_start=pack_start, axes_class=axes_class, **kwargs)
528527
if add_to_figure:
529528
self._fig.add_axes(ax)
530529
return ax

lib/mpl_toolkits/axes_grid1/axes_grid.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def set_label_mode(self, mode):
271271
- "1": Only the bottom left axes is labelled.
272272
- "all": all axes are labelled.
273273
"""
274+
_api.check_in_list(["all", "L", "1"], mode=mode)
274275
if mode == "all":
275276
for ax in self.axes_all:
276277
_tick_only(ax, False, False)
@@ -291,7 +292,7 @@ def set_label_mode(self, mode):
291292
ax = col[-1]
292293
_tick_only(ax, bottom_on=False, left_on=True)
293294

294-
elif mode == "1":
295+
else: # "1"
295296
for ax in self.axes_all:
296297
_tick_only(ax, bottom_on=True, left_on=True)
297298

@@ -379,6 +380,10 @@ def __init__(self, fig,
379380
to associated *cbar_axes*.
380381
axes_class : subclass of `matplotlib.axes.Axes`, default: None
381382
"""
383+
_api.check_in_list(["each", "single", "edge", None],
384+
cbar_mode=cbar_mode)
385+
_api.check_in_list(["left", "right", "bottom", "top"],
386+
cbar_location=cbar_location)
382387
self._colorbar_mode = cbar_mode
383388
self._colorbar_location = cbar_location
384389
self._colorbar_pad = cbar_pad

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from mpl_toolkits.axes_grid1.anchored_artists import (
1818
AnchoredSizeBar, AnchoredDirectionArrows)
1919
from mpl_toolkits.axes_grid1.axes_divider import (
20-
HBoxDivider, make_axes_area_auto_adjustable)
20+
Divider, HBoxDivider, make_axes_area_auto_adjustable)
2121
from mpl_toolkits.axes_grid1.axes_rgb import RGBAxes
2222
from mpl_toolkits.axes_grid1.inset_locator import (
2323
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch)
@@ -518,6 +518,18 @@ def test_grid_errors(rect, ngrids, error, message):
518518
Grid(fig, rect, (2, 3), ngrids=ngrids)
519519

520520

521+
@pytest.mark.parametrize('anchor, error, message', (
522+
(None, TypeError, "anchor must be str"),
523+
("CC", ValueError, "'CC' is not a valid value for anchor"),
524+
((1, 1, 1), TypeError, "anchor must be str"),
525+
))
526+
def test_divider_errors(anchor, error, message):
527+
fig = plt.figure()
528+
with pytest.raises(error, match=message):
529+
Divider(fig, [0, 0, 1, 1], [Size.Fixed(1)], [Size.Fixed(1)],
530+
anchor=anchor)
531+
532+
521533
@check_figures_equal(extensions=["png"])
522534
def test_mark_inset_unstales_viewlim(fig_test, fig_ref):
523535
inset, full = fig_test.subplots(1, 2)

0 commit comments

Comments
 (0)