From 10504dbc2331dc56cb55ed1273d017b099d9170a Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 12:42:06 -0800 Subject: [PATCH 1/8] issue PendingDeprecationWarning for bool(ds) `bool(ds)` is frequently mis-interpreted by users and may be deprecated in the future, raising an error. See https://github.com/pydata/xarray/issues/6124 --- xarray/core/dataset.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 3fbc6154c5d..120e97b9e6e 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1449,6 +1449,13 @@ def __len__(self) -> int: return len(self.data_vars) def __bool__(self) -> bool: + warnings.warn( + "coercing a Dataset to a bool be deprecated in the future. Using " + "len(ds) > 0 to check for data_variables or converting to an array with " + "Dataset.to_array to test whether array values are true is encouraged", + PendingDeprecationWarning, + stacklevel=2, + ) return bool(self.data_vars) def __iter__(self) -> Iterator[Hashable]: From 795dfa579d21c3125f9abe094b247f38a7b31ea4 Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 12:57:05 -0800 Subject: [PATCH 2/8] check for PendingDeprecationWarning on bool(ds) in tests --- xarray/tests/test_dataset.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index c8770601c30..40b9b31c7fa 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -544,7 +544,9 @@ def test_properties(self): assert "aasldfjalskdfj" not in ds.variables assert "dim1" in repr(ds.variables) assert len(ds) == 3 - assert bool(ds) + + with pytest.warns(PendingDeprecationWarning): + assert bool(ds) assert list(ds.data_vars) == ["var1", "var2", "var3"] assert list(ds.data_vars.keys()) == ["var1", "var2", "var3"] From 4f50f0461e0df92d5d3c8337b9068188fc5479ac Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 13:00:54 -0800 Subject: [PATCH 3/8] document deprecation warning in whatsnew --- doc/whats-new.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 2572651415d..89070750b41 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -35,6 +35,11 @@ Deprecations By `Tom Nicholas `_. +- Coercing a dataset to bool, e.g. ``bool(ds)``, is being deprecated and will raise an + error in a future version (not yet planned). For now, invoking ``Dataset.__bool__`` + issues a ``PendingDeprecationWarning`` (:issue:`6124`). + By `Michael Delgado `_. + Bug fixes ~~~~~~~~~ - Fix applying function with non-xarray arguments using :py:func:`xr.map_blocks`. From f3eb2f6520015d884478ec6cd1c17ce82e4e2008 Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 13:26:50 -0800 Subject: [PATCH 4/8] add reference to pull request in whats-new.rst --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 89070750b41..2104ef29aa6 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,7 +37,7 @@ Deprecations - Coercing a dataset to bool, e.g. ``bool(ds)``, is being deprecated and will raise an error in a future version (not yet planned). For now, invoking ``Dataset.__bool__`` - issues a ``PendingDeprecationWarning`` (:issue:`6124`). + issues a ``PendingDeprecationWarning`` (:issue:`6124`, :pull:`6126`). By `Michael Delgado `_. Bug fixes From 80b9f47f360530fdf65301f63eb3efef361a2072 Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 13:51:06 -0800 Subject: [PATCH 5/8] update recommendation to bool(ds.data_vars) --- xarray/core/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 120e97b9e6e..e9901cbc79c 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1451,7 +1451,7 @@ def __len__(self) -> int: def __bool__(self) -> bool: warnings.warn( "coercing a Dataset to a bool be deprecated in the future. Using " - "len(ds) > 0 to check for data_variables or converting to an array with " + "bool(ds.data_vars) to check for data_variables or converting to an array with " "Dataset.to_array to test whether array values are true is encouraged", PendingDeprecationWarning, stacklevel=2, From 08c97dbdf8facb8ed4866fe569c55ee655f8eca1 Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 13:55:05 -0800 Subject: [PATCH 6/8] further clarify deprecation warning message --- xarray/core/dataset.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index e9901cbc79c..2ad765eeae5 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1451,8 +1451,9 @@ def __len__(self) -> int: def __bool__(self) -> bool: warnings.warn( "coercing a Dataset to a bool be deprecated in the future. Using " - "bool(ds.data_vars) to check for data_variables or converting to an array with " - "Dataset.to_array to test whether array values are true is encouraged", + "bool(ds.data_vars) to check for the presence of at least on data " + "variable or converting to an array with Dataset.to_array to test " + "whether array values are true is encouraged.", PendingDeprecationWarning, stacklevel=2, ) From b7d655b44e37b7b85c2bcb3d5c16c3870f68e08a Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 13:58:31 -0800 Subject: [PATCH 7/8] fix typo in deprecation warning --- xarray/core/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 2ad765eeae5..c2c4d2d1078 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1451,7 +1451,7 @@ def __len__(self) -> int: def __bool__(self) -> bool: warnings.warn( "coercing a Dataset to a bool be deprecated in the future. Using " - "bool(ds.data_vars) to check for the presence of at least on data " + "bool(ds.data_vars) to check for the presence of at least one data " "variable or converting to an array with Dataset.to_array to test " "whether array values are true is encouraged.", PendingDeprecationWarning, From a9ba93b07f5ac68f443df61bc8087bb3a2fa8188 Mon Sep 17 00:00:00 2001 From: Michael Delgado Date: Wed, 29 Dec 2021 14:02:50 -0800 Subject: [PATCH 8/8] clean up warning language --- xarray/core/dataset.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index c2c4d2d1078..058f2214f92 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1450,9 +1450,9 @@ def __len__(self) -> int: def __bool__(self) -> bool: warnings.warn( - "coercing a Dataset to a bool be deprecated in the future. Using " - "bool(ds.data_vars) to check for the presence of at least one data " - "variable or converting to an array with Dataset.to_array to test " + "coercing a Dataset to a bool will be deprecated. " + "Using bool(ds.data_vars) to check for at least one " + "data variable or using Dataset.to_array to test " "whether array values are true is encouraged.", PendingDeprecationWarning, stacklevel=2,