code: def to_collection(
      val: Optional[types.AnyVal | Collection[types.AnyVal]],
      val_type: Type[Any] | tuple[Type[Any], ...],
      col_type: Type[Any],
  ) -> Optional[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)}"
          )
  
imports: from __future__ import annotations
  import inspect
  import logging
  import pathlib
  import sys
  import warnings
  from typing import Any, Callable, Collection, Iterable, Literal, Optional, Type, Union, cast
  from .None import errors
  from .None import types
label_desc: Use image node_id 8 for calling a global function with example usage: to_collection(val, val_type, col_type) and returns: None, col_type, col_type