Skip to content

Commit

Permalink
Raise an error when setting None to a base Parameter with `allow_No…
Browse files Browse the repository at this point in the history
…ne=False` (#691)
  • Loading branch information
maximlt authored Mar 12, 2023
1 parent 6d91ba2 commit 6c995be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,8 @@ def __set__(self, obj, val):

def _validate_value(self, value, allow_None):
"""Implements validation for parameter value"""
if value is None and allow_None is False:
raise ValueError("Parameter %r does not accept `None`." % (self.name))

def _validate(self, val):
"""Implements validation for the parameter value and attributes"""
Expand Down
24 changes: 24 additions & 0 deletions tests/API1/testparameterizedobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,30 @@ class TestPO1(param.Parameterized):
x = param.Number(default=numbergen.UniformRandom(lbound=-1,ubound=1,seed=1),bounds=(-1,1))
y = param.Number(default=1,bounds=(-1,1))


class TestParameter(API1TestCase):

def setUp(self):
super(TestParameter, self).setUp()

class _TestParameter(param.Parameterized):
a = param.Parameter(default='')
b = param.Parameter(default='',allow_None=True)
c = param.Parameter(default=None)

self._TestParameter = _TestParameter

def test_handling_of_None(self):
t = self._TestParameter()

with self.assertRaises(ValueError):
t.a = None

t.b = None

assert t.c is None


class TestNumberParameter(API1TestCase):

def test_outside_bounds(self):
Expand Down

0 comments on commit 6c995be

Please sign in to comment.