Skip to content

Commit

Permalink
Move error handling for non-existing options to dedicated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nekeal committed Jul 22, 2023
1 parent ae6ed80 commit 10c8b5f
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/textual/widgets/_option_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,16 +678,8 @@ def replace_option_prompt(self, option_id: str, prompt: RenderableType) -> Self:
Returns:
The `OptionList` instance.
Raises:
OptionDoesNotExist: If no option has the given ID.
"""
try:
self._replace_option_prompt(self._option_ids[option_id], prompt)
except KeyError:
raise OptionDoesNotExist(
f"There is no option with an ID of '{option_id}'"
) from None
self._replace_option_prompt(self.get_option_index(option_id), prompt)
return self

def replace_option_prompt_at_index(
Expand All @@ -705,12 +697,7 @@ def replace_option_prompt_at_index(
Raises:
OptionDoesNotExist: If there is no option with the given index.
"""
try:
self._replace_option_prompt(index, prompt)
except IndexError:
raise OptionDoesNotExist(
f"There is no option with an index of {index}"
) from None
self._replace_option_prompt(index, prompt)
return self

def clear_options(self) -> Self:
Expand Down Expand Up @@ -855,6 +842,22 @@ def get_option(self, option_id: str) -> Option:
f"There is no option with an ID of '{option_id}'"
) from None

def get_option_index(self, option_id):
"""Get the index of the option with the given ID.
Args:
option_id: The ID of the option to get the index of.
Raises:
OptionDoesNotExist: If no option has the given ID.
"""
try:
return self._option_ids[option_id]
except:
raise OptionDoesNotExist(
f"There is no option with an ID of '{option_id}'"
) from None

def render_line(self, y: int) -> Strip:
"""Render a single line in the option list.
Expand Down

0 comments on commit 10c8b5f

Please sign in to comment.