Skip to content

Commit

Permalink
Validate type of param.Boolean default value (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
minimav authored Mar 27, 2023
1 parent f338d19 commit 7ece88c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ class Boolean(Parameter):
def __init__(self, default=False, bounds=(0,1), **params):
self.bounds = bounds
super(Boolean, self).__init__(default=default, **params)
self._validate(default)

def _validate_value(self, val, allow_None):
if allow_None:
Expand All @@ -1007,8 +1008,11 @@ def _validate_value(self, val, allow_None):
"Boolean value or None, not %s."
% (self.name, val))
elif not isinstance(val, bool):
raise ValueError("Boolean parameter %r must be True or False, "
"not %s." % (self.name, val))
name = "" if self.name is None else " %r" % self.name
raise ValueError("Boolean parameter%s must be True or False, not %s." % (name, val))

def _validate(self, val):
self._validate_value(val, self.allow_None)



Expand Down
7 changes: 7 additions & 0 deletions tests/API1/testbooleanparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def test_bad_type(self):
with self.assertRaisesRegex(ValueError, msg):
p.e = 'test'

def test_bad_default_type(self):
msg = r"Boolean parameter must be True or False, not test."

with self.assertRaisesRegex(ValueError, msg):
class A(param.Parameterized):
b = param.Boolean(default='test')


class TestEventParameters(API1TestCase):

Expand Down

0 comments on commit 7ece88c

Please sign in to comment.