8000 ENH: add a hook to the DType API for parsing dtype strings · Issue #30699 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: add a hook to the DType API for parsing dtype strings #30699

@ngoldbaum

Description

@ngoldbaum

Over in nanograv/PINT#1944 (comment) and astropy/astropy#19197 we ran into a behavior difference between built-in DTypes and user dtypes: a string representation of a user dtype doesn't round-trip through np.dtype.

Works:

>>> np.dtype(np.dtype(np.int64).str)
dtype('int64')

Doesn't work:

>>> np.dtype(np.dtype(numpy_quaddtype.QuadPrecDType().str))
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    np.dtype(np.dtype(numpy_quaddtype.QuadPrecDType().str))
             ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: data type "QuadPrecDType(backend='sleef')" not understood

astropy was relying on this round-tripping via dtype.str.

We spoke a little about this during today's triage meeting and we came up with a possible way to make this work.

  • Add two new DType API slots.
    • The first will be a string or maybe a null-terminated array of strings that a DType passes to NumPy and NumPy can use to decide whether or not an unrecognized string dtype name corresponds to a registered user DType. These strings could be something like "QuadPrecDType", and if NumPy receives QuadPrecDType(backend='longdouble'), NumPy sees the beginning of the string is a match and calls the function defined in the second slot with the user-supplied string.
    • The second will be a function that accepts a string, parses it, and returns a new DType instance.

Ping @SwayamInSync @mhvk what do you think of this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0