[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raise
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 76/100
Línea de trabajo
Empieza por encontrar Text.right_crop() en el código fuente de Rich y leer sus actualizaciones de spans y de la longitud en caché junto con los ejemplos del issue. Comprueba si ya hay pruebas para cantidades cero, negativas, de longitud exacta y excesivas; después, añade o actualiza casos para el comportamiento esperado indicado. Se considera terminado cuando las cantidades no positivas dejan el texto sin cambios y las cantidades excesivas producen un texto vacío con len(text) == 0.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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).
- Lenguaje dominante
- Python
- Estrellas
- 57.8k
- Forks
- 2.6k
- Merge medio
- 20 min
- PR fusionados (30 d)
- 3
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Textualize/rich
-
Color.parse() raises raw ValueError instead of ColorParseError for an empty rgb(...) componentAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Textualize/rich#4269 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Textualize/rich#4268 ·
-
Needs triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Textualize/rich#4267 ·
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
Textualize/rich#4266 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Textualize/rich#4262 ·
Todos los issues de Textualize/rich
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
mohanraj00/entelos#200 ·
Los mantenedores suelen responder en 1 día
-
Dead url in mise.tomlAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 68/100
-
sweep
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
bketelsen/truenas-mcp#77 ·
Los mantenedores suelen responder en 1 día
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 86/100
sandbox-quantum/switch#2163 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
openedx/xblock-lti-consumer#765 ·
Los mantenedores suelen responder en 1 día