Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit optional fields from ast.dump() #116023

Closed
JelleZijlstra opened this issue Feb 28, 2024 · 4 comments
Closed

Omit optional fields from ast.dump() #116023

JelleZijlstra opened this issue Feb 28, 2024 · 4 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Feb 28, 2024

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

@JelleZijlstra JelleZijlstra added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Feb 28, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Feb 28, 2024
sobolevn added a commit that referenced this issue Apr 24, 2024
Co-authored-by: Carl Meyer <carl@oddbird.net>
@JelleZijlstra
Copy link
Member Author

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.

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 28, 2024

>>> 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?

@AlexWaygood
Copy link
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!

@JelleZijlstra
Copy link
Member Author

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
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants