`Style` keeps the ANSI codes from the first color system it renders with
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/rich
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Textualize/rich#4269 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4268 ·
-
Needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4267 ·
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseOpen
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Textualize/rich#4265 ·
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day