Textualize/textual · error · OptionDoesNotExist
There is no option with an index of {index}
Error message
There is no option with an index of {index} What it means
Raised by OptionList.get_option_at_index when the integer index is out of range for the internal _options list (negative-beyond-end or >= len(options)). The IndexError from list access is re-raised as OptionDoesNotExist.
Source
Thrown at src/textual/widgets/_option_list.py:478
option = self.get_option(option_id)
return self._option_to_index[option]
def get_option_at_index(self, index: int) -> Option:
"""Get the option at the given index.
Args:
index: The index of the option to get.
Returns:
The option at that index.
Raises:
OptionDoesNotExist: If there is no option with the given index.
"""
try:
return self._options[index]
except IndexError:
raise OptionDoesNotExist(
f"There is no option with an index of {index}"
) from None
def _set_option_disabled(self, index: int, disabled: bool) -> Self:
"""Set the disabled state of an option in the list.
Args:
index: The index of the option to set the disabled state of.
disabled: The disabled state to set.
Returns:
The `OptionList` instance.
"""
self._options[index].disabled = disabled
if index == self.highlighted:
self.highlighted = _widget_navigation.find_next_enabled(
self._options, anchor=index, direction=1
)View on GitHub (pinned to 06dbeef4bb)
Solutions
- Bounds-check against option_list.option_count before calling.
- Refresh any stored index after remove_option/set_options operations.
- Use negative indexing awareness: Python allows -1..-n; only out-of-range values raise.
Example fix
# before
option = option_list.get_option_at_index(idx)
# after
if 0 <= idx < option_list.option_count:
option = option_list.get_option_at_index(idx) Defensive patterns
Strategy: validation
Validate before calling
if 0 <= index < option_list.option_count:
option = option_list.get_option_at_index(index) Type guard
def valid_index(ol, i): return -ol.option_count <= i < ol.option_count
Prevention
- Always compare against option_count
- Recompute indexes after removals
- Prefer id-based access over positional
When it happens
Trigger: Calling get_option_at_index(i) where i >= number of options, or a negative index below -len(options); commonly hit from highlight/selection code like _get_visual_from_index after options are removed.
Common situations: Iterating with a stale count after options were removed (indexes shift), using highlighted index after clearing the list, or off-by-one loops over option_count.
Related errors
- Unable to remove; there is no option at index {index}
- Row index {row_index!r} is not valid.
- Column index {column_index!r} is not valid.
- Unable to add {option!r} due to duplicate ID
- There is no option with an ID of {option_id!r}
AI-assisted analysis of Textualize/textual@06dbeef4bb (2026-08-27).
Data as JSON: /api/errors/eb5262d167b2f5e3.
Report an issue: GitHub.