Repository navigation
Dataset: Raise DimensionError like Variable & DataArray when indexed - #3926
Conversation
`dim_extent` for `Dataset` used a manual `contains` check and returned -1 as a sentinel when the dimension was absent. This caused `get_slice` to call `throw_index_error(-1)`, producing the nonsensical message "Dimension size is -1 and the allowed range is [1:-2]". For `Variable` and `DataArray`, `object.dims()[dim]` already delegates to `small_stable_map::operator[]`, which calls `throw_dimension_not_found_error` and raises `DimensionError` when the dimension is not found. The Dataset branch now does the same via `object.sizes()[dim]`, making all three types consistent. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
|
This seems correct to me. But I would like to wait for @SimonHeybrock to be back from holiday to see if there was an underlying reason why we return -1 if the dim is not found. Is that ok with you? |
If you ask me, I guess yea I won't mind - I'm not in a hurry in general and this PR is only blocking the merge of #3925, not the review of it. |
I didn't think there was an underlying reason, Claude confirms: I found the origin of it. Here's the story: It just fell out of the implementation — it wasn't a deliberate design choice. The // TODO We really need a way to get the dimension labels and extents from
// Dataset (not using class Dimensions).
template <class T> auto dim_extent(const T &object, const Dim dim) {
if constexpr (std::is_same_v<T, Dataset> || std::is_same_v<T, DatasetProxy>) {
scipp::index extent = -1;
for (const auto & [ key, item ] : object) {
if (item.dims().contains(dim)) {
if (extent == -1)
extent = item.dims()[dim];
else if (item.dims()[dim] == extent - 1)
--extent;
}
}
return extent;
} else {
return object.dims()[dim];
}
}At that point Later (commit scipp::index extent = -1;
if (object.sizes().contains(dim))
extent = object.sizes().at(dim);
return extent;This carried the PR #3926 fixes this by just returning |
Found out this is needed while working on tests for #3925. Commits were written by Claude-code, with my own instructions verification & guidance.