Raise an exception if an annotated argument in a decorated function is an Quantity with incorrect units and valid numerical values, or assume inputs are SI Quantities.


validations (dict) – Validation dictionary.

  • TypeError – If the argument is not a Quantity, units is not entirely units or argname does not have a type annotation.
  • UnitConversionError – If the argument is not in acceptable units.
  • UnitsError – If after the assumption checks, the argument is still not in acceptable units.
  • ValueError – If the argument contains nan or other invalid values as determined by the keywords.

~astropy.units.UnitsWarning – If a Quantity is not provided and unique units are provided, a UnitsWarning will be raised and the inputted units will be assumed.


Decorated function.

Return type:



>>> from astropy import units as u
>>> @check_quantity({
... "x": {"units": u.m},
... "y": {"units": u.s,
...       "can_be_negative": False,
...       "can_be_complex": True,
...       "can_be_inf": False}
... })
... def func(x: u.m, y: u.s=1 * u.s):
...     return x
>>> func(1 * u.m)
<Quantity 1. m>
>>> func(1 * u.m, 2 * u.m)
Traceback (most recent call last):
astropy.units.core.UnitConversionError: The argument y to func should be a Quantity with the following units: s