Skip to content

Improve some error messages: apply_ufunc & set_options. #4259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,8 @@ def func(*arrays):
if data.ndim != len(dims):
raise ValueError(
"applied function returned data with unexpected "
"number of dimensions: {} vs {}, for dimensions {}".format(
data.ndim, len(dims), dims
)
f"number of dimensions. Received {data.ndim} dimension(s) but "
f"expected {len(dims)} dimensions with names: {dims!r}"
)

var = Variable(dims, data, fastpath=True)
Expand Down Expand Up @@ -984,21 +983,28 @@ def earth_mover_distance(first_samples,
input_core_dims = ((),) * (len(args))
elif len(input_core_dims) != len(args):
raise ValueError(
"input_core_dims must be None or a tuple with the length same to "
"the number of arguments. Given input_core_dims: {}, "
"number of args: {}.".format(input_core_dims, len(args))
f"input_core_dims must be None or a tuple with the length same to "
f"the number of arguments. "
f"Given {len(input_core_dims)} input_core_dims: {input_core_dims}, "
f" but number of args is {len(args)}."
)

if kwargs is None:
kwargs = {}

signature = _UFuncSignature(input_core_dims, output_core_dims)

if exclude_dims and not exclude_dims <= signature.all_core_dims:
raise ValueError(
"each dimension in `exclude_dims` must also be a "
"core dimension in the function signature"
)
if exclude_dims:
if not isinstance(exclude_dims, set):
raise TypeError(
f"Expected exclude_dims to be a 'set'. Received '{type(exclude_dims).__name__}' instead."
)
if not exclude_dims <= signature.all_core_dims:
raise ValueError(
f"each dimension in `exclude_dims` must also be a "
f"core dimension in the function signature. "
f"Please make {(exclude_dims - signature.all_core_dims)} a core dimension"
)

if kwargs:
func = functools.partial(func, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def __init__(
if not hashable(group):
raise TypeError(
"`group` must be an xarray.DataArray or the "
"name of an xarray variable or dimension"
"name of an xarray variable or dimension."
f"Received {group!r} instead."
)
group = obj[group]
if len(group) == 0:
Expand Down
10 changes: 9 additions & 1 deletion xarray/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ def __init__(self, **kwargs):
% (k, set(OPTIONS))
)
if k in _VALIDATORS and not _VALIDATORS[k](v):
raise ValueError(f"option {k!r} given an invalid value: {v!r}")
if k == ARITHMETIC_JOIN:
expected = f"Expected one of {_JOIN_OPTIONS!r}"
elif k == DISPLAY_STYLE:
expected = f"Expected one of {_DISPLAY_OPTIONS!r}"
else:
expected = ""
raise ValueError(
f"option {k!r} given an invalid value: {v!r}. " + expected
)
self.old[k] = OPTIONS[k]
self._apply_update(kwargs)

Expand Down