Below you can find the documentation of the public API of argvalidate.
Even though more constants, decorators, exceptions and functions may be specified in the argvalidate module, only those described here form the API and are intended for public use.
Note
Do not rely on any internals of argvalidate, as these may change at any time, without further notice.
The API described here forms the public interface to argvalidate and as such will have functionality pending for removal deprecated first.
Decorator used for checking arguments passed to a function or method.
Parameters: |
|
---|---|
Raises: |
|
Example:
class MyClass:
@accepts(int, str)
def my_method(self, x_is_int, y_is_str):
[...]
@accepts(int, str)
def my_function(x_is_int, y_is_str):
[....]
Decorator used for checking the return value of a function or method.
Parameters: | expected_type – expected type or return value |
---|---|
Raises ReturnValueTypeException: | |
Raised if the return value’s type does not match the definition in the decorator’s expected_type parameter. |
Example:
@return_value(int)
def my_func():
return 5
Note
Stacking of decorator of the same type (ie. accepts() and accepts(), returns() and returns()) is not possible and will cause a DecoratorStackingException to be raised.
Stacking of different types of decorators (ie. returns() and accepts()) is possible though and will neither raise an exception nor break anything.
Wrapper for backwards-compatibility.
Deprecated : | This decorator has been replaced with accepts(). |
---|
Simple helper function to create a tuple from every argument passed to it.
Parameters: | args – type definitions |
---|
A tuple can be used instead of calling this function, however, the tuple returned by this function contains a customized __repr__ method, which makes Exceptions easier to read.
Example:
@func_check_args(one_of(int, str, float))
def my_func(x):
pass
Returns True if argvalidate raises exceptions, False if argvalidate creates warnings instead.
This behaviour can be controlled via the environment variable ARGVALIDATE_WARN.
Returns True if argvalidate generates warnings for keyword arguments passed as arguments.
This behaviour can be controlled via the environment variable ARGVALIDATE_WARN_KWARG_AS_ARG.
Base argvalidate exception.
Used as base for all exceptions.
Exception for invalid argument type.
This exception provides the following attributes:
Name of function that caused the exception to be raised (str, read-only).
Name of the keyword argument passed to the function, but not specified in the decorator (str, read-only).
Argument type that was expected (type, read-only).
Argument type that was passed to the function (type, read-only).
Exception for invalid return value type.
This exception provides the following attributes:
Name of function that caused the exception to be raised (string, read-only).
Argument type that was expected (type, read-only).
Type of value returned by the function (type, read-only).
Exception for invalid decorator non-keyword argument count.
This exception provides the following attributes:
Name of function that caused the exception to be raised (str, read-only).
Number of arguments that were expected (int, read-only).
Number of arguments that were passed to the function (int, read-only).
Exception for unspecified decorator keyword argument.
This exception provides the following attributes:
Name of function that caused the exception to be raised (str, read-only).
Name of the keyword argument passed to the function, but not specified in the decorator (str, read-only).
Exception for stacking a decorator with itself.
This exception provides the following attributes:
Name of function that caused the exception to be raised (str, read-only).
Name of the decorator that was stacked with itself (str, read-only).