holoviz / param

Param: Make your Python code clearer and more reliable by declaring Parameters
https://param.holoviz.org
BSD 3-Clause "New" or "Revised" License
433 stars 74 forks source link

Number's `crop_to_bounds` does not respect exclusive bounds #80

Open ceball opened 9 years ago

ceball commented 9 years ago

Create a number with bounds (0,1] and verify that Number respects the bounds:

>>> import param
>>> class Q(param.Parameterized):
...     n = param.Number(default=0.5, bounds=(0,1), inclusive_bounds=(False,True))
...
>>> q = Q()
>>> q.n=0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "param\__init__.py", line 563, in __set__
    if not callable(val): self._check_value(val)
  File "param\__init__.py", line 671, in _check_value
    self._checkBounds(val)
  File "param\__init__.py", line 643, in _checkBounds
    raise ValueError("Parameter '%s' must be greater than %s"%(self._attrib_name,vmin))
ValueError: Parameter 'n' must be greater than 0
>>> q.n=1

But Number's crop_to_bounds (and hence set_in_bounds) does not respect exclusive bounds:

>>> q.params()['n'].crop_to_bounds(0)
0
>>> q.params()['n'].crop_to_bounds(1)
1
ceball commented 9 years ago

Note: see https://github.com/ioam/topographica/blob/master/topo/tkgui/projectionpanel.py#L140 (UnitsPanel) for an example where someone has had to hack around this problem.

jbednar commented 4 years ago

I guess we'd need to implement this for Number using the floating-point epsilon value subtracted from the exclusive upper bound, and then reimplement it for Integer by subtracting 1 from the exclusive upper bound.