Use Exception.add_note() for enriched error context in nddata and coordinates#18877
Use Exception.add_note() for enriched error context in nddata and coordinates#18877FazeelUsmani wants to merge 23 commits intoastropy:mainfrom
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
neutrinoceros
left a comment
There was a problem hiding this comment.
pytest 8.0 (which we require) supports matching exception notes with the match keyword argument in pytest.raises, please use that instead of capturing the exception manually.
|
@FazeelUsmani could you avoid merging from main 4 times a day ? I get notified every single time and it's a waste of CI resources |
|
Okay, sure. |
|
Please review this PR |
nstarman
left a comment
There was a problem hiding this comment.
Thanks @FazeelUsmani ! This looks like a nice addition.
Co-authored-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
nstarman
left a comment
There was a problem hiding this comment.
Thanks @FazeelUsmani !
Ping @eerovaher @mhvk @mwcraig for final reviews.
mwcraig
left a comment
There was a problem hiding this comment.
Thanks for this @FazeelUsmani!
| def test_gravitational_redshift_exception_note(): | ||
| """Test that exception notes are added for UnitsError in gravitational_redshift.""" | ||
| someloc = EarthLocation(lon=-87.7 * u.deg, lat=37 * u.deg) | ||
| sometime = Time("2017-8-21 18:26:40") | ||
|
|
||
| # Test with wrong units to trigger UnitsError | ||
| masses = { | ||
| "sun": constants.G * constants.M_sun, | ||
| "jupiter": 1 * u.km, # wrong units - should be mass or G*mass | ||
| } | ||
|
|
||
| with pytest.raises(u.UnitsError, match="masses.*gravitational parameters"): | ||
| someloc.gravitational_redshift(sometime, masses=masses) | ||
|
|
||
|
|
There was a problem hiding this comment.
| def test_gravitational_redshift_exception_note(): | |
| """Test that exception notes are added for UnitsError in gravitational_redshift.""" | |
| someloc = EarthLocation(lon=-87.7 * u.deg, lat=37 * u.deg) | |
| sometime = Time("2017-8-21 18:26:40") | |
| # Test with wrong units to trigger UnitsError | |
| masses = { | |
| "sun": constants.G * constants.M_sun, | |
| "jupiter": 1 * u.km, # wrong units - should be mass or G*mass | |
| } | |
| with pytest.raises(u.UnitsError, match="masses.*gravitational parameters"): | |
| someloc.gravitational_redshift(sometime, masses=masses) |
There is no need for a new test. The UnitsError is already being tested, so the existing test should be updated instead.
There was a problem hiding this comment.
This does not need a change log entry.
Co-authored-by: Eero Vaher <eero.vaher@gmail.com>
astropy/nddata/tests/test_utils.py
Outdated
| def test_extract_array_fill_value_exception_note(): | ||
| """Test that exception notes are added when fill_value type is incompatible.""" | ||
| data = np.arange(12, dtype=int).reshape(3, 4) | ||
|
|
||
| # Try to extract with partial mode and incompatible fill_value (NaN for integer array) | ||
| with pytest.raises(ValueError, match="fill_value.*data type"): | ||
| extract_array(data, (5, 5), (1, 1), mode="partial", fill_value=np.nan) |
There was a problem hiding this comment.
It shouldn't be my job to review nddata, but why is this new test needed if we already have
astropy/astropy/nddata/tests/test_utils.py
Lines 430 to 436 in 818c1ac
There was a problem hiding this comment.
If anything I would prefer that the new test replace the old test given that the new test explains what it is checking. I am also fine with both staying in there since they are not quite identical....hence the approval.
If @FazeelUsmani would like to replace the old test you cited with this new test I'm happy to approve that also.
There was a problem hiding this comment.
I don't see what's the point of having two separate tests for checking a single static error message.
There was a problem hiding this comment.
Yes, I used Claude code
There was a problem hiding this comment.
Yes, I used Claude code
That in and of itself is not an issue -- I also use Claud Code. One needs to happen here is cleaning up the tests. As @eerovaher points out, the tests generated by Claude are already present elsewhere in the code (though I actually like Claude's version of the test in NDData more than the current test).
There are a couple paths forward here:
- You can manually delete the two new tests from the pull request
- You can ask Claude to delete them
I often find that I need to "coach" Claude to end up with a useful result on par with what a skilled programmer would produce.
There was a problem hiding this comment.
Obligatory link to https://xkcd.com/1319/ (and https://arxiv.org/abs/2507.09089 - "Measuring the Impact of Early-2025 AI on Experienced Open-Source Developer Productivity"; certainly a case to be made if one includes reviewer productivity)
| @@ -0,0 +1 @@ | |||
| Enhanced error messages in ``extract_array`` using Python 3.11's ``Exception.add_note()`` to provide additional context when ``fill_value`` type is incompatible with the array dtype. | |||
There was a problem hiding this comment.
This PR changes some details regarding how exactly the error messages are constructed, but the messages in this PR don't contain any information or provide any additional context compared to the messages in main.
neutrinoceros
left a comment
There was a problem hiding this comment.
I agree with Eero that the changelog fragment is superfluous or misleading. Other than that, I don't think this PR should close the issue it's linked to.
Taking a step back, I also feel like much more work went into reviewing AI output here than would it would normally take to produce the patch manually. This is extractive work at best: extracting value both from human coders from the past whose work was fed into the model, and reviewer time. In my opinion it is not worth its cost, and I would recommend taking collection action against this.
This PR adopts Python 3.11's
Exception.add_note()method to enrich exception messages with contextual information, replacing the olderexc.args +=pattern.Changes
extract_array()to useadd_note()when fill_value type is incompatiblegravitational_redshift()to useadd_note()for mass/gravitational parameter unit errorsTesting
Added two new test functions:
test_extract_array_fill_value_exception_note(): Verifies notes in nddata.utilstest_gravitational_redshift_exception_note(): Verifies notes in coordinates.earthReferences
Fixes #16879
Related
This is part of the larger effort to adopt exception notes throughout the Astropy codebase. These changes demonstrate the pattern for other contributors to follow.
Changelog Entry