`SelectionList.toggle()` with a value that isn't in the list selects it and then raises `KeyError`
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/textual
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6790 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Textualize/textual#6789 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6787 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6782 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Textualize/textual#6778 · 1 comment ·
All issues in Textualize/textual
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day