Skip to content

[Bug] SelectionList raises OptionDoesNotExist when highlighted index is stale after filtering #6726

Open

Description

@hrshtmk

Description

SelectionList._on_option_list_option_highlighted can raise an unhandled OptionDoesNotExist when the option_index on the event no longer corresponds to an existing option, e.g. a stale index left over after the option list was filtered or changed.

Real-world impact

This is crashing archinstall's "Additional packages" menu, which uses a filterable SelectionList. If keystrokes arrive while the list is being filtered or the screen is transitioning, the highlight handler gets called with an index that no longer exists. OptionDoesNotExist is raised, and the fallout surfaces further up as a NoMatches exception (No nodes match 'SelectionList' on SelectListScreen()), which takes down the whole installer.

Downstream PR with the workaround: archlinux/archinstall#4780

Right now archinstall has to override the Textual handler in its own _SelectionList subclass to stay safe. A fix in Textual would let every app using SelectionList avoid this.

Steps to reproduce

  1. Create a SelectionList with a few options.
  2. Cause a highlight event with an index that is no longer valid (e.g.
    filter the options while a highlight is pending, or set highlighted
    to an out-of-range index).
  3. get_option_at_index raises OptionDoesNotExist inside the handler.

Minimal reproduction (included as a regression test on the branch below):

selection_list.highlighted = 999
event = OptionList.OptionHighlighted(selection_list, valid_option, 999)
selection_list._on_option_list_option_highlighted(event)  # raises

Expected behavior

A stale highlight event is ignored and no exception is raised.

Proposed fix

Bounds-check event.option_index in _on_option_list_option_highlighted and return early if the option doesn't exist. I have this ready with a regression test and a CHANGELOG entry:

Branch: https://github.com/hrshtmk/textual/tree/fix/selection-list-bounds
Commit: 939d4d6

I know PRs are limited to collaborators, so I'm filing this with the fix on my branch.
Happy to open a PR if a maintainer can enable that.

Activity

  1. github-actions commented on Oct 2, 2026

    @github-actions

    We found the following entry in the FAQ which you may find helpful:

    Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

    This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

    This is an automated reply, generated by FAQtory

  2. DawnofGenX commented on Oct 4, 2026

    @DawnofGenX

    I'd like to work on this.

    The bug is in SelectionList._on_option_list_option_highlighted — it can raise an unhandled OptionDoesNotExist when the option_index on the event no longer corresponds to an existing option.

    I'll add a regression test and fix the root cause.

  3. hrshtmk commented on Oct 5, 2026

    @hrshtmk
    Author

    I'd like to work on this.

    The bug is in SelectionList._on_option_list_option_highlighted — it can raise an unhandled OptionDoesNotExist when the option_index on the event no longer corresponds to an existing option.

    I'll add a regression test and fix the root cause.

    Write a better LLM script, my guy. The fix, test and changelog are already attached in my description if you can't see.

  4. Guurdeep commented on Oct 8, 2026

    @Guurdeep

    Hi! I've put together the fix along with a unit regression test and CHANGELOG entry on my branch:
    https://github.com/Guurdeep/textual/tree/fix/selection-list-bounds

    Summary of Changes:

    • src/textual/widgets/_selection_list.py: Added try...except OptionDoesNotExist: handling in _on_option_list_option_highlighted so stale highlight events (e.g. after filtering options or screen transitions) are ignored gracefully.
    • tests/selection_list/test_selection_messages.py: Added test_stale_highlight_event_ignored() regression test.
    • CHANGELOG.md: Added entry under [Unreleased].
      Since PR creation is restricted to repository collaborators, could a maintainer please enable PR access for this branch or pull the fix directly? Thanks
  5. added a commit that references this issue on Oct 8, 2026
    f4a2e37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions