Skip to content
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

Merged
merged 1 commit into from
Jun 11, 2021

Conversation

jerobado
Copy link
Member

Description

As discussed over at #11825, this will add an else clause that will catch the UnboundLocalError and raise a TypeError if unit type is incorrect.

Just let me know if I missed something.

Fixes #11825

@github-actions github-actions bot added the units label Jun 10, 2021
Copy link
Member

@pllim pllim left a 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!

astropy/units/tests/test_format.py Outdated Show resolved Hide resolved
@pllim pllim requested review from olebole and mhvk June 10, 2021 12:31
@pllim pllim added this to the v5.0 milestone Jun 10, 2021
@pllim
Copy link
Member

pllim commented Jun 10, 2021

p.s. Need to fix code style failure too.

@pllim pllim added the Bug label Jun 10, 2021
astropy/units/format/utils.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mhvk mhvk left a 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.)

@@ -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)}')
Copy link
Contributor

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 Show resolved Hide resolved
def test_fits_to_string_function_error():
"""Test if will raise a TypeError if incorrect unit type."""

with pytest.raises(TypeError, match='Incorrect type'):
Copy link
Contributor

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

@@ -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."""
Copy link
Contributor

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.
"""

@mhvk
Copy link
Contributor

mhvk commented Jun 10, 2021

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).

@jerobado
Copy link
Member Author

My local branch fix-issue-11825 is now updated with the last 2 commits I recently added:

$ git log --oneline
dd3d49ef0 (HEAD -> fix-issue-11825, jerobado/fix-issue-11825) Make raising of TypeError more specific
de9d9adce Keep two lines to the next function to conform with PEP-8
129bf9b0c Add TypeError test for Fits.to_string() function
5102b9479 Add else clause and raise TypeError if incorrect unit type
d33db763d (jerobado/main, main) Merge pull request #11830 from mhvk/unit-format-refactoring

Should I make the changes first before I do the rebase/squashed thing?

@mhvk
Copy link
Contributor

mhvk commented Jun 10, 2021

@jerobado - I'd rebase & squash, and then force-push so we can look at it again.

Copy link
Contributor

@mhvk mhvk left a 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
@mhvk
Copy link
Contributor

mhvk commented Jun 11, 2021

Great, merging! Thanks, @jerobado!

@mhvk mhvk merged commit 1e70875 into astropy:main Jun 11, 2021
@jerobado
Copy link
Member Author

Thank you @mhvk, @pllim, @olebole and also @embray!

I appreciate your guidance on this one :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

u.format.fits.Fits.to_string() throws the wrong exception with bad argument
4 participants