Skip to content

Commit 49789a6

Browse files
authored
Account for endian being possibly None in convert_v3_to_v2_metadata (#787)
1 parent 661ae45 commit 49789a6

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

virtualizarr/tests/test_writers/test_kerchunk.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,36 @@ def test_accessor_to_kerchunk_parquet(self, tmp_path, array_v3_metadata):
193193
}
194194

195195

196-
@pytest.mark.parametrize("endian,expected_dtype_char", [("little", "<"), ("big", ">")])
196+
@pytest.mark.parametrize(
197+
["dtype", "endian", "expected_dtype_char"],
198+
[("i8", "little", "<"), ("i8", "big", ">"), ("i1", None, "|")],
199+
)
197200
def test_convert_v3_to_v2_metadata(
198-
array_v3_metadata, endian: str, expected_dtype_char: str
201+
array_v3_metadata, dtype: str, endian: str | None, expected_dtype_char: str
199202
):
200203
shape = (5, 20)
201204
chunks = (5, 10)
205+
202206
codecs = [
203207
{"name": "bytes", "configuration": {"endian": endian}},
204208
{
205209
"name": "numcodecs.delta",
206-
"configuration": {"dtype": f"{expected_dtype_char}i8"},
210+
"configuration": {"dtype": f"{expected_dtype_char}{dtype}"},
207211
},
208212
{
209213
"name": "numcodecs.blosc",
210214
"configuration": {"cname": "zstd", "clevel": 5, "shuffle": 1},
211215
},
212216
]
213217

214-
v3_metadata = array_v3_metadata(shape=shape, chunks=chunks, codecs=codecs)
218+
v3_metadata = array_v3_metadata(
219+
data_type=np.dtype(dtype), shape=shape, chunks=chunks, codecs=codecs
220+
)
215221
v2_metadata = convert_v3_to_v2_metadata(v3_metadata)
216222

217223
assert isinstance(v2_metadata, ArrayV2Metadata)
218224
assert v2_metadata.shape == shape
219-
expected_dtype = np.dtype(f"{expected_dtype_char}i4") # assuming int32
225+
expected_dtype = np.dtype(f"{expected_dtype_char}{dtype}")
220226
assert v2_metadata.dtype.to_native_dtype() == expected_dtype
221227
assert v2_metadata.chunks == chunks
222228
assert v2_metadata.fill_value == 0
@@ -232,7 +238,7 @@ def test_convert_v3_to_v2_metadata(
232238

233239
filters_config = filter_codec.get_config()
234240
assert filters_config["id"] == "delta"
235-
expected_delta_dtype = f"{expected_dtype_char}i8"
241+
expected_delta_dtype = f"{expected_dtype_char}{dtype}"
236242
assert filters_config["dtype"] == expected_delta_dtype
237243
assert filters_config["astype"] == expected_delta_dtype
238244
assert v2_metadata.attributes == {}

virtualizarr/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def convert_v3_to_v2_metadata(
145145
# but other codec pipelines could store endianness elsewhere.
146146
big_endian = any(
147147
isinstance(codec, ArrayBytesCodec)
148-
and hasattr(codec, "endian")
149-
and codec.endian.value == "big"
148+
and getattr(codec, "endian", None) is not None
149+
and codec.endian.value == "big" # type: ignore[attr-defined]
150150
for codec in v3_metadata.codecs
151151
)
152152
if big_endian:

0 commit comments

Comments
 (0)