`Style` keeps the ANSI codes from the first color system it renders with

Open Beginner friendly
#4,261 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

Research direction

The bug is in Style._make_ansi_codes() in rich/style.py, which caches its result in Style._ansi without recording the color system, and Style.parse returns a shared cached instance. Run the reproduction snippet from the issue with the truecolor, standard and 256 color systems in different orders to see the wrong codes. Done when each console gets codes for its own color system, and a test covers rendering one style through two consoles with different color systems.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing tests that render the same markup through consoles with different color systems.

Style._make_ansi_codes() caches its result in Style._ansi, but the cache doesn't record which color system the codes were made for. Since Style.parse is cached too, every console that parses the same style string shares one Style instance, so whichever console renders it first decides the codes for all the others.

import io
from rich.console import Console

def show(color_system):
    console = Console(file=io.StringIO(), color_system=color_system, force_terminal=True)
    console.print("[#ff8800]orange[/]", end="")
    print(f"{color_system:>9}: {console.file.getvalue()!r}")

show("truecolor")
show("standard")
show("256")

Output on Rich 15.0.0 (and main at 3a109cc):

truecolor: '\x1b[38;2;255;136;0morange\x1b[0m'
 standard: '\x1b[38;2;255;136;0morange\x1b[0m'
      256: '\x1b[38;2;255;136;0morange\x1b[0m'

The standard and 256-color consoles get truecolor codes. The other way round, a truecolor console that renders after a standard one gets \x1b[91m:

 standard: '\x1b[91morange\x1b[0m'
truecolor: '\x1b[91morange\x1b[0m'

I expected each console to get codes for its own color system:

truecolor: '\x1b[38;2;255;136;0morange\x1b[0m'
 standard: '\x1b[91morange\x1b[0m'
      256: '\x1b[38;5;208morange\x1b[0m'

It shows up in practice when a program has more than one console (say, one for the terminal and one recording to a file with a different color system), or in tests that create consoles with different settings in one process.

I have a small fix that stores the color system next to the cached codes, and I'm happy to open a PR.

Python 3.13, macOS.

Dominant language
Python
Stars
57.8k
Forks
2.6k
Avg merge
20m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/rich

All issues in Textualize/rich

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.