Skip to content

Commit 4946489

Browse files
authored
Change default write mode of to_zarr() to 'w-' xarray-contrib/datatree#275
* change default to_zarr mode to w- * regression test * add whats new entry
1 parent 1a73cb9 commit 4946489

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

datatree/datatree.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,12 @@ def to_netcdf(
14961496
)
14971497

14981498
def to_zarr(
1499-
self, store, mode: str = "w", encoding=None, consolidated: bool = True, **kwargs
1499+
self,
1500+
store,
1501+
mode: str = "w-",
1502+
encoding=None,
1503+
consolidated: bool = True,
1504+
**kwargs,
15001505
):
15011506
"""
15021507
Write datatree contents to a Zarr store.
@@ -1505,7 +1510,7 @@ def to_zarr(
15051510
----------
15061511
store : MutableMapping, str or Path, optional
15071512
Store or path to directory in file system
1508-
mode : {{"w", "w-", "a", "r+", None}, default: "w"
1513+
mode : {{"w", "w-", "a", "r+", None}, default: "w-"
15091514
Persistence mode: “w” means create (overwrite if exists); “w-” means create (fail if exists);
15101515
“a” means override existing variables (create if does not exist); “r+” means modify existing
15111516
array values only (raise an error if any metadata or shapes would change). The default mode

datatree/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _create_empty_zarr_group(store, group, mode):
176176
def _datatree_to_zarr(
177177
dt: DataTree,
178178
store,
179-
mode: str = "w",
179+
mode: str = "w-",
180180
encoding=None,
181181
consolidated: bool = True,
182182
**kwargs,

datatree/tests/test_io.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import zarr.errors
23

34
from datatree.io import open_datatree
45
from datatree.testing import assert_equal
@@ -109,3 +110,11 @@ def test_to_zarr_not_consolidated(self, tmpdir, simple_datatree):
109110
with pytest.warns(RuntimeWarning, match="consolidated"):
110111
roundtrip_dt = open_datatree(filepath, engine="zarr")
111112
assert_equal(original_dt, roundtrip_dt)
113+
114+
@requires_zarr
115+
def test_to_zarr_default_write_mode(self, tmpdir, simple_datatree):
116+
simple_datatree.to_zarr(tmpdir)
117+
118+
# with default settings, to_zarr should not overwrite an existing dir
119+
with pytest.raises(zarr.errors.ContainsGroupError):
120+
simple_datatree.to_zarr(tmpdir)

docs/source/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ New Features
2626
Breaking changes
2727
~~~~~~~~~~~~~~~~
2828

29+
- Change default write mode of :py:meth:`DataTree.to_zarr` to ``'w-'`` to match ``xarray``
30+
default and prevent accidental directory overwrites. (:issue:`274`, :pull:`275`)
31+
By `Sam Levang <https://github.com/slevang>`_.
32+
2933
Deprecations
3034
~~~~~~~~~~~~
3135

0 commit comments

Comments
 (0)