astropy/astropy — pull request #3051 by mhvk (2014-10-23 20:26:59+00:00)
Title: Let Quantity treat input with unit attributes more carefully.

Currently, one can inadvertently mess up input to `Quantity` with a `.unit` attribute, as shown below. This PR cleans up that path, and also ensures that if the `unit` attribute contains something unrecognizable as a `unit`, an exception is raised (rather than assume the content has no unit). Finally, it doesn't presume that conversion between units is done by simple rescaling, but rather uses the `.to` method. This to ensure that if one is using equivalencies, e.g., between velocity and wavelength, or degrees C and K, conversion will be done correctly.

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

In [2]: class MyQuantityLookalike(np.ndarray):
...: pass
...:

In [3]: a = np.arange(3.)

In [4]: mylookalike = a.view(MyQuantityLookalike)

In [5]: mylookalike.unit = u.m

In [6]: u.Quantity(mylookalike, u.mm, copy=False)
Out[6]:

In [7]: mylookalike
Out[7]: MyQuantityLookalike([ 0., 1000., 2000.])

In [8]: mylookalike.unit
Out[8]: Unit("m")
```