{"record":{"id":"eb5262d167b2f5e3","repo":"Textualize/textual","slug":"there-is-no-option-with-an-index-of-index","errorCode":null,"errorMessage":"There is no option with an index of {index}","messagePattern":"There is no option with an index of (.+?)","errorType":"exception","errorClass":"OptionDoesNotExist","httpStatus":null,"severity":"error","filePath":"src/textual/widgets/_option_list.py","lineNumber":478,"sourceCode":"        option = self.get_option(option_id)\n        return self._option_to_index[option]\n\n    def get_option_at_index(self, index: int) -> Option:\n        \"\"\"Get the option at the given index.\n\n        Args:\n            index: The index of the option to get.\n\n        Returns:\n            The option at that index.\n\n        Raises:\n            OptionDoesNotExist: If there is no option with the given index.\n        \"\"\"\n        try:\n            return self._options[index]\n        except IndexError:\n            raise OptionDoesNotExist(\n                f\"There is no option with an index of {index}\"\n            ) from None\n\n    def _set_option_disabled(self, index: int, disabled: bool) -> Self:\n        \"\"\"Set the disabled state of an option in the list.\n\n        Args:\n            index: The index of the option to set the disabled state of.\n            disabled: The disabled state to set.\n\n        Returns:\n            The `OptionList` instance.\n        \"\"\"\n        self._options[index].disabled = disabled\n        if index == self.highlighted:\n            self.highlighted = _widget_navigation.find_next_enabled(\n                self._options, anchor=index, direction=1\n            )","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/widgets/_option_list.py#L460-L496","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\noption = option_list.get_option_at_index(idx)\n# after\nif 0 <= idx < option_list.option_count:\n    option = option_list.get_option_at_index(idx)","handlingStrategy":"validation","validationCode":"if 0 <= index < option_list.option_count:\n    option = option_list.get_option_at_index(index)","typeGuard":"def valid_index(ol, i): return -ol.option_count <= i < ol.option_count","tryCatchPattern":null,"preventionTips":["Always compare against option_count","Recompute indexes after removals","Prefer id-based access over positional"],"tags":["optionlist","index-out-of-range","textual"],"backgroundTag":"index-out-of-bounds","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}