[New-bugs-announce] [issue38131] compile(mode='eval') uninformative error message

Ilya Kamenshchikov report at bugs.python.org
Thu Sep 12 06:21:59 EDT 2019

New submission from Ilya Kamenshchikov <ikamenshchikov at gmail.com>:

While trying to construct a valid ast node programmatically, I have tried following: 

import ast

tree = ast.BinOp(left=ast.Num(n=2), right=ast.Num(n=2), op=ast.Add())
expr = ast.Expression(body=[tree])
exe = compile(expr, filename="", mode="eval")

Unfortunately this gives unhelpful error message:

>>>    exe = compile(expr, filename="", mode="eval")
TypeError: required field "lineno" missing from expr

Turns out I need to make body of ast.Expression not a list, but a node:
expr = ast.Expression(body=tree)  # works

Confusion also comes from naming the field "body", which takes value of a list for ast.Module and some others.

components: Library (Lib)
messages: 352090
nosy: Ilya Kamenshchikov
priority: normal
severity: normal
status: open
title: compile(mode='eval') uninformative error message
versions: Python 3.6, Python 3.7, Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list