Skip to content

Commit

Permalink
Enhance the ugly error in constructor when no data passed (#8920)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
aimtsou and pre-commit-ci[bot] authored Apr 10, 2024
1 parent a07e16c commit 0bc686c
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,18 @@ def as_variable(
if isinstance(obj, Variable):
obj = obj.copy(deep=False)
elif isinstance(obj, tuple):
if isinstance(obj[1], DataArray):
try:
dims_, data_, *attrs = obj
except ValueError:
raise ValueError(f"Tuple {obj} is not in the form (dims, data[, attrs])")

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 "
Expand Down

0 comments on commit 0bc686c

Please sign in to comment.