def smart_cast(var, type_):
    if type_ is None or var is None:
        return var
    try:
        if issubclass(type_, type(None)):
            return var
    except TypeError:
        pass
    if is_str(var):
        if type_ in VALID_BOOL_TYPES:
            return bool_from_str(var)
        elif type_ is slice:
            args = [None if len(arg) == 0 else int(arg) for arg in var.split(':')]
            return slice(*args)
        elif type_ is list:
            subvar_list = var.split(',')
            return [smart_cast2(subvar) for subvar in subvar_list]
        elif isinstance(type_, six.string_types):
            if type_ == 'fuzzy_subset':
                return fuzzy_subset(var)
            if type_ == 'eval':
                return eval(var, {}, {})
            else:
                raise NotImplementedError('Uknown smart type_=%r' % (type_,))
    return type_(var)