cristal.utils.type_checking module#
This module provides a decorator for type checking function arguments.
- cristal.utils.type_checking.check_all_int(value)#
Check if all elements in the value are integers.
- Return type:
bool
- cristal.utils.type_checking.check_all_int_or_float(value)#
Check if all elements in the value are either int or float.
- Return type:
bool
- cristal.utils.type_checking.check_all_of_type(value, expected_type)#
Check if all elements in the value are of the expected type.
- Return type:
bool
- cristal.utils.type_checking.check_any_condition(value, conditions)#
Check if the value satisfies any condition in the list.
- Return type:
bool
- cristal.utils.type_checking.check_in_list(value, valid_values)#
Check if the value is in the list of valid values.
- Return type:
bool
- cristal.utils.type_checking.check_multiple_conditions(value, conditions)#
Check if the value satisfies all conditions in the list.
- Return type:
bool
- cristal.utils.type_checking.check_none(value)#
Check if the value is None.
- Return type:
bool
- cristal.utils.type_checking.check_types(conditions=None)#
Decorator to check types of function arguments.
- Return type:
Callable
- Parameters:
conditions (dict[str, Callable[[Any], bool]] | None, optional) – Custom conditions for argument validation, by default None
- Returns:
The decorated function with type checks applied.
- Return type:
Any
- Raises:
TypeError – If an argument does not match its expected type or fails a custom condition.
Example
>>> @check_types({"a": lambda x: x > 0}) ... def my_function(a: int, b: str | None = None) -> None: ... pass
This will raise a TypeError if a is not an int greater than 0 or if b is not a str or None.
>>> @check_types() ... def another_function(x: float) -> None: ... pass
This will raise a TypeError if x is not float.
- cristal.utils.type_checking.positive_integer(value)#
Check if the value is a positive integer.
- Return type:
bool