[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raise
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 76/100
Direzione di ricerca
Inizia trovando Text.right_crop() nel codice sorgente di Rich e leggendo i suoi aggiornamenti degli span e della lunghezza memorizzata nella cache insieme agli esempi dell’issue. Verifica se esistono già test per quantità pari a zero, negative, esattamente pari alla lunghezza e superiori alla lunghezza; poi aggiungi o aggiorna i casi per il comportamento atteso indicato. Il lavoro è completato quando le quantità non positive lasciano il testo invariato e quelle superiori alla lunghezza producono un testo vuoto con len(text) == 0.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Rich main (3a109cc), Python 3.13.
Text.right_crop() is documented as "Remove a number of characters from the end of the text", but amounts outside 1..len(text) leave the Text object inconsistent:
from rich.text import Text
def probe(amount):
text = Text("abc")
try:
text.right_crop(amount)
return repr(text.plain), len(text)
except Exception as error:
return type(error).__name__ + ": " + str(error)
for amount in (1, 0, -1, 3, 10):
print(amount, probe(amount))
1 ('ab', 2)
0 ('', 3)
-1 ('a', 4)
3 ('', 0)
10 ValueError: __len__() should return >= 0
Two separate problems:
amount == 0wipes the text.max_offset = len(self.plain) - 0keeps every span, butself._text = [self.plain[:-amount]]isself.plain[:0]=="", so the text becomes empty while_lengthstays at 3 and the spans still cover0..3. Any code that crops by a computed amount can therefore lose the line instead of leaving it alone.amount > len(text)makes_lengthnegative (3 - 10), so the nextlen(text)raisesValueError: __len__() should return >= 0.
(The negative-amount case, where right_crop(-1) leaves len(text) == 4, is #4256; the same clamp covers it.)
Expected: a non-positive amount is a no-op, and an amount larger than the text removes the whole text (plain == "", len(text) == 0), so the cached length and spans stay consistent.
Pull requests to this repository are restricted to collaborators, so I can't send the change directly. The fix is small:
def right_crop(self, amount: int = 1) -> None:
"""Remove a number of characters from the end of the text."""
if amount <= 0:
return
amount = min(amount, len(self.plain))
max_offset = len(self.plain) - amount
...
With that, the table above becomes 1 ('ab', 2), 0 ('abc', 3), -1 ('abc', 3), 3 ('', 0), 10 ('', 0).
- Lingua principale
- Python
- Stelle
- 57.8k
- Fork
- 2.6k
- Merge medio
- 20m
- PR unite (30g)
- 3
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Textualize/rich
-
Color.parse() raises raw ValueError instead of ColorParseError for an empty rgb(...) componentAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
Textualize/rich#4269 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Textualize/rich#4268 ·
-
Needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Textualize/rich#4267 ·
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
Textualize/rich#4266 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Textualize/rich#4262 ·
Tutte le issue di Textualize/rich
Issue simili
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 86/100
sandbox-quantum/switch#2163 ·
I maintainer di solito rispondono entro 1 giorno
-
bug pydanty:is-working
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
pydantic/pydantic-ai#13939 ·
I maintainer di solito rispondono entro 1 giorno
-
priority: low python
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
LearningCircuit/local-deep-research#8849 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 86/100
francescopace/espectre#223 ·
I maintainer di solito rispondono entro 3 giorni
-
good first issue Status: Triaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 84/100
canonical/imagecraft#538 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni