strings in compound dtypes

This commit is contained in:
sneakers-the-rat 2024-09-02 22:40:17 -07:00
parent 0f1e0d0caf
commit f28c766b96
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D
2 changed files with 10 additions and 4 deletions

View file

@ -124,8 +124,13 @@ class H5Proxy:
with h5py.File(self.file, "r") as h5f:
obj = h5f.get(self.path)
if self.field is not None:
if h5py.h5t.check_string_dtype(obj.dtype[self.field]):
obj = obj.fields(self.field).asstr()
if encoding := h5py.h5t.check_string_dtype(obj.dtype[self.field]):
if isinstance(item, tuple):
item = (*item, self.field)
else:
item = (item, self.field)
return obj[item].decode(encoding.encoding)
else:
obj = obj.fields(self.field)
else:

View file

@ -157,11 +157,12 @@ def test_compound_dtype(tmp_path):
assert all(instance.array[1] == 2)
def test_strings(hdf5_array):
@pytest.mark.parametrize("compound", [True, False])
def test_strings(hdf5_array, compound):
"""
HDF5 proxy can get and set strings just like any other dtype
"""
array = hdf5_array((10, 10), str)
array = hdf5_array((10, 10), str, compound=compound)
class MyModel(BaseModel):
array: NDArray[Shape["10, 10"], str]