(
    val: types.AnyVal | Collection[types.AnyVal],
    val_type: Type[Any] | Tuple[Type[Any], ...],
    col_type: Type[Any],
)
Validate and cast a value or values to a collection.

    Args:
        val (object): Value or values to validate and cast.
        val_type (type): Type of each value in collection, e.g. ``int`` or ``(str, bytes)``.
        col_type (type): Type of collection to return, e.g. ``tuple`` or ``set``.

    Returns:
        Collection of type ``col_type`` with values all of type ``val_type``.

    Raises:
        TypeError
Validate and cast a value or values to a collection.
Validate and cast a value or values to a collection .
def to_collection(
    val: types.AnyVal | Collection[types.AnyVal],
    val_type: Type[Any] | Tuple[Type[Any], ...],
    col_type: Type[Any],
) -> Collection[types.AnyVal]:
    """
    Validate and cast a value or values to a collection.

    Args:
        val (object): Value or values to validate and cast.
        val_type (type): Type of each value in collection, e.g. ``int`` or ``(str, bytes)``.
        col_type (type): Type of collection to return, e.g. ``tuple`` or ``set``.

    Returns:
        Collection of type ``col_type`` with values all of type ``val_type``.

    Raises:
        TypeError
    """
    if val is None:
        return None
    if isinstance(val, val_type):
        return col_type([val])
    elif isinstance(val, (tuple, list, set, frozenset)):
        if not all(isinstance(v, val_type) for v in val):
            raise TypeError(f"not all values are of type {val_type}")
        return col_type(val)
    else:
        # TODO: use standard error message, maybe?
        raise TypeError(
            f"values must be {val_type} or a collection thereof, not {type(val)}"
        )
def to_collection ( val : types . AnyVal | Collection [ types . AnyVal ] , val_type : Type [ Any ] | Tuple [ Type [ Any ] , ... ] , col_type : Type [ Any ] , ) -> Collection [ types . AnyVal ] : if val is None : return None if isinstance ( val , val_type ) : return col_type ( [ val ] ) elif isinstance ( val , ( tuple , list , set , frozenset ) ) : if not all ( isinstance ( v , val_type ) for v in val ) : raise TypeError ( f"not all values are of type {val_type}" ) return col_type ( val ) else : # TODO: use standard error message, maybe? raise TypeError ( f"values must be {val_type} or a collection thereof, not {type(val)}" )