mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Raise a widget-specific exception when given a bad option
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
from ._selection_list import Selection
|
from ._selection_list import Selection, SelectionError
|
||||||
|
|
||||||
__all__ = ["Selection"]
|
__all__ = ["Selection", "SelectionError"]
|
||||||
|
|||||||
Reference in New Issue
Block a user