Raise a widget-specific exception when given a bad option

This commit is contained in:
Dave Pearson
2023-05-22 13:21:25 +01:00
parent 1d925da551
commit dae0cd7c02
2 changed files with 10 additions and 4 deletions

View File

@@ -22,6 +22,10 @@ MessageSelectionType = TypeVar("MessageSelectionType")
"""The type for the value of a `SelectionList` message""" """The type for the value of a `SelectionList` message"""
class SelectionError(TypeError):
"""Type of an error raised if a selection is badly-formed."""
class Selection(Generic[SelectionType], Option): class Selection(Generic[SelectionType], Option):
"""A selection for the `SelectionList`.""" """A selection for the `SelectionList`."""
@@ -200,6 +204,9 @@ class SelectionList(Generic[SelectionType], OptionList):
Returns: Returns:
An instance of a `Selection`. An instance of a `Selection`.
Raises:
SelectionError: If the selection was badly-formed.
""" """
if len(selection) == 3: if len(selection) == 3:
label, value, selected = cast( label, value, selected = cast(
@@ -210,8 +217,7 @@ class SelectionList(Generic[SelectionType], OptionList):
"tuple[TextType, SelectionType, bool]", (*selection, False) "tuple[TextType, SelectionType, bool]", (*selection, False)
) )
else: else:
# TODO: Proper error. raise SelectionError(f"Expected 2 or 3 values, got {len(selection)}")
raise TypeError("Wrong number of values for a selection.")
if selected: if selected:
self._selected[value] = None self._selected[value] = None
return Selection(label, value) return Selection(label, value)

View File

@@ -1,3 +1,3 @@
from ._selection_list import Selection from ._selection_list import Selection, SelectionError
__all__ = ["Selection"] __all__ = ["Selection", "SelectionError"]