Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing validation on slider #6982

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions panel/tests/widgets/test_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@
def test_editable_slider(document, comm,
editableslider, start, end, step, val1, val2, val3, diff1):

slider = editableslider(start=start, end=end, value=val1, name='Slider')

Check failure on line 456 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / core:test-core:ubuntu-latest

test_editable_slider[EditableIntSlider] ValueError: Value 4 not in bounds 0 to 1.

Check failure on line 456 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / unit:test-310:macos-latest

test_editable_slider[EditableIntSlider] ValueError: Value 4 not in bounds 0 to 1.

Check failure on line 456 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / unit:test-312:macos-latest

test_editable_slider[EditableIntSlider] ValueError: Value 4 not in bounds 0 to 1.

Check failure on line 456 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / unit:test-310:ubuntu-latest

test_editable_slider[EditableIntSlider] ValueError: Value 4 not in bounds 0 to 1.

Check failure on line 456 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / unit:test-312:ubuntu-latest

test_editable_slider[EditableIntSlider] ValueError: Value 4 not in bounds 0 to 1.

widget = slider.get_root(document, comm=comm)

Expand Down Expand Up @@ -628,7 +628,7 @@
value=val_init, name='Slider'
)
except Exception:
assert fail_init

Check failure on line 631 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / core:test-core:ubuntu-latest

test_editable_slider_bounds[1-5-0-None-2-3-False-False-EditableIntSlider] assert False

Check failure on line 631 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / unit:test-310:macos-latest

test_editable_slider_bounds[1-5-0-None-2-3-False-False-EditableFloatSlider] assert False

Check failure on line 631 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / unit:test-310:ubuntu-latest

test_editable_slider_bounds[1-5-0-None-2-3-False-False-EditableFloatSlider] assert False

Check failure on line 631 in panel/tests/widgets/test_slider.py

View workflow job for this annotation

GitHub Actions / unit:test-312:ubuntu-latest

test_editable_slider_bounds[1-5-0-None-2-3-False-False-EditableFloatSlider] assert False

try:
slider.value = val_update
Expand Down Expand Up @@ -774,3 +774,19 @@
slider = editableslider(fixed_end=fixed_end, end=end, step=step)
assert slider.start == end - step
assert slider.end == end

@pytest.mark.parametrize(
"slider_class,start,end,value",[
(FloatSlider, 0, 10, 11),
(IntSlider, 0, 10, 11),
(EditableFloatSlider, 0,10,11),
(EditableIntSlider, 0, 10, 11),
]
)
def test_cannot_set_continous_value_outside_bounds(slider_class, start, end, value):
with pytest.raises(ValueError):
slider_class(value=value, start=start, end=end)

slider = slider_class(start=start, end=end)
with pytest.raises(ValueError):
slider.value=value
12 changes: 12 additions & 0 deletions panel/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def _update_model(


class ContinuousSlider(_SliderBase):
"""The ContinuousSlider is an abstract base class for a slider widget with value, start, end
and step parameters.

It allows selecting a continuous value within a set of bounds using a slider.
"""

format = param.ClassSelector(class_=(str, TickFormatter,), doc="""
A custom format string or Bokeh TickFormatter.""")
Expand All @@ -116,8 +121,12 @@ class ContinuousSlider(_SliderBase):
def __init__(self, **params):
if 'value' not in params:
params['value'] = params.get('start', self.start)

super().__init__(**params)

self._validate_value()
self.param.watch(self._validate_value, ['value', 'start', 'end'])

def _get_embed_state(self, root, values=None, max_opts=3):
ref = root.ref['id']
w_model, parent = self._models[ref]
Expand Down Expand Up @@ -165,6 +174,9 @@ def _get_embed_state(self, root, values=None, max_opts=3):

return (dw, w_model, values, lambda x: x.value, 'value', 'cb_obj.value')

def _validate_value(self, *args):
if self.value and not self.start <= self.value <= self.end:
raise ValueError(f'Value {self.value} not in bounds {self.start} to {self.end}.')

class FloatSlider(ContinuousSlider):
"""
Expand Down
Loading