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

Validate type of param.Boolean default value #722

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 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)

jbednar marked this conversation as resolved.
Show resolved Hide resolved
def _validate_value(self, val, allow_None):
if allow_None:
Expand All @@ -1007,8 +1008,15 @@ 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))
if self.name is not None:
raise ValueError("Boolean parameter %r must be True or False, "
"not %s." % (self.name, val))
else:
raise ValueError("Boolean parameter must be True or False, "
"not %s." % val)

jbednar marked this conversation as resolved.
Show resolved Hide resolved
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