Subclasses cannot preserve their types during some binary ops and ufuncs combining DataArray and Dataset
维护者通常 1 天内回复
@AtharvRG 已经在做这个了。
开始于 2026年10月31日。
评估
调研方向
Read xr.Dataset._binary_op and SupportsArithmetic.__array_ufunc__, then locate the existing tests for binary operations and ufuncs involving DataArray and Dataset. The proposed hooks and their exact API require maintainer agreement, so confirm the intended subclass protocol before editing. Done means the three listed combinations return the specialized subclass type while existing non-subclass behavior remains covered by tests.
由索引模型根据 Issue 内容生成。
描述
What is your issue?
From uxarray downstream (UXARRAY/uxarray#1802). While xarray currently discourages subclassing in general, it does also point out uxarray in particular as an example where subclassing is used. In the context of uxarray, it is desirable and intuitive for binary operations and ufuncs involving a uxarray object and an xarray object to return a result which uses the more-specialized uxarray type.
The heart of the issue here is that xarray supports binary operations and binary ufuncs with one input being a Dataset and the other being a DataArray, but currently checks whether it should handle operations directly by checking isinstance(obj, xr.DataArray) or isinstance(obj, xr.Dataset) (which give True even for subclasses). When True, xarray fully handles the operations in a few cases, before subclasses ever get a chance to override anything. These cases are:
xr.Dataset * CustomDataArray, where * can be any binary operation (e.g. multiplication, addition, subtraction)ufunc(xr.Dataset, CustomDataArray), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)ufunc(xr.DataArray, CustomDataset), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)
All other combinations (such as xr.Dataset * CustomDataset, ufunc(xr.DataArray, CustomDataArray), and even xr.DataArray * CustomDataset) can be overridden by the subclass directly, returning the CustomDataset or CustomDataArray type one might hope for (UXARRAY/uxarray#1767 handled this for uxarray). However, these three remaining cases cannot fixed directly in the subclass, because:
- For (1), the operation goes directly to xr.Dataset's methods, and when xr.Dataset sees that the second term is an instance of xr.DataArray it decides to handle everything, without giving the second term a chance to alter the behavior in any way.
- For (2) and (3), when numpy applies the ufunc here it sees that the first term is not a subclass of the second term (or vice versa), so it starts checking for any
__array_ufunc__methods on the inputs, in order. This leads to applying the xarray object's__array_ufunc__, which similarly decides to handle everything after seeing that the second term is an instance of an xarray object (xr.DataArray in (2); xr.Dataset in (3)), also without giving the second term a chance to alter the behavior in any way.
One possible solution would be to introduce hooks or flags which do nothing in xarray, but which subclasses could override to regain control. For (1), I believe there would just need to be a hook inside xr.Dataset._binary_op, inside the if isinstance(other, DataArray | Dataset): block. It could probably be something as simple as if getattr(other, "_subclass_overrides_binary_op", False): return NotImplemented. For (2) and (3), I believe there would just need to be a hook inside SupportsArithmetic.__array_ufunc__, something like for x in inputs: if isinstance(x, SupportsArithmetic) and getattr(x, "_subclass_overrides_array_ufunc", False): return NotImplemented.
I would be very interested to open a PR to solve this issue, if xarray maintainers agree that something like this would sound reasonable. Also happy to take any suggestions on this topic, or clarify further if anything isn't clear!
Related: #3980
- 主要语言
- Python
- 星标
- 4.2k
- 派生
- 1.5k
- 平均合并
- 2 天 21 小时
- 30 天内合并 PR
- 74
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pydata/xarray 的其他 Issue
-
xarray removes axis grid from pcolormesh可能已有人在做 @Sealdot 今天认领。 未关闭bug needs triage
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
`DataTree.map_over_datasets` behavior can be obscure sometimes可能已有人在做 @mohansree14 于 24 天前认领。 未关闭needs triage
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identical可能已有人在做 @SohamXYZDev 于 29 天前认领。 未关闭bug needs triage
难度 2/5 1-3 小时 新手友好度 73/100
维护者通常 1 天内回复
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)可能已有人在做 @d-chambers 于 60 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
DataTree constructor error message says dict even though dict is not accepted可能已有人在做 @aryansk 于 100 天前认领。 未关闭bug contrib-good-first-issue topic-DataTree topic-error reporting
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
abetlen/llama-cpp-python#2424 ·
维护者通常 1 天内回复
-
Bug IronPython Python 3
难度 2/5 1-3 小时 新手友好度 72/100
pyrevitlabs/pyRevit#4184 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复