ENH: Convert flatiter, broadcast and nditer to heap types #32641
Ready for review.
Does this historical comment add to current understanding of the code.
Since we export these types, and CPython errors when creating static types with heap type base classes, I am not sure we can do this yet.
You are right, PyType_Ready rejects it and none of these three set Py_TPFLAGS_BASETYPE and neither does ufunc, so the static C route only ever worked because PyType_Ready skips that check.
Maybe BASETYPE is the line to draw here. If a type never claimed to be a base, converting it seems correct.
ndarray, dtype and the scalar types are the real problem and i think they need their own discussion.
Also does this require release note?
Edit: I have added release note
I don't think we can merge this, since it will break any subclassing of these types with static types.
Forgive my ignorance Matti but I was just curious, does numpy subclass those with static types somewhere or are you thinking users might?
Users might, since we export them in the API struct.
So would like to know which direction is acceptable.
1. Adding note in this release that static subclassing of these types is unsupported, then convert them a release later.
2. Heap types only under Py_LIMITED_API, static types otherwise. I know other folks are not fan of #ifdef Py_LIMITED_API as it means two definitions per type but it's confined to the type definition block and normal builds are untouched. This can go along with the release note from point 1 as well and then in later release we can drop non limited one entirely?
If neither works, then _multiarray_umath can't be built against the Limited API at all, since a static PyTypeObject doesn't compile once Py_LIMITED_API is defined.
I think we should discuss this at a community/triage meeting. In the meantime, can you scan popular github projects for code subclassing these types as static (or for that matter, even heap) types?
I searched for this and for the types in this PR no project sets tp_base to PyArrayIter_Type, PyArrayMultiIter_Type, NpyIter_Type or PyUFunc_Type. The ones mentioning them almost all are vendored NumPy headers, NumPy forks or CPython itself.
We discussed this at the community meeting and the general opinion was positive, the probability that anyone is subclassing these is quite small. I will give this one more review pass.
Other than @ngoldbaum's valid analysis on the performance cost of the change to heap types on #32552, I didn't find any problems here. One thing it might be nice to check is that the assignments of the type in PyArray_API succeed. A good test would be to add to _multiarray_tests.c.src a check that only known slots are NULL, since I don't think we have anything today that does a check. There are some slots that should be NULL, so the test would have to take that into consideration.
mattip
prathamhole14
PR summary
<!-- Please take some time to make it easier for us to review
your pull request (PR). Describe the PR using the questions below as guidance.
Please keep in mind that even seemingly small changes can have unexpected consequences
for workflows unrelated to the ones you may be familiar with. For this reason, maintainers
carefully consider the risks of all changes, including bug fixes.
Make sure to review the guidelines for contributing code to NumPy and fill out the AI disclosure section below.
Please note that non-maintainers may only have 2 non-draft PRs open for review at a
time.
-->
Related to: https://github.com/numpy/numpy/issues/31913 as part of adaptation of LIMITED API for
_multiarray_umathmoduleAlso related to: https://github.com/numpy/numpy/issues/32451 and https://github.com/numpy/numpy/issues/32747
AI Disclosure
<!-- If AI was used in the preparation of this PR, please disclose
the tool(s) used, how they were used, and specify what code or text is AI generated.
If no AI tools were used, please write "No AI tools used" in this section.
All interactions with reviewers must be performed by humans, including the submission of PRs.
Read the NumPy policy on AI generated code at https://numpy.org/devdocs/dev/ai_policy.html.
-->
Ported
nditermyself and after getting idea of it (and other changes are almost similar) and mechanical used claude opus 5 extra high to convert to heap type, and verified the code changes myself.