diff --git a/versioned_hdf5/tests/test_wrappers.py b/versioned_hdf5/tests/test_wrappers.py index 07c26954..171fa9f8 100644 --- a/versioned_hdf5/tests/test_wrappers.py +++ b/versioned_hdf5/tests/test_wrappers.py @@ -7,6 +7,12 @@ from ..wrappers import InMemoryArrayDataset, InMemoryGroup, InMemorySparseDataset +@pytest.fixture() +def premade_group(h5file): + group = h5file.create_group("group") + return InMemoryGroup(group.id) + + def test_InMemoryArrayDataset(h5file): group = h5file.create_group("group") parent = InMemoryGroup(group.id) @@ -107,3 +113,24 @@ def test_InMemoryArrayDataset_resize_multidimension(oldshape, newshape, h5file): ) h5file["data"].resize(newshape) assert_equal(dataset[()], h5file["data"][()], str(newshape)) + + +def test_group_repr(premade_group): + """Test that repr(InMemoryGroup) also shows reprs of child objects.""" + foo = premade_group.create_dataset( + "foo", + data=np.array([1, 2, 3, 4, 5, np.nan]), + ) + bar = premade_group.create_dataset( + "bar", + data=np.array([1, 2, 3, 4, 5, np.nan]), + ) + baz = premade_group.create_dataset( + "baz", + data=np.array([1, 2, 3, 4, 5, np.nan]), + ) + + result = repr(premade_group) + assert repr(foo) in result + assert repr(bar) in result + assert repr(baz) in result diff --git a/versioned_hdf5/wrappers.py b/versioned_hdf5/wrappers.py index b109dce3..c730dd44 100644 --- a/versioned_hdf5/wrappers.py +++ b/versioned_hdf5/wrappers.py @@ -6,6 +6,7 @@ """ import posixpath +import textwrap import warnings from collections import defaultdict from weakref import WeakValueDictionary @@ -66,15 +67,16 @@ def close(self): # Based on Group.__repr__ def __repr__(self): - namestr = ('"%s"' % self.name) if self.name is not None else "(anonymous)" + namestr = f'"{self.name}"' if self.name is not None else "(anonymous)" if not self: - r = "" - elif self._committed: - r = "" % namestr - else: - r = "" % (namestr, len(self)) + return "" + if self._committed: + return f"" - return r + text = [f""] + for item in self.values(): + text.append(textwrap.indent(repr(item), prefix=" ")) + return "\n".join(text) def _check_committed(self): if self._committed: