From bb0aac7039de7db5181309d788780f4fd6db0588 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Wed, 23 Aug 2023 07:10:33 +0100 Subject: [PATCH 1/3] Fix tokenize with empty attrs --- xarray/core/variable.py | 2 +- xarray/tests/test_dask.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index c89545c43ae..ce53172afa0 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..95e3b371b16 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): + # 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 From 1a9646874fe15d5e2e02fe737cfbb0e256a279e0 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Wed, 23 Aug 2023 07:28:48 +0100 Subject: [PATCH 2/3] docs --- doc/whats-new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 4cd24a54fc8..41ff19395fb 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -34,7 +34,8 @@ Deprecations Bug fixes ~~~~~~~~~ - +- Fix bug where empty attrs would generate inconsistent tokens (:issue:`6970`, :pull:`8101`). + By `Mattia Almansi `_. Documentation ~~~~~~~~~~~~~ From 693e52c436db99f462afc98f5bb3c32ea19acd87 Mon Sep 17 00:00:00 2001 From: Mattia Almansi Date: Wed, 23 Aug 2023 14:27:03 +0100 Subject: [PATCH 3/3] typing Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/tests/test_dask.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 95e3b371b16..1c2511427ac 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -299,7 +299,7 @@ def test_persist(self): self.assertLazyAndAllClose(u + 1, v) self.assertLazyAndAllClose(u + 1, v2) - def test_tokenize_empty_attrs(self): + def test_tokenize_empty_attrs(self) -> None: # Issue #6970 assert self.eager_var._attrs is None expected = dask.base.tokenize(self.eager_var)