GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve `repr()` of AST nodes · Issue #116022 · python/cpython

Improve repr() of AST nodes #116022

New issue
Copy link
New issue
Copy link
Closed
#117046
Closed
Improve repr() of AST nodes#116022
#117046
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-parsertype-featureA feature request or enhancementA feature request or enhancement

Description

@JelleZijlstra
JelleZijlstra
opened on Feb 28, 2024
Issue body actions

Feature or enhancement

Proposal:

I often use ast.parse in the terminal to explore what the AST looks like:

>>> ast.parse("x = 3")
<ast.Module object at 0x105450b50>

But I have to remember to use ast.dump() to get useful output:

>>> ast.dump(ast.parse("x = 3"))
"Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Constant(value=3))], type_ignores=[])"

It would be nice if the default repr() of AST nodes was more like the output of ast.dump(), so it's easier to see at a glance how it works.

One concern would be around the size of the output:

>>> from pathlib import Path
>>> import typing
>>> typing_py = Path(typing.__file__).read_text()
>>> len(ast.dump(ast.parse(typing_py)))
304244

As a middle ground, we could limit the depth of the AST provided in the repr(), e.g. to 2 levels, and also the number of list elements provided.

The repr() of a module's AST might then look something like:

Module(body=[Expr(value=Constant(...)), ..., Assign(targets=[Name(...)], value=Constant(...))], type_ignores=[])

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-116022: Improve repr() of AST nodes #117046
Reacted by Daniël van Noord, Tomas R. and Alex Waygood

Activity

  1. JelleZijlstra
    added
    type-featureA feature request or enhancementA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-parser
    on Feb 28, 2024
  2. tomasr8 commented on Mar 10, 2024

    @tomasr8
    tomasr8
    on Mar 10, 2024
    Member
    More actions

    This is something that would be useful for me as well since I also sometimes poke around the AST 😄

    Were you planning to implement this yourself or do you think I could give it a try? Looking at the code, it'll probably have to be implemented in C since that's where the base AST type is defined.

  3. JelleZijlstra commented on Mar 10, 2024

    @JelleZijlstra
    JelleZijlstra
    on Mar 10, 2024
    MemberAuthor
    More actions

    I will do it eventually if no one else gets to it but haven't started yet. You're welcome to start working on it! This might need changes to asdl_c.py which is the Python code that generates the C code for the AST.

    Reacted by Tomas R.
  4. JelleZijlstra
    added a commit that references this issue on Sep 18, 2024

    gh-116022: Improve `repr()` of AST nodes (#117046)

    21d2a9a
  5. JelleZijlstra
    closed this as completedin #117046on Sep 18, 2024
  6. savannahostrowski
    added a commit that references this issue on Sep 22, 2024

    pythongh-116022: Improve `repr()` of AST nodes (python#117046)

    491004a
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

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-parsertype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.