`Console.use_theme(theme, inherit=False)` still inherits the current styles

Aperta Adatta ai principianti
#4,259 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
88/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli

Direzione di ricerca

Il bug si trova in ThemeContext.enter, che chiama self.console.push_theme(self.theme) senza passare il flag inherit memorizzato; la correzione consiste nel passare inherit=self.inherit. Riproduci il problema con lo snippet nella issue, confrontando push_theme(theme, inherit=False) con use_theme(theme, inherit=False), e verifica che repr.number non risulti più evidenziato. Il lavoro è completo quando entrambi gli output coincidono e un test copre use_theme con inherit=False.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
57.8k
Fork
2.6k
Merge medio
20m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/rich

Tutte le issue di Textualize/rich

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.