`str()` of a style from `Style.update_link()` leaves out the new link once the original has been converted to a string

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

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/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 rich/style.py: update_link() copia _style_definition nel nuovo Style, e __str__ restituisce quel valore in cache quando è impostato. Riproduci il problema con lo snippet presente nell'issue, poi verifica che la copia parta senza una definizione in cache, come fa _add. Il lavoro è completato quando str(style.update_link(...)) riflette il nuovo link dopo che l'originale è stato convertito in stringa, e un test copre sia l'impostazione sia la rimozione del link.

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

Descrizione

Hi! I hit this while writing generated tests against Rich. Style.update_link() returns a style with a different link, but str() of it gives the original style's definition if the original was converted to a string first:

from rich.style import Style

style = Style(bold=True)
print("before str():", str(style.update_link("http://x")))
str(style)  # caches the definition on the original
print("after str():", str(style.update_link("http://x")))

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

before str(): bold link http://x
after str(): bold

update_link copies self._style_definition into the new style, and __str__ returns that cached string when it's set, so the copy reports the original's definition. The same happens the other way round: update_link(None) on a linked style that has been converted to a string still shows link ....

I think the copy should start with no cached definition, as _add does. I have a one-line fix with a test and I'm 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.