Introduce `__numpy_dtype__` protocol
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
On Sat, Nov 8, 2025 at 2:25 PM sebastian <sebastian@sipsolutions.net> wrote:
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.
Thanks for working on this Sebastian. After reading the discussion in https://github.com/numpy/numpy/issues/25306, I agree that this is a useful thing to add and I don't see any problems. Cheers, Ralf
Oh thanks for the heads up! I actually pretty recently opened a PR for the quaddtype to expose the `.dtype` attribute from scalar (PR #234). Will updated it as per the new protocol `__numpy_dtype__` and maybe keep the `.dtype` for backward compatibility. Best, Swayam Singh
participants (3)
-
Ralf Gommers -
sebastian -
Swayam Singh