Skip to content

gh-145217: Add colour to pprint output#145218

Open
hugovk wants to merge 11 commits intopython:mainfrom
hugovk:3.15-pprint-colour
Open

gh-145217: Add colour to pprint output#145218
hugovk wants to merge 11 commits intopython:mainfrom
hugovk:3.15-pprint-colour

Conversation

@hugovk
Copy link
Copy Markdown
Member

@hugovk hugovk commented Feb 25, 2026

Example:

import pprint

data = ["spam", "eggs", ("lumberjack", "knights"), "ni", 42, None]
data.insert(0, data)

pprint.pp(data)
pprint.pprint(data)
pprint.PrettyPrinter().pprint(data)
image

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

@hugovk hugovk requested a review from AA-Turner as a code owner February 25, 2026 14:20
@hugovk hugovk added type-feature A feature request or enhancement stdlib Standard Library Python modules in the Lib/ directory labels Feb 25, 2026
@StanFromIreland

This comment was marked as resolved.

Comment thread Lib/pprint.py
@StanFromIreland

This comment was marked as resolved.

@hugovk

This comment was marked as resolved.

@johnslavik johnslavik self-requested a review April 10, 2026 10:13
Comment thread Doc/whatsnew/3.15.rst
Comment thread Lib/pprint.py
self._expand = bool(expand)
self._sort_dicts = sort_dicts
self._underscore_numbers = underscore_numbers
self._color = color
Copy link
Copy Markdown
Member

@sunmy2019 sunmy2019 Apr 28, 2026

Choose a reason for hiding this comment

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

Shall we add a bool(color) conversion just as a few lines above?

        self._compact = bool(compact)
        self._expand = bool(expand)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, can do, but not sure if it's necessary?

Copy link
Copy Markdown
Member

@sunmy2019 sunmy2019 left a comment

Choose a reason for hiding this comment

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

Do we have explicit test for color=True and can_colorize=False? It's an end to end test.

something like

        obj = {"key": "value"}
        stream = io.StringIO()

        # color=True should not produce no ANSI codes for streams
        # that do not support color
        pprint.pprint(obj, stream=stream, color=True)
        result = stream.getvalue()
        self.assertNotIn("\x1b[", result)

Comment thread Misc/NEWS.d/next/Library/2026-02-25-16-19-21.gh-issue-145217.QQBY0-.rst Outdated
Comment thread Lib/pprint.py Outdated
hugovk and others added 2 commits April 28, 2026 14:46
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Comment thread Lib/test/test_pprint.py Outdated
@sunmy2019
Copy link
Copy Markdown
Member

If user's __repr__ contains color, it would break the color.

image
Code

import os
import pprint

os.environ["FORCE_COLOR"] = "1"
os.environ["NO_COLOR"] = ""


class Colorful:
    """something with a colorful repr"""

    def __repr__(self):
        return "\x1b[31mred\x1b[0m"

pprint.pprint({"a": Colorful(), "b": 42, "c": "hello"}, color=True)

What should we do for this case?

  1. Document and keep as is?
  2. Pre check escape sequence before colorize?
  3. Other techniques for this?

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
@hugovk
Copy link
Copy Markdown
Member Author

hugovk commented Apr 28, 2026

  1. Pre check escape sequence before colorize?

I think if there's any colour escapes, we bail and print it as-is without extra colour from us. So we get something like this:

image

And I expect colour is rare in users' __repr__s? If so, this won't happen often, and when it does, we don't override any of their own colour or need more complex handling. And they don't end up with just a green "a" like in your example.

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

Labels

awaiting core review stdlib Standard Library Python modules in the Lib/ directory type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants