From 433f78dc3ea073d54d8fa36f1574c7e74a3b49db Mon Sep 17 00:00:00 2001 From: Sam Levang <39069044+slevang@users.noreply.github.com> Date: Thu, 9 Nov 2023 23:40:29 -0500 Subject: [PATCH] Change default write mode of `to_zarr()` to `'w-'` (#275) * change default to_zarr mode to w- * regression test * add whats new entry --- datatree/datatree.py | 9 +++++++-- datatree/io.py | 2 +- datatree/tests/test_io.py | 9 +++++++++ docs/source/whats-new.rst | 4 ++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/datatree/datatree.py b/datatree/datatree.py index 52049f47..c86c2e2e 100644 --- a/datatree/datatree.py +++ b/datatree/datatree.py @@ -1496,7 +1496,12 @@ def to_netcdf( ) def to_zarr( - self, store, mode: str = "w", encoding=None, consolidated: bool = True, **kwargs + self, + store, + mode: str = "w-", + encoding=None, + consolidated: bool = True, + **kwargs, ): """ Write datatree contents to a Zarr store. @@ -1505,7 +1510,7 @@ def to_zarr( ---------- store : MutableMapping, str or Path, optional Store or path to directory in file system - mode : {{"w", "w-", "a", "r+", None}, default: "w" + mode : {{"w", "w-", "a", "r+", None}, default: "w-" Persistence mode: “w” means create (overwrite if exists); “w-” means create (fail if exists); “a” means override existing variables (create if does not exist); “r+” means modify existing array values only (raise an error if any metadata or shapes would change). The default mode diff --git a/datatree/io.py b/datatree/io.py index 73992f13..8bb7682f 100644 --- a/datatree/io.py +++ b/datatree/io.py @@ -176,7 +176,7 @@ def _create_empty_zarr_group(store, group, mode): def _datatree_to_zarr( dt: DataTree, store, - mode: str = "w", + mode: str = "w-", encoding=None, consolidated: bool = True, **kwargs, diff --git a/datatree/tests/test_io.py b/datatree/tests/test_io.py index 59199371..6fa20479 100644 --- a/datatree/tests/test_io.py +++ b/datatree/tests/test_io.py @@ -1,4 +1,5 @@ import pytest +import zarr.errors from datatree.io import open_datatree from datatree.testing import assert_equal @@ -109,3 +110,11 @@ def test_to_zarr_not_consolidated(self, tmpdir, simple_datatree): with pytest.warns(RuntimeWarning, match="consolidated"): roundtrip_dt = open_datatree(filepath, engine="zarr") assert_equal(original_dt, roundtrip_dt) + + @requires_zarr + def test_to_zarr_default_write_mode(self, tmpdir, simple_datatree): + simple_datatree.to_zarr(tmpdir) + + # with default settings, to_zarr should not overwrite an existing dir + with pytest.raises(zarr.errors.ContainsGroupError): + simple_datatree.to_zarr(tmpdir) diff --git a/docs/source/whats-new.rst b/docs/source/whats-new.rst index a72bfdca..fe1ac57f 100644 --- a/docs/source/whats-new.rst +++ b/docs/source/whats-new.rst @@ -26,6 +26,10 @@ New Features Breaking changes ~~~~~~~~~~~~~~~~ +- Change default write mode of :py:meth:`DataTree.to_zarr` to ``'w-'`` to match ``xarray`` + default and prevent accidental directory overwrites. (:issue:`274`, :pull:`275`) + By `Sam Levang `_. + Deprecations ~~~~~~~~~~~~