[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raise
まだ誰も着手していません。
評価
調査の方向性
まず Rich のソース内で Text.right_crop() を見つけ、issue の例とあわせて、span とキャッシュされた長さの更新処理を読みます。ゼロ、負の値、長さと同じ値、長さを超える値を対象とした既存のテストを確認し、示された期待動作に合わせてテストケースを追加または更新します。完了条件は、ゼロ以下の値ではテキストが変化せず、長さを超える値では len(text) == 0 となる空のテキストが生成されることです。
索引モデルが issue の本文から書いたものです。
説明
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).
- 主要言語
- Python
- スター
- 57.8k
- フォーク
- 2.6k
- 平均マージ
- 20分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Textualize/rich のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
Textualize/rich#4269 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Textualize/rich#4268 ·
-
Needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Textualize/rich#4267 ·
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
Textualize/rich#4266 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Textualize/rich#4262 ·
Textualize/rich の issue をすべて見る
似ている issue
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskオープン
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
sandbox-quantum/switch#2163 ·
メンテナーはふだん 1 日以内に返信
-
bug pydanty:is-working
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
pydantic/pydantic-ai#13939 ·
メンテナーはふだん 1 日以内に返信
-
priority: low python
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
LearningCircuit/local-deep-research#8849 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
francescopace/espectre#223 ·
メンテナーはふだん 3 日以内に返信
-
good first issue Status: Triaged
難易度 1/5 1時間未満 初心者へのやさしさ 84/100
canonical/imagecraft#538 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信