def try_enum(cls, val):
    """A function that tries to turn the value into enum ``cls``.

    If it fails it returns the value instead.
    """
    try:
        return cls._enum_value_map_[val]
    except (KeyError, TypeError, AttributeError):
        return val
