Introduce `__numpy_dtype__` protocol

sebastian <[email protected]> Sat, 08 Nov 2025 14:24:54 +0100
Newsgroups gmane.comp.python.numeric.general
Message-ID <[email protected]>
Hi all,

A heads up, that I have a PR to introduce `__numpy_dtype__` as a 
protocol.
Right now when NumPy coerces to a dtype (e.g. via `np.dtype()` or 
`dtype=...`)
it checks for a `.dtype` attribute on the object and if that is a NumPy 
dtype
the conversion succeeds.

The problem with this two-fold:
1. Not all dtypes are NumPy dtypes.  Right now a non-NumPy dtype can 
interoperate
    with NumPy by having a `dtype.dtype` attribute that is a NumPy dtype. 
  But that
    seems awkward and may in some cases prevent the use of the dtype 
attribute for
    something more to the point.
2. While numpy prevents `np.dtype(numpy_array)`, some other array-like 
objects will
    be converted to a dtype, e.g.:
        np.array([1, 2, 3], dtype=pandas.Series([1, 2, 3]))
    works but should not.  Switching to `__numpy_dtype__` allows us to 
disable this trap.

Right now there is no semantic change, besides that downstream would be 
encouraged to
implement `__numpy_dtype__` if they currently have a `dtype` attribute 
that interoperates
with NumPy.
(In the case of a pandas Series that might be to raise an error or just 
set it to `None` so
that NumPy raises one.)

To me this seems be uncontroversial, since using `.dtype` has problems 
and this gives us a
way to move away from it and disambiguate these situations.

Cheers,

Sebastian
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: [email protected]