def _check_attribute(obj, attr, typ, allow_none=False):
    """Check that a given attribute attr exists in the object obj and
    is of type typ. Optionally, allow the attribute to be None.
    """
    if not hasattr(obj, attr):
        return False

    val = getattr(obj, attr)
    return isinstance(val, typ) or (allow_none and val is None)