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

Open Beginner friendly
#6,767 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend

Research direction

Start in src/textual/widgets/_selection_list.py at _toggle(), then read _select(), _deselect() and the _values lookup used to post SelectionToggled; _make_selection() explains why _select() runs before _values is filled. Reproduce with the snippet in the issue, then make toggle() of an unknown value a no-op before any mutation or message is posted, keeping the select()/_make_selection() path intact. Run the existing selection-list tests under tests/ to confirm nothing regressed; done means no KeyError, no SelectedChanged, and selected unchanged.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
37.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.