Skip to content

Omit optional fields from ast.dump() #116023

Closed

Description

@JelleZijlstra

Feature or enhancement

Proposal:

>>> ast.dump(ast.arguments())
'arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[])'

After #105858, it would be nice if ast.dump() also didn't output optional fields that are set to None or an empty string, so that its output is more concise. This should make it easier to understand the structure of large ASTs where most fields are missing.

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

Activity

  1. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 28, 2024
  2. added a commit that references this issue on Feb 28, 2024
  3. added a commit that references this issue on Apr 24, 2024
  4. JelleZijlstra commented on Apr 28, 2024

    @JelleZijlstra
    MemberAuthor

    I noticed that the representation on an empty ast.Tuple changed from Tuple(elts=[], ctx=Load()) to Tuple(ctx=Load()), and same for lists and dicts:

    >>> ast.dump(ast.parse('[]'))
    'Module(body=[Expr(value=List(ctx=Load()))])'
    >>> ast.dump(ast.parse('{}'))
    'Module(body=[Expr(value=Dict())])'
    >>> ast.dump(ast.parse('()'))
    'Module(body=[Expr(value=Tuple(ctx=Load()))])'
    

    I think this is fine; it matches the constructor and it is analogous to how list(), dict(), and tuple() work. However, I thought I'd flag it in case others think these nodes should be special-cased.

  5. AlexWaygood commented on Apr 28, 2024

    @AlexWaygood
    Member
    >>> ast.dump(ast.parse('[]'))
    'Module(body=[Expr(value=List(ctx=Load()))])'
    >>> ast.dump(ast.parse('{}'))
    'Module(body=[Expr(value=Dict())])'
    >>> ast.dump(ast.parse('()'))
    'Module(body=[Expr(value=Tuple(ctx=Load()))])'

    Why does the repr for ast.Dict() not include ctx=Load() in it, when the reprs for Tuple and List do?

  6. AlexWaygood commented on Apr 28, 2024

    @AlexWaygood
    Member

    Why does the repr for ast.Dict() not include ctx=Load() in it, when the reprs for Tuple and List do?

    I see that the same thing happens on Python 3.12, so this definitely isn't a regression... but I'd still be curious if anybody knows why this is different for the different nodes!

  7. JelleZijlstra commented on Apr 28, 2024

    @JelleZijlstra
    MemberAuthor

    It's because ast.Dict can't appear on the left-hand side of an assignment.

    >>> [x] = (3,)
    >>> (x,) = (3,)
    >>> {x: x} = {3: 3}
      File "<stdin>", line 1
        {x: x} = {3: 3}
        ^^^^^^
    SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
    

    I was thinking of whether we'd also want to default the ctx parameter of these and other nodes to Load(), but that's for another day.

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/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions