| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
As mentioned in #131507 and #130645, theming support is an important piece of the puzzle in introducing color to Python output in the terminal.
It should be as easy to use theming as to operate with colors by hand.
It should be trivially possible for the user to set up their own favorite colors in their $PYTHONSTARTUP script.
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.
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.
It should be impossible for user code to modify the default theme in place as this can lead to inconsistencies.
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.
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 import _colorize in their $PYTHONSTARTUP script and use set_theme() to set it to a new value. This new value can be:
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.
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.
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.
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.
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.
| Back | FazBrowse Home | New Git URL |
Activity
pythongh-133346: Make theming support in _colorize extensible
gh-133346: Make theming support in _colorize extensible (GH-133347)
hugovk commented on May 21, 2025
This is done for 3.14 beta 🎨
pythongh-133346: Make theming support in _colorize extensible (python…
gh-133346: make `_colorize.Argparse` kw-only constructible (#140620)
pythongh-133346: make `_colorize.Argparse` kw-only constructible (pyt…
[3.14] gh-133346: make `_colorize.Argparse` kw-only constructible (GH…
gh-133346: add tests for `_colorize.Theme` (#139687)
pythongh-133346: add tests for `_colorize.Theme` (pythonGH-139687)
[3.14] gh-133346: add tests for `_colorize.Theme` (GH-139687) (#140622)
pythongh-133346: make `_colorize.Argparse` kw-only constructible (pyt…
pythongh-133346: add tests for `_colorize.Theme` (python#139687)
yurikhan commented on Apr 29, 2026
The way this was accomplished is really unfortunate.
jonathandung commented on Jun 18, 2026
Any updates on the stable API thing? Will the _colorize module become an alias of, say, a new stdlib colorize module?