From 6b2a5b18ec467eabb5299e02e5e09a2af915107f Mon Sep 17 00:00:00 2001 From: Aimilios Tsouvelekakis Date: Mon, 8 Apr 2024 17:35:54 +0300 Subject: [PATCH 1/4] Enhance the ugly error in constructor when no data passed --- xarray/core/variable.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 1f18f61441c..ae84cdaafd8 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -123,6 +123,13 @@ def as_variable( if isinstance(obj, Variable): obj = obj.copy(deep=False) elif isinstance(obj, tuple): + try: + dims, data, *attrs = obj + except ValueError: + raise ValueError( + f"Tuple {obj} is not in the form (dims, data[, attrs])" + ) + if isinstance(obj[1], DataArray): raise TypeError( f"Variable {name!r}: Using a DataArray object to construct a variable is" From 3c0c5786966ad61f6d79dbbbdfa0a5a70b53dc84 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:45:19 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/core/variable.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index ae84cdaafd8..10ade0ac245 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -126,10 +126,8 @@ def as_variable( try: dims, data, *attrs = obj except ValueError: - raise ValueError( - f"Tuple {obj} is not in the form (dims, data[, attrs])" - ) - + raise ValueError(f"Tuple {obj} is not in the form (dims, data[, attrs])") + if isinstance(obj[1], DataArray): raise TypeError( f"Variable {name!r}: Using a DataArray object to construct a variable is" From 3dc721559528c0f40a12a5604a881bf7adc25479 Mon Sep 17 00:00:00 2001 From: Aimilios Tsouvelekakis Date: Mon, 8 Apr 2024 20:18:45 +0300 Subject: [PATCH 3/4] Fix mypy issues --- xarray/core/variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 10ade0ac245..a4c1d88bb16 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -124,7 +124,7 @@ def as_variable( obj = obj.copy(deep=False) elif isinstance(obj, tuple): try: - dims, data, *attrs = obj + dims_, data_, *attrs = obj except ValueError: raise ValueError(f"Tuple {obj} is not in the form (dims, data[, attrs])") From 740606b54dfece2a5be8239dab0f87e7f9f41f78 Mon Sep 17 00:00:00 2001 From: Aimilios Tsouvelekakis Date: Thu, 11 Apr 2024 00:24:48 +0300 Subject: [PATCH 4/4] Get unpacked data to be used downstream --- xarray/core/variable.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index a4c1d88bb16..e89cf95411c 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -128,13 +128,13 @@ def as_variable( except ValueError: raise ValueError(f"Tuple {obj} is not in the form (dims, data[, attrs])") - if isinstance(obj[1], DataArray): + if isinstance(data_, DataArray): raise TypeError( f"Variable {name!r}: Using a DataArray object to construct a variable is" " ambiguous, please extract the data using the .data property." ) try: - obj = Variable(*obj) + obj = Variable(dims_, data_, *attrs) except (TypeError, ValueError) as error: raise error.__class__( f"Variable {name!r}: Could not convert tuple of form "