Skip to content

Commit 0bc686c

Browse files
Enhance the ugly error in constructor when no data passed (#8920)
* Enhance the ugly error in constructor when no data passed * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix mypy issues * Get unpacked data to be used downstream --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a07e16c commit 0bc686c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

xarray/core/variable.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,18 @@ def as_variable(
123123
if isinstance(obj, Variable):
124124
obj = obj.copy(deep=False)
125125
elif isinstance(obj, tuple):
126-
if isinstance(obj[1], DataArray):
126+
try:
127+
dims_, data_, *attrs = obj
128+
except ValueError:
129+
raise ValueError(f"Tuple {obj} is not in the form (dims, data[, attrs])")
130+
131+
if isinstance(data_, DataArray):
127132
raise TypeError(
128133
f"Variable {name!r}: Using a DataArray object to construct a variable is"
129134
" ambiguous, please extract the data using the .data property."
130135
)
131136
try:
132-
obj = Variable(*obj)
137+
obj = Variable(dims_, data_, *attrs)
133138
except (TypeError, ValueError) as error:
134139
raise error.__class__(
135140
f"Variable {name!r}: Could not convert tuple of form "

0 commit comments

Comments
 (0)