From f01ca2ac0e575a7fe088aced456dc1a715190eca Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Fri, 20 Sep 2024 21:29:55 +0200 Subject: [PATCH] comment on the purpose of the explicit isinstance and `np.asarray` --- xarray/core/variable.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 5b0038f027d..70dea472281 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -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":