Skip to content

Commit

Permalink
ListSelector restricted to list type objects (#531)
Browse files Browse the repository at this point in the history
* limit ListSelector to list type objs

* Add tests
  • Loading branch information
maximlt authored Jan 16, 2023
1 parent e71f75e commit a9ec25b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,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'

0 comments on commit a9ec25b

Please sign in to comment.