Skip to content

ENH: Simplify stable ABI use for user DType creation #31301

Description

@seberg

gh-31364 will users to use heaptypes as DTypes, but it would still be nice to create a newer API, see below:

The user DTypes were created at a time when the stable ABI was not ready for them. But with Python 3.12 the basis should exist now: there should be a path forward around this (i.e. even if it might require hacks similar to what we have, I suspect there is more prior art around that, e.g. maybe in nanobind, at the time NumPy was relatively early to the game, it's not anymore).

This goes hand-in-hand with any effort to make NumPy use heap-types mostly/everywhere, but I think we'll want a new API call that merges:

  • PyType_FromMetaclass (or some variation of that)
  • PyArrayInitDTypeMeta_FromSpec

to do both in one go. Now there are a couple of things to figure out here, for one, DTypeMeta currently has a tp_new to prevent creation from Python (this can just be removed in principle, although direct __new__ calls are then unsafe from Python) -- this collides with PyType_FromMetaclass().
Another thing is, that PyArrayInitDTypeMeta_FromSpec was based on the stable API at the time without thinking much about improving the pattern. But Python has had some improvements, I believe (i.e. now using only slots and a new PEP https://discuss.python.org/t/pep-820-pyslot-unified-slot-system-for-the-c-api/105552 to revamp the slot system. If NumPy has API that wraps type creation from Python, then we need to check that out.).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions