`SelectionList.toggle()` with a value that isn't in the list selects it and then raises `KeyError`
まだ誰も着手していません。
評価
調査の方向性
src/textual/widgets/_selection_list.py の _toggle() から始め、次に _select()、_deselect()、および SelectionToggled の送信に使われる _values の参照を読みます。_make_selection() を読むと、_values が設定される前に _select() が実行される理由がわかります。Issue のスニペットで再現し、未知の値に対する toggle() は、変更やメッセージ送信を行う前に何もしないようにします。一方で select()/_make_selection() の経路はそのまま維持します。既存の選択リストのテストを tests/ で実行し、リグレッションがないことを確認します。KeyError が発生せず、SelectedChanged が送られず、selected が変わらなければ完了です。
索引モデルが issue の本文から書いたものです。
説明
Hi! I hit this while writing generated tests against Textual's widgets.
Calling SelectionList.toggle() with a value that isn't one of the list's options changes the selection, posts SelectedChanged, and only then raises KeyError. The value stays in selected, and deselect_all() doesn't remove it, since that only walks the options.
import asyncio
from textual.app import App, ComposeResult
from textual.widgets import SelectionList
class ToggleApp(App):
def compose(self) -> ComposeResult:
yield SelectionList(("One", 1), ("Two", 2))
def on_selection_list_selected_changed(self, event: SelectionList.SelectedChanged) -> None:
print("SelectedChanged, selected =", event.selection_list.selected)
async def main() -> None:
app = ToggleApp()
async with app.run_test() as pilot:
selection_list = app.query_one(SelectionList)
try:
selection_list.toggle(3) # 3 is not a value in the list
except Exception as error:
print("toggle(3) raised", type(error).__name__, error)
await pilot.pause()
print("selected after toggle(3):", selection_list.selected)
selection_list.deselect_all()
selection_list.select(4) # also not in the list
await pilot.pause()
print("selected after select(4):", selection_list.selected)
asyncio.run(main())
Output (the same on 8.2.7 and on main at 5e5b7ef):
toggle(3) raised KeyError 3
SelectedChanged, selected = [3]
selected after toggle(3): [3]
SelectedChanged, selected = [3, 4]
selected after select(4): [3, 4]
_toggle() calls _select() (or _deselect()) and then looks the value up in _values to post SelectionToggled, which raises for an unknown value after the change has been made.
I'd expect toggle() of an unknown value to change nothing, the way deselect() of an unknown value does. select() of an unknown value also adds it to selected without complaint (last two lines). Is that intended? _make_selection() relies on _select() before _values is filled in, so I've left select() alone in the PR.
Textual 8.2.7 and main (5e5b7ef), Python 3.13, macOS.
- 主要言語
- Python
- スター
- 37.7k
- フォーク
- 1.5k
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Textualize/textual のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Textualize/textual#6790 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
Textualize/textual#6789 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Textualize/textual#6787 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Textualize/textual#6782 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
Textualize/textual#6778 · コメント 1 件 ·
Textualize/textual の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
mohanraj00/entelos#200 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
-
sweep
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
bketelsen/truenas-mcp#77 ·
メンテナーはふだん 1 日以内に返信
-
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 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
openedx/xblock-lti-consumer#765 ·
メンテナーはふだん 1 日以内に返信