Skip to content

Commit

Permalink
Fix bug with small default range in SpinBox (#397)
Browse files Browse the repository at this point in the history
* fix bug with small default range in SpinBox

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix bug in test

* fix settting range (int conversion)

* fix typing

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Talley Lambert <talley.lambert@gmail.com>
  • Loading branch information
3 people committed May 2, 2022
1 parent 71665cb commit 876190a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
6 changes: 6 additions & 0 deletions magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ def __init__(self):
def _mgui_set_value(self, value) -> None:
super()._mgui_set_value(int(value))

def _pre_set_hook(self, value):
return int(value)


class FloatSpinBox(QBaseRangedWidget):
def __init__(self):
Expand Down Expand Up @@ -500,6 +503,9 @@ def _mgui_set_tracking(self, value: bool) -> None:
if hasattr(self._qwidget, "setTracking"):
self._qwidget.setTracking(value)

def _pre_set_hook(self, value):
return int(value)


class Slider(_Slider):
_qwidget: QtW.QSlider
Expand Down
33 changes: 25 additions & 8 deletions magicgui/widgets/_bases/ranged_widget.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import builtins
from abc import ABC, abstractmethod
from typing import Tuple
from math import ceil, log10
from typing import Tuple, Union, cast
from warnings import warn

from magicgui.widgets import _protocols

from .value_widget import UNSET, ValueWidget
from .value_widget import UNSET, ValueWidget, _Unset


class RangedWidget(ValueWidget):
"""Widget with a contstrained value. Wraps RangedWidgetProtocol.
"""Widget with a constrained value. Wraps RangedWidgetProtocol.
Parameters
----------
min : float, optional
The minimum allowable value, by default 0
The minimum allowable value, by default 0 (or `value` if `value` is less than 0)
max : float, optional
The maximum allowable value, by default 1000
The maximum allowable value, by default 1000 (or `value` if `value` is greater
than 1000)
step : float, optional
The step size for incrementing the value, by default 1
"""

_widget: _protocols.RangedWidgetProtocol

def __init__(self, min: float = 0, max: float = 1000, step: float = 1, **kwargs):
def __init__(
self,
min: Union[float, _Unset] = UNSET,
max: Union[float, _Unset] = UNSET,
step: float = 1,
**kwargs,
): # sourcery skip: avoid-builtin-shadow
for key in ("maximum", "minimum"):
if key in kwargs:
warn(
Expand All @@ -38,9 +47,17 @@ def __init__(self, min: float = 0, max: float = 1000, step: float = 1, **kwargs)
val = kwargs.pop("value", UNSET)
super().__init__(**kwargs)

tmp_val = float(val if val not in (UNSET, None) else 1)

self.step = step
self.min = min
self.max = max
self.min: float = (
cast(float, min) if min is not UNSET else builtins.min(0, tmp_val)
)
self.max: float = (
cast(float, max)
if max is not UNSET
else builtins.max(1000, 10 ** ceil(log10(builtins.max(1, tmp_val + 1)))) - 1
)
if val not in (UNSET, None):
self.value = val

Expand Down
25 changes: 24 additions & 1 deletion tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_basic_widget_attributes():

assert repr(widget) == "SpinBox(value=1, annotation=None, name='my_name')"
assert widget.options == {
"max": 1000,
"max": 999,
"min": 0,
"step": 1,
"enabled": False,
Expand Down Expand Up @@ -558,6 +558,29 @@ def f(x: int = None):
assert rw.value == 0


@pytest.mark.parametrize(
"value,maksimum", [(10, 999), (None, 999), (1000, 9999), (1500, 9999)]
)
def test_range_big_value(value, maksimum):
rw = widgets.SpinBox(value=value)
rw.value == value
rw.max = maksimum


def test_range_negative_value():
rw = widgets.SpinBox(value=-10)
rw.value == -10
rw.min == -10


def test_exception_range_out_of_range():
with pytest.raises(ValueError):
widgets.SpinBox(value=10000, max=1000)

with pytest.raises(ValueError):
widgets.SpinBox(value=-10, min=0)


def test_containers_show_nested_containers():
"""make sure showing a container shows a nested FunctionGui."""

Expand Down

0 comments on commit 876190a

Please sign in to comment.