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

Ensure that quantity operations with incompatible types give a useful TypeError #2934

Merged
merged 1 commit into from
Sep 24, 2014

Conversation

mhvk
Copy link
Contributor

@mhvk mhvk commented Sep 12, 2014

Currently, a rather uninformative TypeError is raised if one, e.g., adds a quantity to something that is not a number:

In [1]: import astropy.units as u, numpy as np

In [2]: np.add(np.arange(10.)*u.cm,  u.cm)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-63526e011ceb> in <module>()                                    
----> 1 np.add(np.arange(10.)*u.cm,  u.cm)                                      

/usr/lib/python3/dist-packages/astropy/units/quantity.py in __array_prepare__(self, obj, context)                                                               
    294             # (this allows, e.g., `q > 0.` independent of unit)         
    295             maybe_arbitrary_arg = args[scales.index(0.)]                
--> 296             if _can_have_arbitrary_unit(maybe_arbitrary_arg):           
    297                 scales = [1., 1.]                                       
    298             else:                                                       

/usr/lib/python3/dist-packages/astropy/units/quantity.py in _can_have_arbitrary_unit(value)
     57     `True` if each member is either zero or not finite, `False` otherwise
     58     """
---> 59     return np.all(np.logical_or(np.equal(value, 0.), ~np.isfinite(value)))
     60 
     61 

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

With this PR, this becomes:

In [2]: np.add(np.arange(10.)*u.cm,  u.cm)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-63526e011ceb> in <module>()
----> 1 np.add(np.arange(10.)*u.cm,  u.cm)

/data/mhvk/venv/astropy-dev/lib/python3.4/site-packages/astropy-1.0.dev9886-py3.4-linux-x86_64.egg/astropy/units/quantity.py in __array_prepare__(self, obj, context)
    310                                 .format(function.__name__,
    311                                         args[0].__class__.__name__,
--> 312                                         args[1].__class__.__name__))
    313 
    314         # In the case of np.power, the unit itself needs to be modified by an

TypeError: Unsupported operand type(s) for add: 'Quantity' and 'Unit'

@astrofrog
Copy link
Member

👍 - maybe you could also check the content of the exception in the test, since that is the main change here?

@astrofrog astrofrog added this to the v0.4.2 milestone Sep 12, 2014
@mhvk mhvk force-pushed the quantity-non-number-operations branch 5 times, most recently from d842a3c to 9212514 Compare September 13, 2014 21:02
@mhvk
Copy link
Contributor Author

mhvk commented Sep 13, 2014

@astrofrog - that check on the error message turned out to be more of a hassle than thought, but anyway, it is done. I think this is ready now.

@mhvk mhvk force-pushed the quantity-non-number-operations branch from 9212514 to bd3ca8b Compare September 23, 2014 13:54
@mhvk
Copy link
Contributor Author

mhvk commented Sep 23, 2014

@astrofrog - do you think this is ready to go in? @embray - this just clarifies an exception, so I don't think this necessarily has to be in 0.4.2; would be happy to change it to 0.4.3 if that helps you.

@embray embray modified the milestones: v0.4.2, v0.4.3 Sep 23, 2014
@mhvk
Copy link
Contributor Author

mhvk commented Sep 24, 2014

Rebased and moved the changelog entry to 0.4.3. @astrofrog - I think this is ready to go in (assuming travis still passes), so marked it as such. I also applied the Affects-release tag; @embray - hopefully, this is the right usage?

@embray
Copy link
Member

embray commented Sep 24, 2014

Thanks @mhvk , looks good!

embray added a commit that referenced this pull request Sep 24, 2014
Ensure that quantity operations with incompatible types give a useful TypeError
@embray embray merged commit e83d361 into astropy:master Sep 24, 2014
@mhvk mhvk deleted the quantity-non-number-operations branch September 26, 2014 16:44
embray added a commit that referenced this pull request Dec 31, 2014
Ensure that quantity operations with incompatible types give a useful TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants