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 — Theming support for _colorize · Issue #133346 · python/cpython

Theming support for _colorize #133346

New issue
Copy link
New issue
Copy link
Closed
Closed
Theming support for _colorize#133346
Copy link
Assignees
ambv
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@ambv
ambv
opened on May 3, 2025
Issue body actions

As mentioned in #131507 and #130645, theming support is an important piece of the puzzle in introducing color to Python output in the terminal.

Design goals

Simplicity of use with type safety

It should be as easy to use theming as to operate with colors by hand.

User configurability through $PYTHONSTARTUP

It should be trivially possible for the user to set up their own favorite colors in their $PYTHONSTARTUP script.

Theme exchange through PyPI

It should be possible for the user to pip install theme-something and set_theme(theme_something.theme) in their $PYTHONSTARTUP and be done with it.

Forward-compatibility of old themes

It should be possible for the user to keep using a theme from 3.14 in future versions of Python without changes. The new color definitions would be automatically taken from the default theme.

Immutability of theme objects

It should be impossible for user code to modify the default theme in place as this can lead to inconsistencies.

Implementation

Types

The _colorize module contains a Theme dataclass, which groups theme sections together. It's immutable. Each theme section corresponds to a theming use case, like the REPL or Argparse. Each theme section is its own dataclass as it will contain keys unique to the use case. Every theme section is also immutable.

For convenience, each theme section inherits from a ThemeSection mixin that provides the theme section with the capability to create a colorless variant or a copy with only some keys modified.

The _colorize module also exposes a default_theme and theme_no_color globals. Those can be copied to create derivative themes.

Using themes in stdlib code

Python standard libraries using color themes should be using the get_theme() function exclusively. This function will return the currently configured theme. If the end user disabled colors or stdout is not a TTY, the returned theme will contain empty strings for every theme section key. This allows the standard library using color themes to program assuming colors everywhere, and they will turn into no-ops in environments where color should not be emitted.

Implementations can optionally also directly call can_colorize() to avoid costly color-specific operations like parsing source code to syntax highlight it.

Importantly, standard libraries should never cache output of get_theme() and can_colorize() as those values can change during the execution of the program:

  • the end user can switch out a theme after your standard library was already imported;
  • they can switch out a theme in the interactive shell or through PDB;
  • can_colorize() might return a different answer if os.environ changes or the console mode changes (possible on Windows).

Configuration

The end user can import _colorize in their $PYTHONSTARTUP script and use set_theme() to set it to a new value. This new value can be:

  • a theme created in-place using regular dataclass instantiation and the convenience .copy_with() methods;
  • a theme imported from another module, possibly installed from PyPI.

In non-interactive use of Python, the $PYTHONSTARTUP file is not executed. For such use cases, customizing the theme can be achieved through a sitecustomize or a usercustomize module.

Rejected Ideas

Introduce a new configuration file

There are already too many configuration files. Given Python's extensible nature, a format like TOML or INI would become insufficient. This would require a lengthy discussion, in which time no theming support would be available to users at all.

Use simple dictionaries

This is easier to implement but makes type-checking harder, allows user code to override color codes at will (which can lead to broken terminal output), and provides less conveniences for creating derivative themes from the default one.

Introduce a theme like $LS_COLORS to $PYTHON_COLORS

Since there are many theming sections, the resulting string format would have to be very human-unfriendly. Setting it would affect Python-based applications that might want to handle theming themselves. Priority would be unclear.

Open issues

Rename _colorize to colorize

There's frankly very little time for this before Python 3.14 beta 1. I would rather keep the library name underscored for 3.14 and gather feedback from the community on the format and behavior, and introduce a stable API in Python 3.15, keeping _colorize for backward compatibility.

Linked PRs

  • gh-133346: Make theming support in _colorize extensible #133347
  • gh-133346: make _colorize.Argparse kw-only constructible #140620
  • [3.14] gh-133346: make _colorize.Argparse kw-only constructible (GH-140620) #140621
  • gh-133346: add tests for _colorize.Theme #139687
  • [3.14] gh-133346: add tests for _colorize.Theme (GH-139687) #140622
Reacted by Hugo van Kemenade, devdanzin, GalaxySnail and Duncan Holm

Activity

  1. ambv
    self-assigned this
    on May 3, 2025
  2. ambv
    added a commit that references this issue on May 3, 2025

    pythongh-133346: Make theming support in _colorize extensible

    3344008
  3. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 4, 2025
  4. hugovk
    marked Use a less red-ish color for the new prompt and non-error colorized traceback text #118626 as a duplicate of this issue on May 4, 2025
  5. ambv
    added a commit that references this issue on May 5, 2025

    gh-133346: Make theming support in _colorize extensible (GH-133347)

    f610bbd
  6. hugovk commented on May 21, 2025

    @hugovk
    hugovk
    on May 21, 2025
    Member
    More actions

    This is done for 3.14 beta 🎨

  7. hugovk
    closed this as completedon May 21, 2025
  8. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-133346: Make theming support in _colorize extensible (python…

    6048ede
  9. picnixz
    added a commit that references this issue on Oct 26, 2025

    gh-133346: make `_colorize.Argparse` kw-only constructible (#140620)

    387ac2d
  10. miss-islington
    added a commit that references this issue on Oct 26, 2025

    pythongh-133346: make `_colorize.Argparse` kw-only constructible (pyt…

    9f95ae1
  11. picnixz
    added 2 commits that reference this issue on Oct 26, 2025

    [3.14] gh-133346: make `_colorize.Argparse` kw-only constructible (GH…

    d664102

    gh-133346: add tests for `_colorize.Theme` (#139687)

    37827c1
  12. miss-islington
    added a commit that references this issue on Oct 26, 2025

    pythongh-133346: add tests for `_colorize.Theme` (pythonGH-139687)

    c115c09
  13. picnixz
    added a commit that references this issue on Oct 26, 2025

    [3.14] gh-133346: add tests for `_colorize.Theme` (GH-139687) (#140622)

    46a3fd2
  14. StanFromIreland
    added 2 commits that reference this issue on Dec 6, 2025

    pythongh-133346: make `_colorize.Argparse` kw-only constructible (pyt…

    c8e88fa

    pythongh-133346: add tests for `_colorize.Theme` (python#139687)

    068be65
  15. yurikhan commented on Apr 29, 2026

    @yurikhan
    yurikhan
    on Apr 29, 2026
    More actions

    The way this was accomplished is really unfortunate.

    • The default colors, are unreadable on dark backgrounds. Especially bold dark blue, which some terminals (traditionally but misguidedly) render as bold bright blue, but some don’t.
    • The customizability solution is local-only. SSHing to a remote server and running something like ansible-playbook --help there will use non-customized colors. (Unless I teach my ssh client to bring my usercustomize.py with me everywhere I go.)
    • The customizability solution is imperative, Turing-complete monkey-patching, rather than a declarative config. People who are in charge of servers I may ssh into will be suspicious of my bringing in executable code on every login.
  16. jonathandung commented on Jun 18, 2026

    @jonathandung
    jonathandung
    on Jun 18, 2026
    Contributor
    More actions

    Any updates on the stable API thing? Will the _colorize module become an alias of, say, a new stdlib colorize module?

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

Metadata

Metadata

Assignees

  • @ambv
    ambv

Labels

stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    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.