diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b83697a3b20..7762edfcef2 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -50,6 +50,8 @@ Deprecations Bug fixes ~~~~~~~~~ +- Fix bug where empty attrs would generate inconsistent tokens (:issue:`6970`, :pull:`8101`). + By `Mattia Almansi `_. - Improved handling of multi-coordinate indexes when updating coordinates, including bug fixes (and improved warnings for deprecated features) for pandas multi-indexes (:pull:`8094`). By `BenoƮt Bovy `_. diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 05f9930aacd..a1e11b86f7e 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -566,7 +566,7 @@ def __dask_tokenize__(self): # around NetCDF and the like from dask.base import normalize_token - return normalize_token((type(self), self._dims, self.data, self._attrs)) + return normalize_token((type(self), self._dims, self.data, self.attrs)) def __dask_graph__(self): if is_duck_dask_array(self._data): diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 6e65d52fdb5..1c2511427ac 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -299,6 +299,17 @@ def test_persist(self): self.assertLazyAndAllClose(u + 1, v) self.assertLazyAndAllClose(u + 1, v2) + def test_tokenize_empty_attrs(self) -> None: + # Issue #6970 + assert self.eager_var._attrs is None + expected = dask.base.tokenize(self.eager_var) + assert self.eager_var.attrs == self.eager_var._attrs == {} + assert ( + expected + == dask.base.tokenize(self.eager_var) + == dask.base.tokenize(self.lazy_var.compute()) + ) + @requires_pint def test_tokenize_duck_dask_array(self): import pint