title: astropy: How can I check that an argument has the correct unit
body: <p>In Python's <code>astropy</code>, how can I check that a function's argument not only has the correct unit, but has a unit at all?</p>
  
  <p>I'm familiar with <code>is_equivalent()</code>, so to check that <code>M</code> has units of mass, I can say</p>
  
  <pre><code>assert M.unit.is_equivalent(u.g)
  </code></pre>
  
  <p>which returns <code>True</code> if, say, <code>M = 1e12 * u.Msun</code>. But if <code>M</code> doesn't have any units at all — e.g. if I accidentally pass <code>M = 1e12</code> — I get</p>
  
  <pre><code>AttributeError: 'Quantity' object has no '_normalize_equivalencies' member
  </code></pre>
  
  <p>One way to do it would be to first multiply <code>M</code> by e.g. <code>(u.m/u.m)</code>, which equals <code>Unit(dimensionless)</code> and won't alter <code>M</code> if it <em>does</em> have a unit, while it would convert a number like <code>1e12</code> into <code>&lt;Quantity 1.e+12&gt;</code> which can then be checked without giving an error.</p>
  
  <p>This doesn't seem like the right way to do it, though. Is there a better way?</p>