CheckValues

class plasmapy.utils.decorators.CheckValues(checks_on_return: Dict[str, bool] = None, **checks)

Bases: plasmapy.utils.decorators.CheckBase

A decorator class to ‘check’ – limit/control – the values of input and return arguments to a function or method.

Parameters:
  • checks_on_return (Dict[str, bool]) – Specifications for value checks on the return of the function being wrapped. (see check values for valid specifications)
  • **checks (Dict[str, Dict[str, bool]]) –

    Specifications for value checks on the input arguments of the function being wrapped. Each keyword argument in checks is the name of a function argument to be checked and the keyword value contains the value check specifications.

    The value check specifications are defined within a dictionary containing the keys defined below. If the dictionary is empty or omitting keys, then the default value will be assumed for the missing keys.

    Key Type Description
    can_be_negative bool [DEFAULT True] values can be negative
    can_be_complex bool [DEFAULT False] values can be complex numbers
    can_be_inf bool [DEFAULT True] values can be inf
    can_be_nan bool [DEFAULT True] values can be nan
    none_shall_pass bool [DEFAULT False] values can be a python None

Notes

  • Checking of function arguments *args and **kwargs is not supported.

Examples

from plasmapy.utils.decorators.checks import CheckValues

@CheckValues(arg1={'can_be_negative': False, 'can_be_nan': False},
             arg2={'can_be_inf': False},
             checks_on_return={'none_shall_pass': True)
def foo(arg1, arg2):
    return None

# on a method
class Foo:
    @CheckValues(arg1={'can_be_negative': False, 'can_be_nan': False},
                 arg2={'can_be_inf': False},
                 checks_on_return={'none_shall_pass': True)
    def bar(self, arg1, arg2):
        return None

Methods Summary

__call__(f)
param f:Function to be wrapped

Methods Documentation

__call__(f)
Parameters:f – Function to be wrapped
Returns:wrapped function of f
Return type:function