Skip to content

gh-148981: Add color to ast.dump#148982

Open
StanFromIreland wants to merge 10 commits intopython:mainfrom
StanFromIreland:ast.dump-color!
Open

gh-148981: Add color to ast.dump#148982
StanFromIreland wants to merge 10 commits intopython:mainfrom
StanFromIreland:ast.dump-color!

Conversation

@StanFromIreland
Copy link
Copy Markdown
Member

@StanFromIreland StanFromIreland commented Apr 25, 2026

For example, echo 'def f(): x = f"{1_0}"; y = True' | ./python -m ast:

Before After
image image

📚 Documentation preview 📚: https://cpython-previews--148982.org.readthedocs.build/

Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break people who want to retain a string (ast.dump returns a string, it doesn't print something). Please don't do this but only do it for the CLI.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Apr 25, 2026

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@StanFromIreland
Copy link
Copy Markdown
Member Author

StanFromIreland commented Apr 25, 2026

This will break people who want to retain a string (ast.dump returns a string, it doesn't print something). Please don't do this but only do it for the CLI.

Ok, I made color=False the default.

I have made the requested changes; please review again

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 25, 2026

I reformulate what I said in the issue: it won't be possible to add colors to the CLI only as we would get a string only, so ok for this additional parameter. However, node that there are sometimes hidden attributes in the dump, so choose another color for them as well.

Comment thread Doc/whatsnew/3.15.rst
Comment thread Lib/ast.py Outdated
Comment thread Lib/ast.py Outdated
@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 25, 2026

Actually, I don't know whether your approach is entirely fine. AST nodes can contain arbitrary strings and this can definitely mess the output. You can either (1) change how the formatted string is constructed (depending on color) [in which case you retain the color keyword] or (2) don't add a color keyword and only add it to the CLI and then colorize the entire string after it's been dumped with the regex.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 25, 2026

Note: I'm not entirely sure we can have a perfect colorization during the formatting as we use repr. Though, we know that ast nodes's repr start with their class name so we can colorize the prefix properly. Colorizing at the end just for the CLI may seem ok though however we should make sure that we don't have ANSI control codes inside already, don't we?

@StanFromIreland StanFromIreland requested a review from picnixz April 25, 2026 12:20
@StanFromIreland
Copy link
Copy Markdown
Member Author

StanFromIreland commented Apr 25, 2026

Thanks for the review Bénédikt, I changed it to add color directly in _format.

Comment thread Lib/test/test_ast/test_ast.py
@StanFromIreland StanFromIreland requested a review from picnixz April 25, 2026 12:30
Comment thread Lib/ast.py Outdated
Comment thread Lib/ast.py Outdated
@StanFromIreland StanFromIreland requested a review from picnixz April 25, 2026 12:52
Comment thread Lib/ast.py
Comment thread Lib/ast.py Outdated
Comment thread Lib/ast.py Outdated
Comment thread Lib/ast.py Outdated
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Member

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@StanFromIreland
Copy link
Copy Markdown
Member Author

Fuzz failure is unrelated, I think something must be messing with the path. I’ll look into it. CC @sethmlarson

Copy link
Copy Markdown
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw we can now remove color=True from argparse.ArgumentParser(color=True) as it's the default (and in fact could remove from other CLIs).

I recommend replacing it with formatter_class=argparse.ArgumentDefaultsHelpFormatter to also include colour defaults in the help (and remove the hardcoded defaults in help=).

Comment thread Doc/library/ast.rst
Comment thread Doc/whatsnew/3.15.rst
Comment thread Lib/ast.py Outdated
@StanFromIreland
Copy link
Copy Markdown
Member Author

btw we can now remove color=True from argparse.ArgumentParser(color=True) as it's the default (and in fact could remove from other CLIs).

I recommend replacing it with formatter_class=argparse.ArgumentDefaultsHelpFormatter to also include colour defaults in the help (and remove the hardcoded defaults in help=).

I did those too.

@JelleZijlstra
Copy link
Copy Markdown
Member

Does it make sense to support ast.dump(color=None) and have it either use or not use color depending on the env? Do we do that in other similar cases?

@StanFromIreland
Copy link
Copy Markdown
Member Author

StanFromIreland commented Apr 25, 2026

Does it make sense to support ast.dump(color=None) and have it either use or not use color depending on the env? Do we do that in other similar cases?

Hmm, we don't have a precedent, the other APIs with color support (e.g. difflib.unified_diff) are all True or False too.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 25, 2026

I think it was a mistake to have color=True for unified_diff, or more generally for functions that return strings because they are actually breaking APIs that expect plain strings (possibly to render to something else afterwards that does not support colors at all)

@StanFromIreland
Copy link
Copy Markdown
Member Author

As for ast.dump(), IMO it's best to just leave it as True/False.

@sunmy2019
Copy link
Copy Markdown
Member

sunmy2019 commented Apr 26, 2026

Does it make sense to support ast.dump(color=None) and have it either use or not use color depending on the env? Do we do that in other similar cases?

Does it make sense? No.

ast.dump is env irrelevant. It it not attached to some file.

It makes sense if we have something like ast.print which accepts the file input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants