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 — gh-148981: Add color to `ast.dump` by StanFromIreland · Pull Request #148982 · python/cpython

Conversation

@StanFromIreland

@StanFromIreland StanFromIreland commented Apr 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

Before After
image image
  • Issue: Add colour to ast.dump output #148981

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

johnslavik and iritkatriel reacted with heart emoji
All reactions
  • ❤️ 2 reactions
@StanFromIreland
Add color to ast.dump
e3feeae
@StanFromIreland
StanFromIreland requested review from AA-Turner, Eclips4, JelleZijlstra, hugovk, isidentical and tomasr8 as code owners April 25, 2026 11:49
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 25, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Apr 25, 2026
Add colour to ast.dump output #148981
Closed
@StanFromIreland
Add the blurb
fd0ec73
picnixz
picnixz requested changes Apr 25, 2026
View reviewed changes

@picnixz picnixz left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sunmy2019 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot removed the awaiting core review label Apr 25, 2026
@bedevere-app

bedevere-app Bot commented Apr 25, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting changes label Apr 25, 2026
@StanFromIreland
Make color=False by default
3bda2fe
@StanFromIreland

StanFromIreland commented Apr 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Apr 25, 2026

Copy link
Copy Markdown
Member

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.

StanFromIreland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed Apr 25, 2026
View reviewed changes
Comment thread Doc/whatsnew/3.15.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Apr 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Apr 25, 2026

Copy link
Copy Markdown
Member

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland
Rewrite to add colors directly in _format()
65fbf80
@StanFromIreland
StanFromIreland requested a review from picnixz April 25, 2026 12:20
@StanFromIreland

StanFromIreland commented Apr 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland
Revert some leftovers from the old
e71c217
picnixz
picnixz reviewed Apr 25, 2026
View reviewed changes
Comment thread Lib/test/test_ast/test_ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland
Expand test
1e59cad
@StanFromIreland
StanFromIreland requested a review from picnixz April 25, 2026 12:30
picnixz
picnixz reviewed Apr 25, 2026
View reviewed changes
Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed Apr 25, 2026
View reviewed changes
Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland
Split docstring, avoid recursion
3adc040
@StanFromIreland
StanFromIreland requested a review from picnixz April 25, 2026 12:52
picnixz
picnixz reviewed Apr 25, 2026
View reviewed changes
Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

StanFromIreland added 2 commits April 25, 2026 13:56
@StanFromIreland
Keep that one as repr(value)
7b3c72a
@StanFromIreland
Further refinement to docstring and no more fstring :-(
997e18c
picnixz
picnixz approved these changes Apr 25, 2026
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting changes labels Apr 25, 2026
Eclips4
Eclips4 approved these changes Apr 25, 2026
View reviewed changes

@Eclips4 Eclips4 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@StanFromIreland

StanFromIreland commented Apr 25, 2026

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hugovk
hugovk approved these changes Apr 25, 2026
View reviewed changes

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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=).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/ast.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/whatsnew/3.15.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ast.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland
Hugo's suggestions
5d51438
@StanFromIreland

StanFromIreland commented Apr 25, 2026

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.

hugovk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Apr 25, 2026

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?

johnslavik reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland

StanFromIreland commented Apr 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Apr 25, 2026

Copy link
Copy Markdown
Member

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland

StanFromIreland commented Apr 25, 2026

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sunmy2019

sunmy2019 commented Apr 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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?

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland
StanFromIreland merged commit 6d4ca16 into python:main Apr 26, 2026
108 of 109 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 26, 2026
@StanFromIreland
StanFromIreland deleted the ast.dump-color! branch April 26, 2026 09:16
@StanFromIreland

StanFromIreland commented Apr 26, 2026

Copy link
Copy Markdown
Member Author

Thanks all for the reviews!

hugovk reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

This comment was marked as outdated.

Sign in to view
@StanFromIreland

This comment was marked as resolved.

Sign in to view
@picnixz

This comment was marked as outdated.

Sign in to view
@picnixz

This comment was marked as outdated.

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

Reviewers

@hugovk hugovk hugovk approved these changes
@picnixz picnixz picnixz approved these changes
@Eclips4 Eclips4 Eclips4 approved these changes
@tomasr8 tomasr8 Awaiting requested review from tomasr8 tomasr8 is a code owner
@isidentical isidentical Awaiting requested review from isidentical isidentical is a code owner
@JelleZijlstra JelleZijlstra Awaiting requested review from JelleZijlstra JelleZijlstra is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@StanFromIreland @picnixz @JelleZijlstra @sunmy2019 @hugovk @Eclips4
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — 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.