GeezerVison is showing github.com — Replace deprecated usage of numpy array shape, dtype and strides setting · Issue #18561 · astropy/astropy
Replace deprecated usage of numpy array shape, dtype and strides setting #18561DescriptionIn Numpy the direct setting of the dtype, shape and strides attributes is being deprecated as these operations are not safe (in particular in the free-threading build). See numpy/numpy#28800 for details.
Most direct setting of the attributes can be replaced with alternatives like array.view(dtype=...) or array.reshape(new_shape). These alternatives have a minor performance penalty (since a new object has to be created), but in general these methods create a view so no data is copied.
The astropy.utils.masked.Masked might be a bit more complex to convert, but can perhaps be handled in the same was as the numpy MaskedArray.
Update: most changes are mechanical changes for which no detailed knowledge of numpy or astropy is required. For a short description on how to approach this see scipy/scipy#23522 (comment) Activitypllim commented on Aug 28, 2025 Thanks for this and the PR(s)! pllim commented on Aug 28, 2025
The astropy.utils.masked.Masked might be a bit more complex to convert
Hopefully @mhvk could advise there. 🙏 eendebakpt commented on Aug 29, 2025 ContributorAuthor More actions For the dtype the number of changes is relatively small. A PR passing all tests is
main...eendebakpt:numpy_dtype_deprecations
There are two issues I found though:
- The PR used the private
_set_dtype. That will be available once the PR in numpy is merged, but not be available in earlier numpy versions. This would mean astropy would have to write code depending on the numpy version. Not very hard, but not very elegant either. In an ideal world we would have added the _set_dtype already in the last numpy version supported by astropy.
- The method
_init_tbdata from astropy/io/fits/hdu/table.py is used in only two places. From the code is looks like we can use a view to convert the data argument dtype (since at both places a view is applied to the data later), but this resulted in an error in the unit test test_table.py::TestVLATables::test_empty_vla_raw_data. It looks like the _init_tbdata is modyfying some global state (maybe via _get_raw_data which gets the .base of a recarray). If someone more familiar with the codebase could have a look, then perhaps there we can avoid the private dtype setter.
@mhvk @seberg @ngoldbaum mhvk commented on Aug 29, 2025 @eendebakpt - we're quite used to having numpy-version specific code paths; it would just require a from astropy.utils.compat import NUMPY_LT_2_4 and then use that.
That said, for .dtype setting in particular, I do think it should never be needed (at least outside of uncertainties, and likely not there either). I'd need to look closer at the FITS code to see what the problem is there... emieczko commented on Sep 30, 2025 Hi @mhvk @eendebakpt - I'm working on a school project involving contributing to an open-source project, and I was hoping to help out with this issue. I see that #18562 and #18599 address most of the work here, but I wanted to check with you guys to see if there was anything left that I could possibly work on; maybe looking for any leftover .dtype or .shape assignments elsewhere in the codebase?
Any guidance you could provide on where I could help would be much appreciated! mhvk commented on Sep 30, 2025 @emieczko - thanks! I think this particular issue is mostly done. It is also reasonable detailed, yet tedious, so perhaps not such a good one to work on. Generally, I'd recommend to pick something you're interested in yourself. Note that it doesn't have to be code: if while trying to use astropy for yourself, you find that the documentation is unclear or so, fixing that is most welcome too -- it is usually written by the people who wrote the code, who are not necessarily the best people to explain it. eendebakpt commented on Sep 30, 2025 ContributorAuthor More actions @emieczko I agree with @mhvk. However, you are interested in working on the dtype, shape and stride deprecation, there are other packages where this still needs to be done. In short:
- Install numpy with the deprecation branches on your local system (the strides is merged to numpy, the others not yet)
- Pick a package (pandas, scikit-learn, matplotlib). Run the test suit for that package, looking out for errors and deprecation warnings
- If needed, report the deprecations using an issue on the git repo and/or submit a PR to address the issue
If interested, you can reach out to me via email or on discuss.python.org jodamatta commented on Apr 29, 2026 Hi there! just like emieczko, i'm working on a school project to contribute to open source software (particularly refactoring issues). Is there anything left over from this issue i could help with? Thanks! @mhvk neutrinoceros commented on Apr 29, 2026 I don't think there is, deprecation warnings show up as hard errors in our CI and we're very diligent in fixing them as the come. It might be that I'm misremembering parts of the problem and that we're not actually done, but checking this takes time and we have other priorities right now... so, unless your program only counts PRs as "contributions", we could actually use a hand with answering your question. In my book, triage is a very valuable contribution, though I'd understand if your school disagrees. mhvk commented on Apr 29, 2026 I indeed think this particular issue is done, indeed I'm confident enough to just go ahead and close it.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|