Skip to content

Commit

Permalink
Fix invalid rank error (#12)
Browse files Browse the repository at this point in the history
The previous implementation of the logic to catch whether a `rank` passed to `partition.indexers` was valid was incorrect.  The minus one is on the wrong side of the inequality; it should be either `rank > ranks - 1`, or, as I've implemented things now, `rank >= ranks`.  This PR adds a test and fixes this issue.  

In practice this was not a big deal, because it just meant that sometimes we would not trigger the error when we should have.
  • Loading branch information
spencerkclark authored May 5, 2021
1 parent 9af1c6c commit ef9a665
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions test_xpartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,10 @@ def test_zeros_like():
# assert_identical does not check dtype or chunks
assert result[var].dtype == expected[var].dtype
assert result[var].chunks == expected[var].chunks


def test_partition_indexers_invalid_rank_error():
data = dask.array.zeros((6, 4), chunks=((6, 4)))
da = xr.DataArray(data, dims=["x", "y"])
with pytest.raises(ValueError, match="greater than maximum rank"):
da.partition.indexers(1, 1, ["x"])
4 changes: 2 additions & 2 deletions xpartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ def indexers(self, ranks: int, rank: int, dims: Sequence[Hashable]) -> Region:
-------
Dict[Hashable, slice]
"""
if (rank - 1) > ranks:
raise ValueError(f"Rank {rank} is greater than available ranks {ranks}.")
if rank >= ranks:
raise ValueError(f"Rank {rank} is greater than maximum rank {ranks - 1}.")

meta_chunk_sizes = self._optimal_meta_chunk_sizes(ranks, dims)
meta_array = self._meta_array(meta_chunk_sizes)
Expand Down

0 comments on commit ef9a665

Please sign in to comment.