Clarify exception message for non-quantity input to ufuncs#16878
Clarify exception message for non-quantity input to ufuncs#16878mhvk merged 1 commit 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.
|
nstarman
left a comment
There was a problem hiding this comment.
Nice! The parenthetical improves the error message a lot.
I would suggest adding a TODO item to convert this to an Exception Note (https://docs.python.org/3/tutorial/errors.html#enriching-exceptions-with-notes) for py3.11+. This is a nearly tailor-made example.
|
|
||
| def test_s2p_not_all_quantity(self): | ||
| # Test for a useful error message - see gh-16873. | ||
| # Non-quantity input should be treated as dimensioness and thus cannot |
There was a problem hiding this comment.
| # Non-quantity input should be treated as dimensioness and thus cannot | |
| # Non-quantity input should be treated as dimensionless and thus cannot |
| def test_s2p_not_all_quantity(self): | ||
| # Test for a useful error message - see gh-16873. | ||
| # Non-quantity input should be treated as dimensioness and thus cannot | ||
| # be converted to radian. |
There was a problem hiding this comment.
| # be converted to radian. | |
| # be converted to radians. |
| """Test unit errors when dimensionless parameters are used""" | ||
|
|
||
| msg = "'NoneType' object has no attribute 'get_converter'" | ||
| msg = "'NoneType'.*no attribute 'get_converter'.*treated as dimensionless" |
There was a problem hiding this comment.
Nifty! I forgot about the regex-capable input.
|
|
||
| def test_sin_with_quantity_out(self): | ||
| # Test for a useful error message - see gh-16873. | ||
| # Non-quantity input should be treated as dimensioness and thus cannot |
There was a problem hiding this comment.
| # Non-quantity input should be treated as dimensioness and thus cannot | |
| # Non-quantity input should be treated as dimensionless and thus cannot |
| def test_sin_with_quantity_out(self): | ||
| # Test for a useful error message - see gh-16873. | ||
| # Non-quantity input should be treated as dimensioness and thus cannot | ||
| # be converted to radian. |
There was a problem hiding this comment.
| # be converted to radian. | |
| # be converted to radians. |
f5efc6d to
9918f83
Compare
Very nice! I went ahead and just used it, with a version check that makes the new exception message match it also for python < 3.11. Also did the other small stuff (how did I not see those??) |
| try: | ||
| converter = from_unit.get_converter(to_unit) | ||
| except AttributeError as e: | ||
| # Check for lack of unit only now, to avoid delay for cases where a unit |
There was a problem hiding this comment.
Double unit here (last word in this line, first in the next)
There was a problem hiding this comment.
Oops, another typo, thanks for catching it... Fix in #16881
…itless-input-fixup Fix comment added in gh-16878 [skip ci]
| f"as dimensionless and cannot be converted to {to_unit}." | ||
| ) | ||
| if sys.version_info >= (3, 11): | ||
| e.add_note(note) |
There was a problem hiding this comment.
Belated comment: I don't know how to feel about having error behavior be different based on Python version. But probably okay for this one case. 🤷♀️
There was a problem hiding this comment.
I did check that the actual error message is exactly identical -- I thought it was easier to do it like this rather than wait for our minimum version of python to be 3.11.
But, actually, aren't we dropping support of python 3.10 by October (following SPEC 0)? If so, isn't this OK regardless?
There was a problem hiding this comment.
But why is "exotic archs" on Python 3.12 failing? See #16878 (review)
| """Test unit errors when dimensionless parameters are used""" | ||
|
|
||
| msg = "'NoneType' object has no attribute 'get_converter'" | ||
| msg = "'NoneType'.*no attribute 'get_converter'.*\n.*treated as dimensionless" |
There was a problem hiding this comment.
I think this broke "exotic archs" regex. See https://github.com/astropy/astropy/actions/runs/10554512360/job/29236483624
There was a problem hiding this comment.
def test_unit_errors(self):
"""Test unit errors when dimensionless parameters are used"""
msg = "'NoneType'.*no attribute 'get_converter'.*\n.*treated as dimensionless"
> with pytest.raises(AttributeError, match=msg):
E AssertionError: Regex pattern did not match.
E Regex: "'NoneType'.*no attribute 'get_converter'.*\n.*treated as dimensionless"
E Input: "'NoneType' object has no attribute 'get_converter'"
astropy/units/tests/test_quantity_erfa_ufuncs.py:546: AssertionError
There was a problem hiding this comment.
Anyways, I opened #16884 as follow-up issue that would block v7.0 release. If no one has idea how to fix, my proposal would be to revert the exception behavior changes (but keep your clearer message). Thanks!
This pull request is to improve the puzzling error message one can get if passing in both numbers and quantities to ufuncs, by adding an explanation (on purpose, the exception type and start of the message are not changed, to ensure this is not an API change -- there were tests in the unit package relying on it). cc @maxnoe
Before:
Now
Fixes #16873