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

Fix regression in ListSelector with check_on_set=False #874

Merged
merged 1 commit into from
Oct 10, 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
2 changes: 1 addition & 1 deletion param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def _instantiate_param_obj(paramobj, owner=None):
p.watchers = {}

# shallow-copy any mutable slot values other than the actual default
for s in p.__class__.__slots__:
for s in p.__class__._all_slots_:
v = getattr(p, s)
if _is_mutable_container(v) and s != "default":
setattr(p, s, copy.copy(v))
Expand Down
9 changes: 4 additions & 5 deletions param/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2071,18 +2071,17 @@ def compute_default(self):
self.objects.append(o)

def _validate(self, val):
if (val is None and self.allow_None):
return
self._validate_type(val)

if self.check_on_set:
self._validate_value(val)
else:
self._ensure_value_is_in_objects(val)

for v in val:
self._ensure_value_is_in_objects(v)

def _validate_type(self, val):
if (val is None and self.allow_None):
return

if not isinstance(val, list):
raise ValueError(
f"{_validate_error_prefix(self)} only takes list types, "
Expand Down
30 changes: 18 additions & 12 deletions tests/testlistselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class P(param.Parameterized):
h = param.ListSelector(default=None)
g = param.ListSelector(default=None,objects=[7,8])
i = param.ListSelector(default=[7],objects=[9],check_on_set=False)
j = param.ListSelector(objects=[11], check_on_set=False, allow_None=True)

self.P = P

Expand Down Expand Up @@ -84,12 +85,17 @@ def test_set_object_outside_bounds(self):
def test_set_object_setattr(self):
p = self.P(e=[6])
p.f = [9]
self.assertEqual(p.f, [9])
assert p.f == [9]
assert p.param.f.objects == [10, 9]
assert self.P.param.f.objects == [10]
p.g = [7]
self.assertEqual(p.g, [7])
assert p.g == [7]
assert p.param.g.objects == self.P.param.g.objects
assert p.param.g.objects == [7, 8]
p.i = [12]
self.assertEqual(p.i, [12])

assert p.i == [12]
assert p.param.i.objects == [9, 7, 12]
assert self.P.param.i.objects == [9, 7]

def test_set_object_not_None(self):
p = self.P(e=[6])
Expand All @@ -111,6 +117,14 @@ def test_set_one_object_not_None(self):
else:
raise AssertionError("Object set outside range.")

def test_set_one_object_allow_None_check_on_set(self):
p = self.P(e=[6])
p.j = None
assert p.j is None
assert p.param.j.objects == [11]
p.j = [12]
assert p.j == [12]
assert p.param.j.objects == [11, 12]

def test_set_object_setattr_post_error(self):
p = self.P(e=[6])
Expand Down Expand Up @@ -147,11 +161,6 @@ class Q(param.Parameterized):
else:
raise AssertionError("ListSelector created without range.")


##################################################################
##################################################################
### new tests (not copied from testobjectselector)

def test_bad_default(self):
with pytest.raises(
ValueError,
Expand All @@ -172,7 +181,6 @@ def test_default_not_checked(self):
class Q(param.Parameterized):
r = param.ListSelector(default=[6])

##########################
def test_default_not_checked_to_be_iterable(self):
with pytest.raises(
ValueError,
Expand All @@ -187,8 +195,6 @@ class Q(param.Parameterized):

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


def test_compute_default(self):
class Q(param.Parameterized):
Expand Down