Skip to content
forked from pydata/xarray

Commit 7c76fe7

Browse files
committed
Don't allow overwriting indexes with region writes
Closes pydata#8589
1 parent 6af547c commit 7c76fe7

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

xarray/backends/api.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,14 +1572,11 @@ def _validate_and_autodetect_region(
15721572
raise TypeError(f"``region`` must be a dict, got {type(region)}")
15731573

15741574
if any(v == "auto" for v in region.values()):
1575-
region_was_autodetected = True
15761575
if mode != "r+":
15771576
raise ValueError(
15781577
f"``mode`` must be 'r+' when using ``region='auto'``, got {mode}"
15791578
)
15801579
region = _auto_detect_regions(ds, region, open_kwargs)
1581-
else:
1582-
region_was_autodetected = False
15831580

15841581
for k, v in region.items():
15851582
if k not in ds.dims:
@@ -1612,7 +1609,7 @@ def _validate_and_autodetect_region(
16121609
f".drop_vars({non_matching_vars!r})"
16131610
)
16141611

1615-
return region, region_was_autodetected
1612+
return region
16161613

16171614

16181615
def _validate_datatypes_for_zarr_append(zstore, dataset):
@@ -1784,12 +1781,9 @@ def to_zarr(
17841781
storage_options=storage_options,
17851782
zarr_version=zarr_version,
17861783
)
1787-
region, region_was_autodetected = _validate_and_autodetect_region(
1788-
dataset, region, mode, open_kwargs
1789-
)
1790-
# drop indices to avoid potential race condition with auto region
1791-
if region_was_autodetected:
1792-
dataset = dataset.drop_vars(dataset.indexes)
1784+
region = _validate_and_autodetect_region(dataset, region, mode, open_kwargs)
1785+
# can't modify indexed with region writes
1786+
dataset = dataset.drop_vars(dataset.indexes)
17931787
if append_dim is not None and append_dim in region:
17941788
raise ValueError(
17951789
f"cannot list the same dimension in both ``append_dim`` and "

xarray/tests/test_backends.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5641,24 +5641,26 @@ def test_zarr_region_index_write(self, tmp_path):
56415641
}
56425642
)
56435643

5644-
ds_region = 1 + ds.isel(x=slice(2, 4), y=slice(6, 8))
5644+
region_slice = dict(x=slice(2, 4), y=slice(6, 8))
5645+
ds_region = 1 + ds.isel(region_slice)
56455646

56465647
ds.to_zarr(tmp_path / "test.zarr")
56475648

5648-
with patch.object(
5649-
ZarrStore,
5650-
"set_variables",
5651-
side_effect=ZarrStore.set_variables,
5652-
autospec=True,
5653-
) as mock:
5654-
ds_region.to_zarr(tmp_path / "test.zarr", region="auto", mode="r+")
5655-
5656-
# should write the data vars but never the index vars with auto mode
5657-
for call in mock.call_args_list:
5658-
written_variables = call.args[1].keys()
5659-
assert "test" in written_variables
5660-
assert "x" not in written_variables
5661-
assert "y" not in written_variables
5649+
for region in [region_slice, "auto"]:
5650+
with patch.object(
5651+
ZarrStore,
5652+
"set_variables",
5653+
side_effect=ZarrStore.set_variables,
5654+
autospec=True,
5655+
) as mock:
5656+
ds_region.to_zarr(tmp_path / "test.zarr", region=region, mode="r+")
5657+
5658+
# should write the data vars but never the index vars with auto mode
5659+
for call in mock.call_args_list:
5660+
written_variables = call.args[1].keys()
5661+
assert "test" in written_variables
5662+
assert "x" not in written_variables
5663+
assert "y" not in written_variables
56625664

56635665
def test_zarr_region_append(self, tmp_path):
56645666
x = np.arange(0, 50, 10)

0 commit comments

Comments
 (0)