[BUG] right_crop(0) clears Text but leaves its length unchanged
- [x] I checked the Rich text documentation and searched open and closed issues for this behavior.
- [x] I could not find it in the FAQ.
Describe the bug
Text.right_crop(0) removes all text even though zero characters were requested, and the cached length stays at its old value. Text.remove_suffix("") reaches the same path because every string ends with the empty suffix.
from rich.text import Text
text = Text("abc")
text.right_crop(0)
print(repr(text.plain), len(text)) # "", 3
suffixed = Text("abc")
suffixed.remove_suffix("")
print(repr(suffixed.plain), len(suffixed)) # "", 3
As a control, right_crop(1) returns plain text "ab" and length 2. The zero case uses self.plain[:-amount], so [:-0] becomes an empty string while _length -= 0 retains 3.
Expected behavior
Cropping zero characters and removing an empty suffix should leave "abc" and length 3 unchanged. In all cases, len(text) should match the text.
Platform
Windows 11, CPython 3.14.8, Rich 15.0.0 from main commit 9d8f9a372cc5916fd4781fec207ced7ddac2f08f. No terminal rendering is involved.
Reproduced with the official Rich 15.0.0 wheel on Python 3.12/Linux. Its rich/text.py matches the file at 9d8f9a3 (Git blob 7e087a4e). An independent synthetic check also found a downstream effect: after right_crop(0) or remove_suffix(""), appending "Z" leaves plain == "Z" but len(text) == 4, with the appended style offset at 3 rather than 0. Cropping one character, removing "c", and removing a non-matching suffix behaved normally.
Would a narrowly scoped amount == 0 early return in Text.right_crop, with regression tests preserving text, length, spans and subsequent append/join behavior, be an acceptable solution? This would leave negative-amount behavior unchanged rather than silently defining a new contract for it. No implementation or PR yet; asking first under the AI policy.
Investigation and this comment were prepared with OpenAI Codex assistance. These are synthetic API checks, not a production incident or a full-suite result.
Thanks for the report and the suggested scope.
I reproduced the bug on the current main and confirmed the root cause: right_crop(0) computes self.plain[:-0] (empty string) while _length -= 0 leaves the cached length unchanged, so len(text) diverges from len(plain). The same path is hit by remove_suffix("").
Proposed fix (narrowly scoped, leaves negative-amount behavior unchanged):
def right_crop(self, amount: int = 1) -> None:
"""Remove a number of characters from the end of the text."""
if amount == 0:
return
max_offset = len(self.plain) - amount
...
This is an early return for the zero case only. Verified locally:
Text('abc').right_crop(0)-> plain'abc', len 3 (unchanged).Text('abc').remove_suffix('')-> plain'abc', len 3 (unchanged).right_crop(1)still yields'ab', len 2.- Subsequent
append('Z')after a zero crop yields'abcZ', len 4 (no stale length/style offset). - Full
tests/test_text.py: 113 passed.
This comment and the investigation were prepared with AI assistance. Per the repo's AI policy, I'm not opening a PR until a maintainer approves the approach. Happy to open a PR with the repo's PR template and regression tests once approved.
@willmcgugan Would an early return for amount == 0 be acceptable, or would you prefer a different guard (e.g. clamping amount)?
I investigated this issue and implemented the proposed early return in "Text.right_crop()":
if amount == 0: return
I also added regression tests for "right_crop(0)" and "remove_suffix("")", including checks that the text length and spans remain unchanged.
My local validation is complete:
- Full test suite: 986 passed, 23 skipped
- Mypy: no issues found in 100 source files
- Black: all 167 files unchanged
- Coverage: 96%
- "git diff --check": clean
I understand that other contributors have proposed the same approach. I'd appreciate confirmation from @willmcgugan that this early-return solution is acceptable before I proceed with submitting a contribution.
For transparency, I used OpenAI ChatGPT (GPT-6) for assistance during investigation and review. I reviewed the changes and ran the checks locally.
Hi @willmcgugan, I reproduced this too: right_crop(0) clears the text because [:-0] is an empty slice, while the cached length stays unchanged, and remove_suffix("") hits the same path. Would an early return when amount == 0, leaving the text and length untouched, be an acceptable fix? Happy to open a PR with regression tests once you confirm this direction.