Skip to content

compile(mode='eval') uninformative error message #82312

Open

Description

@ikamensh
mannequin
BPO 38131
Nosy @benjaminp, @ikamensh, @pablogsal, @isidentical
PRs
  • gh-82312: Improve messages when generating AST nodes from objects wi… #17715
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    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']

    Linked PRs

    Activity

    1. ikamensh commented on Sep 12, 2019

      ikamenshmannequin
      MannequinAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Sep 12, 2019
    3. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      and removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Dec 27, 2019
    4. isidentical commented on Dec 27, 2019

      @isidentical
      SponsorMember

      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>]
    5. isidentical commented on May 7, 2020

      @isidentical
      SponsorMember

      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
    6. transferred this issue fromon Apr 10, 2022
    7. added a commit that references this issue on Jun 29, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions