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 1 commit
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
7 changes: 7 additions & 0 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,15 @@ class Boolean(Parameter):
# no effect since values are either False, True, or None (if allowed).
def __init__(self, default=False, bounds=(0,1), **params):
self.bounds = bounds
self._validate_default(default)
super(Boolean, self).__init__(default=default, **params)

def _validate_default(self, default):
if not isinstance(default, bool) and default is not None:
raise ValueError("Boolean parameter only takes a "
"Boolean default or None, not %s."
% default)

jbednar marked this conversation as resolved.
Show resolved Hide resolved
def _validate_value(self, val, allow_None):
if allow_None:
if not isinstance(val, bool) and val is not None:
Expand Down
6 changes: 6 additions & 0 deletions tests/API1/testbooleanparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def test_bad_type(self):
with self.assertRaisesRegex(ValueError, msg):
p.e = 'test'

def test_bad_default_type(self):
msg = r"Boolean parameter only takes a Boolean default or None, not test"

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


class TestEventParameters(API1TestCase):

Expand Down