(exp)
Validate an `ast.AST` expression.

    Only expressions with no list,set,dict,generator comprehensions
    are accepted.

    Parameters
    ----------
    exp : ast.AST
        A parsed abstract syntax tree
Validate an `ast.AST` expression.
Validate an ast . AST expression .
def validate_exp(exp):
    """
    Validate an `ast.AST` expression.

    Only expressions with no list,set,dict,generator comprehensions
    are accepted.

    Parameters
    ----------
    exp : ast.AST
        A parsed abstract syntax tree

    """
    # pylint: disable=too-many-branches
    if not isinstance(exp, ast.AST):
        raise TypeError("exp is not a 'ast.AST' instance")

    etype = type(exp)
    if etype in [ast.Expr, ast.Expression]:
        return validate_exp(exp.body)
    elif etype == ast.BoolOp:
        return all(map(validate_exp, exp.values))
    elif etype == ast.BinOp:
        return all(map(validate_exp, [exp.left, exp.right]))
    elif etype == ast.UnaryOp:
        return validate_exp(exp.operand)
    elif etype == ast.IfExp:
        return all(map(validate_exp, [exp.test, exp.body, exp.orelse]))
    elif etype == ast.Dict:
        return all(map(validate_exp, chain(exp.keys, exp.values)))
    elif etype == ast.Set:
        return all(map(validate_exp, exp.elts))
    elif etype == ast.Compare:
        return all(map(validate_exp, [exp.left] + exp.comparators))
    elif etype == ast.Call:
        subexp = chain([exp.func], exp.args or [],
                       [k.value for k in exp.keywords or []])
        return all(map(validate_exp, subexp))
    elif etype == ast.Starred:
        assert isinstance(exp.ctx, ast.Load)
        return validate_exp(exp.value)
    elif etype in [ast.Num, ast.Str, ast.Bytes, ast.Ellipsis, ast.NameConstant]:
        return True
    elif etype == ast.Constant:
        return True
    elif etype == ast.Attribute:
        return True
    elif etype == ast.Subscript:
        return all(map(validate_exp, [exp.value, exp.slice]))
    elif etype in {ast.List, ast.Tuple}:
        assert isinstance(exp.ctx, ast.Load)
        return all(map(validate_exp, exp.elts))
    elif etype == ast.Name:
        return True
    elif etype == ast.Slice:
        return all(map(validate_exp,
                       filter(None, [exp.lower, exp.upper, exp.step])))
    elif etype == ast.ExtSlice:
        return all(map(validate_exp, exp.dims))
    elif etype == ast.Index:
        return validate_exp(exp.value)
    elif etype == ast.keyword:
        return validate_exp(exp.value)
    else:
        raise ValueError(exp)
def validate_exp ( exp ) : # pylint: disable=too-many-branches if not isinstance ( exp , ast . AST ) : raise TypeError ( "exp is not a 'ast.AST' instance" ) etype = type ( exp ) if etype in [ ast . Expr , ast . Expression ] : return validate_exp ( exp . body ) elif etype == ast . BoolOp : return all ( map ( validate_exp , exp . values ) ) elif etype == ast . BinOp : return all ( map ( validate_exp , [ exp . left , exp . right ] ) ) elif etype == ast . UnaryOp : return validate_exp ( exp . operand ) elif etype == ast . IfExp : return all ( map ( validate_exp , [ exp . test , exp . body , exp . orelse ] ) ) elif etype == ast . Dict : return all ( map ( validate_exp , chain ( exp . keys , exp . values ) ) ) elif etype == ast . Set : return all ( map ( validate_exp , exp . elts ) ) elif etype == ast . Compare : return all ( map ( validate_exp , [ exp . left ] + exp . comparators ) ) elif etype == ast . Call : subexp = chain ( [ exp . func ] , exp . args or [ ] , [ k . value for k in exp . keywords or [ ] ] ) return all ( map ( validate_exp , subexp ) ) elif etype == ast . Starred : assert isinstance ( exp . ctx , ast . Load ) return validate_exp ( exp . value ) elif etype in [ ast . Num , ast . Str , ast . Bytes , ast . Ellipsis , ast . NameConstant ] : return True elif etype == ast . Constant : return True elif etype == ast . Attribute : return True elif etype == ast . Subscript : return all ( map ( validate_exp , [ exp . value , exp . slice ] ) ) elif etype in { ast . List , ast . Tuple } : assert isinstance ( exp . ctx , ast . Load ) return all ( map ( validate_exp , exp . elts ) ) elif etype == ast . Name : return True elif etype == ast . Slice : return all ( map ( validate_exp , filter ( None , [ exp . lower , exp . upper , exp . step ] ) ) ) elif etype == ast . ExtSlice : return all ( map ( validate_exp , exp . dims ) ) elif etype == ast . Index : return validate_exp ( exp . value ) elif etype == ast . keyword : return validate_exp ( exp . value ) else : raise ValueError ( exp )