Skip to content

Commit e73c1a9

Browse files
committed
TST: add tests for ValueError paths
1 parent 591fc21 commit e73c1a9

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/matplotlib/figure.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,10 @@ def _do_layout(gs, layout, unique_ids, nested):
16501650
slc = (slice(start_row, end_row), slice(start_col, end_col))
16511651

16521652
if (layout[slc] != name).any():
1653-
raise ValueError
1653+
raise ValueError(
1654+
f"While trying to layout\n{layout!r}\n"
1655+
f"we found that the label {name!r} specifies an "
1656+
"impossible axes.")
16541657

16551658
if hasattr(name, 'set_subplotspec'):
16561659
ax = name
@@ -1662,13 +1665,17 @@ def _do_layout(gs, layout, unique_ids, nested):
16621665
output[name] = self.add_subplot(
16631666
gs[slc], **{'label': str(name), **subplot_kw})
16641667

1665-
for (j, k), layout in nested.items():
1666-
layout = np.asarray(layout)
1667-
rows, cols = layout.shape
1668+
for (j, k), nested_layout in nested.items():
1669+
nested_layout = np.asarray(nested_layout, dtype=object)
1670+
rows, cols = nested_layout.shape
16681671
gs_n = gs[j, k].subgridspec(rows, cols, **gridspec_kw)
1669-
nested_output = _do_layout(gs_n, layout,
1670-
*_process_layout(layout))
1671-
1672+
nested_output = _do_layout(gs_n, nested_layout,
1673+
*_process_layout(nested_layout))
1674+
if set(output) & set(nested_output):
1675+
overlap = set(output) & set(nested_output)
1676+
raise ValueError(f"There are duplicate keys {overlap} "
1677+
f"between the outer layout\n{layout!r}\n"
1678+
f"and the nested layout\n{nested_layout}")
16721679
output.update(nested_output)
16731680
return output
16741681

lib/matplotlib/tests/test_figure.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,19 @@ def test_subplot_kw(self, fig_test, fig_ref, subplot_kw):
629629
axA = fig_ref.add_subplot(gs[0, 0], **subplot_kw)
630630

631631
axB = fig_ref.add_subplot(gs[0, 1], **subplot_kw)
632+
633+
@pytest.mark.parametrize('x,match', [
634+
([['A', None], [None, 'A']],
635+
"(?m)we found that the label .A. specifies an impossible axes."),
636+
([['A', 'B'], [None, [['A', 'B'], ['C', 'D']]]],
637+
# This more specific test fails 1/3 times
638+
# "There are duplicate keys {.B., .A.} between the outer layout"
639+
"There are duplicate keys .* between the outer layout"
640+
)
641+
])
642+
def test_fail(self, x, match):
643+
fig = plt.figure()
644+
with pytest.raises(
645+
ValueError,
646+
match=match):
647+
fig.build_grid(x)

0 commit comments

Comments
 (0)