This issue tracker will soon become read-only and move to GitHub.
For a smoother transition, remember to log in and link your GitHub username to your profile.
For more information, see this post about the migration.

classification
Title: compile(mode='eval') uninformative error message
Type: Stage: patch review
Components: Interpreter Core Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, Ilya Kamenshchikov, benjamin.peterson, pablogsal
Priority: normal Keywords: patch

Created on 2019-09-12 10:21 by Ilya Kamenshchikov, last changed 2020-05-07 15:16 by BTaskaya.

Pull Requests
URL Status Linked Edit
PR 17715 open BTaskaya, 2019-12-27 16:24
Messages (3)
msg352090 - (view) Author: Ilya Kamenshchikov (Ilya Kamenshchikov) * Date: 2019-09-12 10:21
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.
msg358905 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-12-27 16:27
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>]
msg368351 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-05-07 15:16
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
History
Date User Action Args
2020-05-07 15:16:43BTaskayasetmessages: + msg368351
2020-02-14 20:58:12BTaskayasetnosy: + benjamin.peterson, pablogsal
2019-12-27 16:27:01BTaskayasetmessages: + msg358905
2019-12-27 16:24:27BTaskayasetnosy: + BTaskaya

components: + Interpreter Core, - Library (Lib)
versions: + Python 3.9, - Python 3.6, Python 3.7, Python 3.8
2019-12-27 16:24:04BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request17160
2019-09-12 10:21:59Ilya Kamenshchikovcreate