check_quantity¶
-
plasmapy.utils.checks.
check_quantity
(validations)¶ 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.Parameters: validations (
dict
) – Validation dictionary.Raises: TypeError
– If the argument is not aQuantity
, units is not entirely units orargname
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 containsnan
or other invalid values as determined by the keywords.
Warns: ~astropy.units.UnitsWarning – If a
Quantity
is not provided and unique units are provided, aUnitsWarning
will be raised and the inputted units will be assumed.Returns: Decorated function.
Return type: function
Examples
>>> 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