`SelectionList.toggle()` with a value that isn't in the list selects it and then raises `KeyError`

オープン 初心者向け
#6,767 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
frontend

調査の方向性

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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Textualize/textual のほかの issue

Textualize/textual の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。