-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add else clause and raise TypeError if incorrect unit type #11832
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably can be filed under bug in the change log. Thanks!
p.s. Need to fix code style failure too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK, but some comments about details, mostly just to try to make things as clear as possible. While you change that, would you be able to rebase and squash the commits into one? (I can help if you don't know how to do that.)
astropy/units/format/utils.py
Outdated
@@ -109,6 +109,8 @@ def decompose_to_known_units(unit, func): | |||
return decompose_to_known_units(unit._represents, func) | |||
raise | |||
return unit | |||
else: | |||
raise TypeError(f'Incorrect type {type(unit)}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good, but I think the error message itself could be more helpful: since "incorrect type" is just repeating the name of the error. How about "unit argument must be a 'NamedUnit' or 'CompositeUnit', not {type(unit)}."
This is a bit more like for float
:
In [4]: float(None)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-b14d5e084cce> in <module>
----> 1 float(None)
TypeError: float() argument must be a string or a number, not 'NoneType'
astropy/units/tests/test_format.py
Outdated
def test_fits_to_string_function_error(): | ||
"""Test if will raise a TypeError if incorrect unit type.""" | ||
|
||
with pytest.raises(TypeError, match='Incorrect type'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to adjust the match
if we go with my suggestion above
astropy/units/tests/test_format.py
Outdated
@@ -379,6 +379,12 @@ def test_flexible_float(): | |||
assert u.min._represents.to_string('latex') == r'$\mathrm{60\,s}$' | |||
|
|||
|
|||
def test_fits_to_string_function_error(): | |||
"""Test if will raise a TypeError if incorrect unit type.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This maybe is a good place to note why we are testing this, perhaps
"""Test function raises TypeError on bad input.
This instead of returning None
, see gh-11825.
"""
I added "no-changelog-entry-needed" since I think it was far from easy to reach the missing code path, and in the end it led to an error (even if an unexpected one). |
My local branch
Should I make the changes first before I do the rebase/squashed thing? |
@jerobado - I'd rebase & squash, and then force-push so we can look at it again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks all good now! Could you still squash the commits?
Add else clause and raise TypeError if incorrect unit type This will add an 'else' clause that will catch the UnboundLocalError and raise a TypeError if unit type is incorrect. Add TypeError test for Fits.to_string() function Keep two lines to the next function to conform with PEP-8 Co-authored-by: Ole Streicher <github@liska.ath.cx> Make raising of TypeError more specific Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com> Apply corrections on previous commits
0b2ac92
to
64ca723
Compare
Great, merging! Thanks, @jerobado! |
Description
As discussed over at #11825, this will add an
else
clause that will catch theUnboundLocalError
and raise aTypeError
if unit type is incorrect.Just let me know if I missed something.
Fixes #11825