`Console.use_theme(theme, inherit=False)` still inherits the current styles
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 88/100
Research direction
The bug is in ThemeContext.enter, which calls self.console.push_theme(self.theme) without passing the stored inherit flag; the fix is to pass inherit=self.inherit. Reproduce with the snippet in the issue, comparing push_theme(theme, inherit=False) against use_theme(theme, inherit=False) and checking that repr.number is no longer highlighted. Done when both outputs match and a test covers use_theme with inherit=False.
Written by the indexing model from the issue text.
Description
Hi! I hit this while testing themes. use_theme takes inherit and passes it to ThemeContext, which stores it but then calls self.console.push_theme(self.theme) without it, so the pushed theme always inherits.
import io
from rich.console import Console
from rich.theme import Theme
# A theme that doesn't include the default styles.
theme = Theme({"warning": "red"}, inherit=False)
def render(console):
console.print("[warning]warning[/] 42", end="")
out = console.file.getvalue()
console.file.truncate(0)
console.file.seek(0)
return repr(out)
console = Console(file=io.StringIO(), force_terminal=True, color_system="truecolor")
console.push_theme(theme, inherit=False)
print("push_theme(theme, inherit=False):", render(console))
console.pop_theme()
with console.use_theme(theme, inherit=False):
print("use_theme(theme, inherit=False): ", render(console))
Output on Rich 15.0.0 (and main at 3a109cc):
push_theme(theme, inherit=False): '\x1b[31mwarning\x1b[0m 42'
use_theme(theme, inherit=False): '\x1b[31mwarning\x1b[0m \x1b[1;36m42\x1b[0m'
push_theme drops repr.number, as expected, but use_theme keeps it and the number is still highlighted. I expected both lines to match.
The fix is one line in ThemeContext.__enter__: self.console.push_theme(self.theme, inherit=self.inherit). 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