[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raise

Open Beginner friendly
#4,265 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start by finding Text.right_crop() in the Rich source and read its span and cached length updates alongside the issue's examples. Check for existing tests covering zero, negative, exact-length, and oversized amounts, then add or update cases for the stated expected behavior. Done means non-positive amounts leave the text unchanged and oversized amounts produce empty text with len(text) == 0.

Written by the indexing model from the issue text.

Description

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:

  1. amount == 0 wipes the text. max_offset = len(self.plain) - 0 keeps every span, but self._text = [self.plain[:-amount]] is self.plain[:0] == "", so the text becomes empty while _length stays at 3 and the spans still cover 0..3. Any code that crops by a computed amount can therefore lose the line instead of leaving it alone.
  2. amount > len(text) makes _length negative (3 - 10), so the next len(text) raises ValueError: __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).

Dominant language
Python
Stars
57.8k
Forks
2.6k
Avg merge
20m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/rich

All issues in Textualize/rich

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.