jangada.serialization.check_types#

jangada.serialization.check_types(obj: Any, types: type | tuple[type], can_be_none: bool = False, raise_error: bool = True) bool#

Check if an object is an instance of the specified type(s).

Parameters:
objAny

The object to check.

typestype | tuple[type]

A single type or tuple of types to check against.

can_be_nonebool, optional

If True, None is considered a valid value. Default is False.

raise_errorbool, optional

If True, raise TypeError on type mismatch. If False, return False instead. Default is True.

Returns:
bool

True if the object matches the expected type(s), False otherwise (only when raise_error=False).

Raises:
TypeError

If the object does not match the expected type(s) and raise_error=True.

Examples

>>> check_types(5, int)
True
>>> check_types("hello", (int, str))
True
>>> check_types(None, int, can_be_none=True)
True
>>> check_types(5, str, raise_error=False)
False
>>> check_types(5, str)
Traceback (most recent call last):
    ...
TypeError: Expected instance of one of the following classes: str. Given int instead