Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
GitHub fields:
assignee = None closed_at = None created_at = <Date 2019-09-12.10:21:59.042> labels = ['interpreter-core', '3.9'] title = "compile(mode='eval') uninformative error message" updated_at = <Date 2020-05-07.15:16:43.409> user = 'https://github.com/ikamensh'
bugs.python.org fields:
activity = <Date 2020-05-07.15:16:43.409> actor = 'BTaskaya' assignee = 'none' closed = False closed_date = None closer = None components = ['Interpreter Core'] creation = <Date 2019-09-12.10:21:59.042> creator = 'Ilya Kamenshchikov' dependencies = [] files = [] hgrepos = [] issue_num = 38131 keywords = ['patch'] message_count = 3.0 messages = ['352090', '358905', '368351'] nosy_count = 4.0 nosy_names = ['benjamin.peterson', 'Ilya Kamenshchikov', 'pablogsal', 'BTaskaya'] pr_nums = ['17715'] priority = 'normal' resolution = None stage = 'patch review' status = 'open' superseder = None type = None url = 'https://bugs.python.org/issue38131' versions = ['Python 3.9']
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.
with PR 17715
>>> import ast >>> expr_without_lineno_but_ok = ast.Expression(body=ast.BinOp(left=ast.Num(n=2), right=ast.Num(n=2), op=ast.Add())) >>> expr_with_lineno_but_with_wrong_body = ast.Expression(body=[ast.BinOp(left=ast.Num(n=2), right=ast.Num(n=2), op=ast.Add())]) >>> ast.fix_missing_locations(expr_with_lineno_but_with_wrong_body) >>> compile(expr_without_lineno_but_ok, "<temp>", "eval") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: required field "lineno" missing from expr >>> compile(expr_with_lineno_but_with_wrong_body, "<temp>", "eval") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: expected some sort of expr, but got [<_ast.BinOp object at 0x7f3f57dc4c80>]
I've updated PR 17715 in order to point out exact field that error happened. Also it now only shows type
>>> import ast >>> expr_without_lineno_but_ok = ast.Expression(body=ast.BinOp(left=ast.Num(n=2), right=ast.Num(n=2), op=ast.Add())) >>> expr_with_lineno_but_with_wrong_body = ast.Expression(body=[ast.BinOp(left=ast.Num(n=2), right=ast.Num(n=2), op=ast.Add())]) >>> ast.fix_missing_locations(expr_with_lineno_but_with_wrong_body) <ast.Expression object at 0x7f854123fbb0> >>> compile(expr_without_lineno_but_ok, "<temp>", "eval") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: required field "lineno" missing from expr >>> compile(expr_with_lineno_but_with_wrong_body, "<temp>", "eval") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: For field 'body'; expected expr type node, got list
gh-82312: Further improvement of error messages for generating AST no…
Activity
ikamensh commentedon Sep 12, 2019
While trying to construct a valid ast node programmatically, I have tried following:
Unfortunately this gives unhelpful error message:
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.
isidentical commentedon Dec 27, 2019
with PR 17715
isidentical commentedon May 7, 2020
I've updated PR 17715 in order to point out exact field that error happened. Also it now only shows type
gh-82312: Further improvement of error messages for generating AST no…