astropy/astropy — pull request #2934 by mhvk (2014-09-12 20:25:17+00:00)
Title: Ensure that quantity operations with incompatible types give a useful TypeError

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)
in ()
----> 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)
in ()
----> 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'
```