Skip to content

Commit

Permalink
comment on the purpose of the explicit isinstance and np.asarray
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis committed Sep 20, 2024
1 parent 9baaf50 commit f01ca2a
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,14 @@ def as_compatible_data(
else:
data = np.asarray(data)

# immediately return array-like types except `numpy.ndarray` subclasses and `numpy` scalars
if not isinstance(data, np.ndarray | np.generic) and (
hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
):
return cast("T_DuckArray", data)

# validate whether the data is valid data types.
# validate whether the data is valid data types. Also, explicitly cast `numpy`
# subclasses and `numpy` scalars to `numpy.ndarray`
data = np.asarray(data)

if data.dtype.kind in "OMm":
Expand Down

0 comments on commit f01ca2a

Please sign in to comment.