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

ListSelector restricted to list type objects #531

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,9 @@ def compute_default(self):
def _validate(self, val):
if (val is None and self.allow_None):
return
if not isinstance(val, list):
raise ValueError("ListSelector parameter %r only takes list "
"types, not %r." % (self.name, val))
for o in val:
super(ListSelector, self)._validate(o)

Expand Down
8 changes: 4 additions & 4 deletions tests/API0/testlistselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_set_object_not_None(self):
p.g = [7]
try:
p.g = None
except TypeError:
except ValueError:
pass
else:
raise AssertionError("Object set outside range.")
Expand Down Expand Up @@ -110,12 +110,12 @@ class Q(param.Parameterized):
### new tests (not copied from testobjectselector)

def test_bad_default(self):
with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
class Q(param.Parameterized):
r = param.ListSelector(default=6,check_on_set=True)

def test_implied_check_on_set(self):
with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
class Q(param.Parameterized):
r = param.ListSelector(default=7,objects=[7,8])

Expand All @@ -135,7 +135,7 @@ def test_set_checked_to_be_iterable(self):
class Q(param.Parameterized):
r = param.ListSelector(default=6,check_on_set=False)

with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
Q.r = 6
##########################

Expand Down
21 changes: 17 additions & 4 deletions tests/API1/testlistselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_set_object_not_None(self):
p.g = [7]
try:
p.g = None
except TypeError:
except ValueError:
pass
else:
raise AssertionError("Object set outside range.")
Expand Down Expand Up @@ -109,12 +109,12 @@ class Q(param.Parameterized):
### new tests (not copied from testobjectselector)

def test_bad_default(self):
with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
class Q(param.Parameterized):
r = param.ListSelector(default=6,check_on_set=True)

def test_implied_check_on_set(self):
with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
class Q(param.Parameterized):
r = param.ListSelector(default=7,objects=[7,8])

Expand All @@ -134,7 +134,7 @@ def test_set_checked_to_be_iterable(self):
class Q(param.Parameterized):
r = param.ListSelector(default=6,check_on_set=False)

with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
Q.r = 6
##########################

Expand All @@ -154,3 +154,16 @@ class Q(param.Parameterized):

with self.assertRaises(TypeError):
Q.param.params('r').compute_default()

def test_initialization_bad_iterable(self):
with self.assertRaises(ValueError):
class Q(param.Parameterized):
j = param.ListSelector('ab', ['a', 'b', 'c', 'd'])

def test_set_bad_iterable(self):
class Q(param.Parameterized):
r = param.ListSelector(objects=['a', 'b', 'c', 'd'])

q = Q()
with self.assertRaises(ValueError):
q.r = 'ab'