Validation
coola.validation ¶
Contain functions to validate function/method arguments.
Every validator follows the same rules:
- It returns
Nonewhen the value is valid, and never converts the value. - A value of the wrong type raises
TypeError, and a value of the right type that breaks the constraint raisesValueError. - The message names the argument (
name, a keyword-only option), the constraint and the value received. - The type checks are strict (
boolis rejected byvalidate_intandvalidate_number), the comparisons accept whatever supports them. - A
NaNis never valid. - The package imports the standard library only.
A validator exists only if it states a constraint that isinstance or one
comparison cannot state without a special case: validate_int exists for
the bool case, validate_number for the numbers.Real plus bool
case, and validate_finite for the overflow of math.isfinite and the
NaN. validate_str(x) is validate_isinstance(x, str) and
validate_positive(x) is validate_gt(x, 0). Validate an argument with
this package instead of writing raise ValueError for a type, range or
membership check.
coola.validation.validate_finite ¶
validate_finite(
value: float, *, name: str = "value"
) -> None
Validate that value is finite (neither infinite nor NaN).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_finite
>>> validate_finite(1.0)
coola.validation.validate_ge ¶
validate_ge(
value: Any, low: Any, *, name: str = "value"
) -> None
Validate that value is greater than or equal to low.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
low
|
Any
|
The lower bound, inclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_ge
>>> validate_ge(1, 0)
coola.validation.validate_gt ¶
validate_gt(
value: Any, low: Any, *, name: str = "value"
) -> None
Validate that value is strictly greater than low.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
low
|
Any
|
The lower bound, exclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_gt
>>> validate_gt(1, 0)
coola.validation.validate_in ¶
validate_in(
value: Any,
valid: Container[Any],
*,
name: str = "value",
) -> None
Validate that value belongs to valid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
valid
|
Container[Any]
|
The container of valid values. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
>>> from coola.validation import validate_in
>>> validate_in("a", ("a", "b", "c"))
coola.validation.validate_int ¶
validate_int(value: Any, *, name: str = 'value') -> None
Validate that value is an int.
Booleans are rejected, even though bool subclasses int.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> from coola.validation import validate_int
>>> validate_int(1)
coola.validation.validate_isinstance ¶
validate_isinstance(
value: Any,
cls: type | tuple[type, ...] | UnionType,
*,
name: str = "value",
) -> None
Validate that value is an instance of cls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
cls
|
type | tuple[type, ...] | UnionType
|
The type or tuple of types that |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> from coola.validation import validate_isinstance
>>> validate_isinstance(1, int)
coola.validation.validate_le ¶
validate_le(
value: Any, high: Any, *, name: str = "value"
) -> None
Validate that value is less than or equal to high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
high
|
Any
|
The upper bound, inclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_le
>>> validate_le(0, 1)
coola.validation.validate_lt ¶
validate_lt(
value: Any, high: Any, *, name: str = "value"
) -> None
Validate that value is strictly less than high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
high
|
Any
|
The upper bound, exclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_lt
>>> validate_lt(0, 1)
coola.validation.validate_not_empty ¶
validate_not_empty(
value: Sized, *, name: str = "value"
) -> None
Validate that value is not empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Sized
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_not_empty
>>> validate_not_empty([1, 2, 3])
coola.validation.validate_number ¶
validate_number(value: Any, *, name: str = 'value') -> None
Validate that value is a real number.
Booleans are rejected, even though bool subclasses int.
The NumPy scalars (numpy.float32, numpy.int64...) are
accepted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> from coola.validation import validate_number
>>> validate_number(1)
>>> validate_number(1.5)
coola.validation.validate_range ¶
validate_range(
value: Any,
low: Any,
high: Any,
*,
low_inclusive: bool = True,
high_inclusive: bool = True,
name: str = "value",
) -> None
Validate that value is in the range from low to high.
By default, the range is closed (low <= value <= high). The two
flags exclude a bound, which gives the open range low < value <
high or a half-open range. The range is written with the usual
notation in the error message, for example [0, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
low
|
Any
|
The lower bound. |
required |
high
|
Any
|
The upper bound. |
required |
low_inclusive
|
bool
|
If |
True
|
high_inclusive
|
bool
|
If |
True
|
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the range is empty (for example |
Example
>>> from coola.validation import validate_range
>>> validate_range(0.5, 0, 1)
>>> validate_range(0, 0, 1, high_inclusive=False)
coola.validation.comparison ¶
Contain functions to validate comparisons.
coola.validation.comparison.validate_finite ¶
validate_finite(
value: float, *, name: str = "value"
) -> None
Validate that value is finite (neither infinite nor NaN).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_finite
>>> validate_finite(1.0)
coola.validation.comparison.validate_ge ¶
validate_ge(
value: Any, low: Any, *, name: str = "value"
) -> None
Validate that value is greater than or equal to low.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
low
|
Any
|
The lower bound, inclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_ge
>>> validate_ge(1, 0)
coola.validation.comparison.validate_gt ¶
validate_gt(
value: Any, low: Any, *, name: str = "value"
) -> None
Validate that value is strictly greater than low.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
low
|
Any
|
The lower bound, exclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_gt
>>> validate_gt(1, 0)
coola.validation.comparison.validate_le ¶
validate_le(
value: Any, high: Any, *, name: str = "value"
) -> None
Validate that value is less than or equal to high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
high
|
Any
|
The upper bound, inclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_le
>>> validate_le(0, 1)
coola.validation.comparison.validate_lt ¶
validate_lt(
value: Any, high: Any, *, name: str = "value"
) -> None
Validate that value is strictly less than high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
high
|
Any
|
The upper bound, exclusive. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_lt
>>> validate_lt(0, 1)
coola.validation.comparison.validate_range ¶
validate_range(
value: Any,
low: Any,
high: Any,
*,
low_inclusive: bool = True,
high_inclusive: bool = True,
name: str = "value",
) -> None
Validate that value is in the range from low to high.
By default, the range is closed (low <= value <= high). The two
flags exclude a bound, which gives the open range low < value <
high or a half-open range. The range is written with the usual
notation in the error message, for example [0, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
low
|
Any
|
The lower bound. |
required |
high
|
Any
|
The upper bound. |
required |
low_inclusive
|
bool
|
If |
True
|
high_inclusive
|
bool
|
If |
True
|
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the range is empty (for example |
Example
>>> from coola.validation import validate_range
>>> validate_range(0.5, 0, 1)
>>> validate_range(0, 0, 1, high_inclusive=False)
coola.validation.container ¶
Contain functions to validate containers.
coola.validation.container.validate_in ¶
validate_in(
value: Any,
valid: Container[Any],
*,
name: str = "value",
) -> None
Validate that value belongs to valid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
valid
|
Container[Any]
|
The container of valid values. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
>>> from coola.validation import validate_in
>>> validate_in("a", ("a", "b", "c"))
coola.validation.container.validate_not_empty ¶
validate_not_empty(
value: Sized, *, name: str = "value"
) -> None
Validate that value is not empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Sized
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Example
>>> from coola.validation import validate_not_empty
>>> validate_not_empty([1, 2, 3])
coola.validation.type ¶
Contain functions to validate types.
The checks are strict and do not convert the value.
validate_isinstance is the general function. validate_int and
validate_number exist because they reject bool, which subclasses
int. A validator is added only if it states a constraint that
isinstance or one comparison cannot state without a special case.
coola.validation.type.validate_int ¶
validate_int(value: Any, *, name: str = 'value') -> None
Validate that value is an int.
Booleans are rejected, even though bool subclasses int.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> from coola.validation import validate_int
>>> validate_int(1)
coola.validation.type.validate_isinstance ¶
validate_isinstance(
value: Any,
cls: type | tuple[type, ...] | UnionType,
*,
name: str = "value",
) -> None
Validate that value is an instance of cls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
cls
|
type | tuple[type, ...] | UnionType
|
The type or tuple of types that |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> from coola.validation import validate_isinstance
>>> validate_isinstance(1, int)
coola.validation.type.validate_number ¶
validate_number(value: Any, *, name: str = 'value') -> None
Validate that value is a real number.
Booleans are rejected, even though bool subclasses int.
The NumPy scalars (numpy.float32, numpy.int64...) are
accepted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to validate. |
required |
name
|
str
|
The name of the value, used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> from coola.validation import validate_number
>>> validate_number(1)
>>> validate_number(1.5)