Skip to content

Commit 982d7c9

Browse files
committed
Fix regressed tests
1 parent a0316a0 commit 982d7c9

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

ultraplot/tests/test_gridspec.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
from ultraplot.gridspec import SubplotGrid
55

66

7+
def _singleton_axis(obj):
8+
if isinstance(obj, SubplotGrid):
9+
assert len(obj) == 1
10+
return obj[0]
11+
return obj
12+
13+
714
def test_grid_has_dynamic_methods():
815
"""
916
Check that we can apply the methods to a SubplotGrid object.
@@ -135,8 +142,9 @@ def test_gridspec_spanning_slice_deduplicates_axes():
135142

136143
# The first two slots in the top row refer to the same spanning subplot.
137144
ax = axs[0, :2]
138-
assert isinstance(ax, uplt.axes.Axes)
139-
assert ax is axs[0, 0]
145+
assert isinstance(ax, uplt.SubplotGrid)
146+
assert len(ax) == 1
147+
assert _singleton_axis(ax) is _singleton_axis(axs[0, 0])
140148

141149
data = np.array([[0.1, 0.2], [0.4, 0.5], [0.7, 0.8]])
142150
ax.scatter(data[:, 0], data[:, 1], c="grey", label="data", legend=True)
@@ -152,8 +160,8 @@ def test_return_type_after_indexing():
152160
Inexing should always return a SubplotGrid even if we have 1 element
153161
"""
154162
fig, axs = uplt.subplots(ncols=2, nrows=2)
155-
assert axs[1, 0:] is uplt.SubplotGrid
163+
assert isinstance(axs[1, 0:], uplt.SubplotGrid)
156164
assert len(axs[1, 0:]) == 2
157165

158-
assert axs[1, 1:] is uplt.SubplotGrid
166+
assert isinstance(axs[1, 1:], uplt.SubplotGrid)
159167
assert len(axs[1, 1:]) == 1

ultraplot/tests/test_legend.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,29 +1355,33 @@ def test_legend_span_inference_with_multi_panels(
13551355
def test_legend_best_axis_selection_right_left():
13561356
fig, axs = uplt.subplots(nrows=1, ncols=3)
13571357
axs.plot([0, 1], [0, 1], label="line")
1358-
ref = [axs[0, 0], axs[0, 2]]
1358+
left = _anchor_axis(axs[0, 0])
1359+
right = _anchor_axis(axs[0, 2])
1360+
ref = [left, right]
13591361

13601362
fig.legend(ref=ref, loc="r", rows=1)
1361-
assert len(axs[0, 2]._panel_dict["right"]) == 1
1362-
assert len(axs[0, 0]._panel_dict["right"]) == 0
1363+
assert len(right._panel_dict["right"]) == 1
1364+
assert len(left._panel_dict["right"]) == 0
13631365

13641366
fig.legend(ref=ref, loc="l", rows=1)
1365-
assert len(axs[0, 0]._panel_dict["left"]) == 1
1366-
assert len(axs[0, 2]._panel_dict["left"]) == 0
1367+
assert len(left._panel_dict["left"]) == 1
1368+
assert len(right._panel_dict["left"]) == 0
13671369

13681370

13691371
def test_legend_best_axis_selection_top_bottom():
13701372
fig, axs = uplt.subplots(nrows=2, ncols=1)
13711373
axs.plot([0, 1], [0, 1], label="line")
1372-
ref = [axs[0, 0], axs[1, 0]]
1374+
top = _anchor_axis(axs[0, 0])
1375+
bottom = _anchor_axis(axs[1, 0])
1376+
ref = [top, bottom]
13731377

13741378
fig.legend(ref=ref, loc="t", cols=1)
1375-
assert len(axs[0, 0]._panel_dict["top"]) == 1
1376-
assert len(axs[1, 0]._panel_dict["top"]) == 0
1379+
assert len(top._panel_dict["top"]) == 1
1380+
assert len(bottom._panel_dict["top"]) == 0
13771381

13781382
fig.legend(ref=ref, loc="b", cols=1)
1379-
assert len(axs[1, 0]._panel_dict["bottom"]) == 1
1380-
assert len(axs[0, 0]._panel_dict["bottom"]) == 0
1383+
assert len(bottom._panel_dict["bottom"]) == 1
1384+
assert len(top._panel_dict["bottom"]) == 0
13811385

13821386

13831387
def test_legend_span_decode_fallback(monkeypatch):

ultraplot/tests/test_subplots.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
import ultraplot as uplt
1010

1111

12+
def _singleton_axis(obj):
13+
if isinstance(obj, uplt.SubplotGrid):
14+
assert len(obj) == 1
15+
return obj[0]
16+
return obj
17+
18+
1219
@pytest.mark.mpl_image_compare
1320
def test_align_labels():
1421
"""
@@ -354,7 +361,7 @@ def test_subset_share_xlabels_implicit_column():
354361
for axi, lab in fig._supxlabel_dict.items()
355362
if lab.get_text() == "Right-column X"
356363
]
357-
assert label_axes and label_axes[0] is ax[1, 1]
364+
assert label_axes and label_axes[0] is _singleton_axis(ax[1, 1])
358365

359366
uplt.close(fig)
360367

@@ -406,7 +413,7 @@ def test_subset_share_ylabels_implicit_row():
406413
label_axes = [
407414
axi for axi, lab in fig._supylabel_dict.items() if lab.get_text() == "Top-row Y"
408415
]
409-
assert label_axes and label_axes[0] is ax[0, 0]
416+
assert label_axes and label_axes[0] is _singleton_axis(ax[0, 0])
410417

411418
uplt.close(fig)
412419

@@ -493,7 +500,7 @@ def test_subset_share_xlabels_implicit_column_top():
493500
for axi, lab in fig._supxlabel_dict.items()
494501
if lab.get_text() == "Right-column X (top)"
495502
]
496-
assert label_axes and label_axes[0] is ax[0, 1]
503+
assert label_axes and label_axes[0] is _singleton_axis(ax[0, 1])
497504

498505
uplt.close(fig)
499506

@@ -512,7 +519,7 @@ def test_subset_share_ylabels_implicit_row_right():
512519
for axi, lab in fig._supylabel_dict.items()
513520
if lab.get_text() == "Top-row Y (right)"
514521
]
515-
assert label_axes and label_axes[0] is ax[0, 1]
522+
assert label_axes and label_axes[0] is _singleton_axis(ax[0, 1])
516523

517524
uplt.close(fig)
518525

0 commit comments

Comments
 (0)