code='1+1'
import ast
expr = ast.parse(code).body[0]
print(type(expr))
compile(ast.Expression(expr), 'string', "eval")
gets me
class '_ast.Expr'
Traceback (most recent call last): File "test_ast.py", line 6, in compile(ast.Expression(expr), '', "eval") TypeError: expected some sort of expr, but got <_ast.Expr object at> 0x7fe89442d9e8>
compile(expr, '<string>', "eval")
does not works either:
TypeError: expected Expression node, got Expr
An interesting explanation about Expressions can be found here.
But basically, the answer's first paragraph says it all:
So, in your case, you would need to dump the Expr first:
TLDR : replace expr by ast.Expression(expr.value)
A comment on this Convert ast node into python object and makeMonday answer gave me the solution: