_check_quantity

plasmapy.utils.checks._check_quantity(arg, argname, funcname, units, can_be_negative=True, can_be_complex=False, can_be_inf=True, can_be_nan=True, none_shall_pass=False)

Raise an exception if an object is not a Quantity with correct units and valid numerical values.

Parameters:
  • arg (Quantity) – The object to be tested.
  • argname (str) – The name of the argument to be printed in error messages.
  • funcname (str) – The name of the original function to be printed in error messages.
  • units (Unit or list of Unit) – Acceptable units for arg.
  • can_be_negative (bool, optional) – True if the Quantity can be negative, False otherwise. Defaults to True.
  • can_be_complex (bool, optional) – True if the Quantity can be a complex number, False otherwise. Defaults to False.
  • can_be_inf (bool, optional) – True if the Quantity can contain infinite values, False otherwise. Defaults to True.
  • can_be_nan (bool, optional) – True if the Quantity can contain NaN values, False otherwise. Defaults to True.
  • none_shall_pass (bool, optional) – True if the Quantity can contain None values, False otherwise. Defaults to True.
Raises:
  • TypeError – If the argument is not a Quantity or units is not entirely units.
  • 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 any nan or other invalid values as determined by the keywords.
Warns:

~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.

Examples

>>> from astropy import units as u
>>> import pytest
>>> _check_quantity(4*u.T, 'B', 'f', u.T)
<Quantity 4. T>
>>> with pytest.warns(u.UnitsWarning, match="No units are specified"):
...     assert _check_quantity(4, 'B', 'f', u.T) == 4 * u.T