[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raise
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
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:
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).
- Dominant language
- Python
- Stars
- 57.8k
- Forks
- 2.6k
- Avg merge
- 20m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/rich
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Textualize/rich#4269 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4268 ·
-
Needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4267 ·
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Textualize/rich#4266 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4262 ·
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day