code: def checked_cast(
      typ: Type[T], val: V, exception: Optional[Exception] = None
  ) -> T:
      """
      Cast a value to a type (with a runtime safety check).
  
      Returns the value unchanged and checks its type at runtime. This signals to the
      typechecker that the value has the designated type.
  
      Like `typing.cast`_ ``check_cast`` performs no runtime conversion on its argument,
      but, unlike ``typing.cast``, ``checked_cast`` will throw an error if the value is
      not of the expected type. The type passed as an argument should be a python class.
  
      Args:
          typ: the type to cast to
          val: the value that we are casting
          exception: override exception to raise if  typecheck fails
      Returns:
          the ``val`` argument, unchanged
  
      .. _typing.cast: https://docs.python.org/3/library/typing.html#typing.cast
      """
      if not isinstance(val, typ):
          raise exception if exception is not None else ValueError(
              f"Value was not of type {typ}:\n{val}"
          )
      return val
  
file_path: Ax/ax/utils/common/typeutils.py
imports: from inspect import signature
  from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar
  import numpy
  from typeguard import check_type
label_desc: Use image node_id 2 for calling a global function with example usage: checked_cast(typ, val, exception) and returns: val