|
| 1 | +import re |
1 | 2 | from typing import Any |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | import pytest |
5 | 6 |
|
| 7 | +from tests.test_codecs.conftest import ExpectErr |
6 | 8 | from zarr.core.chunk_grids import ( |
7 | 9 | _guess_regular_chunks, |
8 | 10 | normalize_chunks_nd, |
@@ -141,6 +143,49 @@ def test_normalize_chunks_1d_errors() -> None: |
141 | 143 | normalize_chunks_1d([10, 20], 100) |
142 | 144 |
|
143 | 145 |
|
| 146 | +@pytest.mark.parametrize( |
| 147 | + "case", |
| 148 | + [ |
| 149 | + # The motivating case: nested/RLE form for a single dim. |
| 150 | + ExpectErr( |
| 151 | + input=([[3, 3], 1], 7), |
| 152 | + msg="non-integer element(s) ([3, 3],) at indices (0,)", |
| 153 | + exception_cls=TypeError, |
| 154 | + ), |
| 155 | + # Multiple non-int elements report all offending indices. |
| 156 | + ExpectErr( |
| 157 | + input=([1, [2, 2], 1, [3]], 9), |
| 158 | + msg="non-integer element(s) ([2, 2], [3]) at indices (1, 3)", |
| 159 | + exception_cls=TypeError, |
| 160 | + ), |
| 161 | + # Strings are also non-integers and should be reported the same way. |
| 162 | + ExpectErr( |
| 163 | + input=([2, "3", 5], 10), |
| 164 | + msg="non-integer element(s) ('3',) at indices (1,)", |
| 165 | + exception_cls=TypeError, |
| 166 | + ), |
| 167 | + ], |
| 168 | + ids=["rle-single-dim", "multiple-non-ints", "string-element"], |
| 169 | +) |
| 170 | +def test_normalize_chunks_1d_rejects_non_int_elements( |
| 171 | + case: ExpectErr[tuple[list[Any], int]], |
| 172 | +) -> None: |
| 173 | + """Reject nested/RLE-style chunk specs with a precise error pointing at offending indices.""" |
| 174 | + from zarr.core.chunk_grids import normalize_chunks_1d |
| 175 | + |
| 176 | + chunks, span = case.input |
| 177 | + with pytest.raises(case.exception_cls, match=re.escape(case.msg)): |
| 178 | + normalize_chunks_1d(chunks, span=span) |
| 179 | + |
| 180 | + |
| 181 | +def test_normalize_chunks_nd_rejects_rle_inner_dim() -> None: |
| 182 | + """End-to-end: a per-dim RLE form like [[3, 3], 1] surfaces the precise error.""" |
| 183 | + with pytest.raises( |
| 184 | + TypeError, match=re.escape("non-integer element(s) ([3, 3],) at indices (0,)") |
| 185 | + ): |
| 186 | + normalize_chunks_nd([[6, 4], [[3, 3], 1]], (10, 10)) |
| 187 | + |
| 188 | + |
144 | 189 | def test_normalize_chunks_errors() -> None: |
145 | 190 | with pytest.raises(ValueError, match="does not accept None"): |
146 | 191 | normalize_chunks_nd(None, (100,)) |
|
0 commit comments