CheckValues

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

Bases: 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

    can_be_zero

    bool

    [DEFAULT True] values can be zero

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

Attributes Summary

checks

Requested checks on the decorated function's input arguments and/or return.

Methods Summary

__call__(f)

Decorate a function.

Attributes Documentation

checks

Requested checks on the decorated function’s input arguments and/or return.

Methods Documentation

__call__(f)[source]

Decorate a function.

Parameters:

f – Function to be wrapped

Returns:

wrapped function of f

Return type:

function