def assert_type(x, types, none_okay=False, error_message='type check failed'):
    """
    Checks that `x` is an instance of `types`

    If this is not true, raise an `AttributeError`

    Parameters
    ----------
    x : any
        A value
    types : class or list or tuple of classes
        The types of `x` to assert
    none_okay : bool
        Is it okay if `x` is None?
    error_message : str
        Message to return if `x` is not of `types`
    """
    if not (none_okay and x is None or isinstance(x, types)):
        raise AttributeError(error_message)
