[BUG] Text.right_crop with a negative amount corrupts text length

Abierto Apto para principiantes
#4,256 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
66/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

Empieza por Text.right_crop() y sus pruebas en el repositorio Rich; el issue no indica ningún archivo ni prueba, así que localízalos buscando right_crop. Comprueba cómo deben tratarse las cantidades negativas y compara el texto y la longitud almacenada en caché antes y después de recortar. La tarea estará terminada cuando una cantidad negativa no pueda dejar el texto y su longitud o sus spans en un estado incoherente, y una prueba de regresión cubra ese comportamiento.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

  • I've checked docs and searched open and closed issues for possible solutions.
  • I can't find my issue in the FAQ.

Describe the bug

Text.right_crop() behaves unexpectedly for a negative amount: it removes characters from the start of the text while increasing the cached length, leaving the Text object internally inconsistent.

Minimal reproduction:

from rich.text import Text

text = Text(abc)
text.right_crop(-1)
print(repr(text.plain), len(text))

Observed output:

'a' 4

A value of -4 leaves plain == abc but reports len(text) == 7. The implementation calculates self.plain[:-amount] and subtracts amount from the cached length, so negative values both slice from the wrong end and increase the cached length. Subsequent operations that rely on the cached length or span offsets can then behave incorrectly.

Expected behavior: a negative crop amount should not silently mutate the text or invalidate its length/spans. Rejecting negative values with ValueError would make the invalid input explicit; please advise if a different contract is preferred.

This report and reproduction were prepared with GitHub Copilot assistance; the example was run locally against the current checkout.

Platform

Click to expand

Windows, PowerShell. The behavior is in Text's internal string/length bookkeeping and does not depend on terminal rendering.

Lenguaje dominante
Python
Estrellas
57.8k
Forks
2.6k
Merge medio
20 min
PR fusionados (30 d)
3

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Textualize/rich

Todos los issues de Textualize/rich

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.