GeezerVison is showing github.com — BUG: `recarray` doesn't have a proper `_set_dtype` method ? · Issue #31295 · numpy/numpy
BUG: recarray doesn't have a proper _set_dtype method ? #31295DescriptionDescribe the issue:
I'm trying to get rid of newly found deprecation warnings about dtype attribute setters in astropy.
There's one occurence in particular where I don't think we can use a view, so the next best thing would be _set_dtype, but it looks like it's not properly set on record arrays (it exists but it's a None)
self = <astropy.io.fits.hdu.table.BinTableHDU object at 0x10bf5f380>
data = rec.array([([ -939524096, 0],), ([ -637534208, -939524096],),
([ -637534208, -1576992768],), ([ ... ([-1090519040, -1157431040],), ([ -704643072, 2047082752],)],
dtype=[('COMPRESSED_DATA', '<i4', (2,))])
def _init_tbdata(self, data):
columns = self.columns
print(type(data))
> data._set_dtype(data.dtype.newbyteorder(">"))
E TypeError: 'NoneType' object is not callable
astropy/io/fits/hdu/table.py:212: TypeError
Reproduce the code example:
import numpy as np
a = np.array([1], dtype="float64").view(np.rec.recarray)
a._set_dtype("float64")
Error message:
Traceback (most recent call last):
File "/Users/clm/dev/gh/neutrinoceros/astropy-compatibility-checks/numpy-dev/_astropy/t.py", line 3, in <module>
a._set_dtype("float64")
~~~~~~~~~~~~^^^^^^^^^^^
TypeError: 'NoneType' object is not callable
Python and NumPy Versions:
2.5.0.dev0+git20260421.388bd75
3.14.3 (main, Feb 4 2026, 01:51:49) [Clang 21.1.4 ]
Runtime Environment:
No response
How does this issue affect you or how did you find it:
No response Activityikrommyd commented on Apr 21, 2026 Just a curiosity of mine regarding your first snippet.
Why doesn't
data = data.view(data.dtype.newbyteorder(">"))
work? seberg commented on Apr 22, 2026 Yeah, this was somewhat intentional, but we could change it. It would be nice to not rely on _set_dtype all this much beyond finalization. (On first look, this does look like a case that shouldn't is very unsafe using _set_dtype, e.g. in threaded executation -- and not just free-threaded.)
Is this something users would see or a very weird niche test? mhvk commented on Apr 22, 2026 Oops, should have posted here: the addition of _set_dtype = None to np.recarray breaks a FITS_rec subclass in astropy. This means older astropy cannot be used with numpy 2.5. Personally, I don't think this is a disaster -- the fix is easy and can be backported (and people should not expect to be able to run newest numpy with older astropy), but I've raised it to ask for others' opinion. See astropy/astropy#19584.
p.s. That said, I guess there is little wrong from the numpy side to postpone using _set_dtype = None for another version.
Extra: I do think the use in FITS_rec can be replaced by views, though my initial attempt left one failing test, so I decided just to use ndarray._set_dtype for now. I'd still like to transition to views later... seberg commented on Apr 22, 2026
Extra: I do think the use in FITS_rec can be replaced by views, though my initial attempt left one failing test, so I decided just to use ndarray._set_dtype for now. I'd still like to transition to views later...
Ah missed that commenting on the astropy PR. That is unfortunate since it seems that the FITS code is precisely the kind of code that should use view (although I am not quite sure if everywhere -- some paths seem to restore the original dtype!). seberg commented on Apr 23, 2026 OK... I am gaming out actually checking walking the MRO, which only leaves the __setattr__ path as a potentially regressing one (and too liberal downstream _set_dtype use, but I think that's on them).
I think it should be around ~100 lines of code, but not too bad (plus tests), given that it should propel us into an even safer state, I think that is fine...
It does mean we have to so a silly amount of attribute lookups for multi-level subclasses... But if we have multi-level subclasses, I think that should be pretty unproblematic...
EDIT: And too be clear, after we are done with the .dtype = ... deprecation and everyone must use _set_dtype() we can delete that walking.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|