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

Fixed List validation #494

Merged
merged 3 commits into from
Jun 27, 2021
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
18 changes: 17 additions & 1 deletion param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,9 @@ class Composite(Parameter):
in the order specified. Likewise, setting the parameter takes a
sequence of values and sets the value of the constituent
attributes.

This Parameter type has not been tested with watchers and
dependencies, and may not support them properly.
"""

__slots__ = ['attribs', 'objtype']
Expand Down Expand Up @@ -1361,8 +1364,12 @@ class List(Parameter):
Parameter whose value is a list of objects, usually of a specified type.

The bounds allow a minimum and/or maximum length of
list to be enforced. If the class is non-None, all
list to be enforced. If the item_type is non-None, all
items in the list are checked to be of that type.

`class_` is accepted as an alias for `item_type`, but is
deprecated due to conflict with how the `class_` slot is
used in Selector classes.
"""

__slots__ = ['bounds', 'item_type', 'class_']
Expand All @@ -1376,6 +1383,15 @@ def __init__(self, default=[], class_=None, item_type=None,
**params)
self._validate(default)

def _validate(self, val):
"""
Checks that the value is numeric and that it is within the hard
bounds; if not, an exception is raised.
"""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops; left a useless docstring in from where I copied this. I'll delete in a later PR.

self._validate_value(val, self.allow_None)
self._validate_bounds(val, self.bounds)
self._validate_item_type(val, self.item_type)

def _validate_bounds(self, val, bounds):
"Checks that the list is of the right length and has the right contents."
if bounds is None:
Expand Down
58 changes: 58 additions & 0 deletions tests/API1/testlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import param
from . import API1TestCase
# TODO: I copied the tests from testobjectselector, although I
# struggled to understand some of them. Both files should be reviewed
# and cleaned up together.

# TODO: tests copied from testobjectselector could use assertRaises
# context manager (and could be updated in testobjectselector too).

class TestListParameters(API1TestCase):

def setUp(self):
super(TestListParameters, self).setUp()
class P(param.Parameterized):
e = param.List([5,6,7], item_type=int)
l = param.List(["red","green","blue"], item_type=str, bounds=(0,10))

self.P = P

def test_default_None(self):
class Q(param.Parameterized):
r = param.List(default=[]) # Also check None)

def test_set_object_constructor(self):
p = self.P(e=[6])
self.assertEqual(p.e, [6])

def test_set_object_outside_bounds(self):
p = self.P()
try:
p.l=[6]*11
except ValueError:
pass
else:
raise AssertionError("Object set outside range.")

def test_set_object_wrong_type(self):
p = self.P()
try:
p.e=['s']
except TypeError:
pass
else:
raise AssertionError("Object allowed of wrong type.")

def test_set_object_not_None(self):
p = self.P(e=[6])
try:
p.e = None
except ValueError:
pass
else:
raise AssertionError("Object set outside range.")


if __name__ == "__main__":
import nose
nose.runmodule()