This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Ilya Kamenshchikov
Recipients Ilya Kamenshchikov
Date 2019-09-12.10:21:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1568283719.06.0.851695472479.issue38131@roundup.psfhosted.org>
In-reply-to
Content
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])
ast.fix_missing_locations(expr)
exe = compile(expr, filename="", mode="eval")
print(eval(exe))

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.
History
Date User Action Args
2019-09-12 10:21:59Ilya Kamenshchikovsetrecipients: + Ilya Kamenshchikov
2019-09-12 10:21:59Ilya Kamenshchikovsetmessageid: <1568283719.06.0.851695472479.issue38131@roundup.psfhosted.org>
2019-09-12 10:21:59Ilya Kamenshchikovlinkissue38131 messages
2019-09-12 10:21:58Ilya Kamenshchikovcreate