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 that ignored allow_None set in a selector #791

Merged
merged 1 commit into from
Jul 12, 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: 2 additions & 0 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,8 @@ def __init__(self, *, objects=Undefined, default=Undefined, instantiate=Undefine
# Required as Parameter sets allow_None=True if default is None
if allow_None is Undefined:
self.allow_None = self._slot_defaults['allow_None']
else:
self.allow_None = allow_None
if self.default is not None and self.check_on_set is True:
self._validate(self.default)

Expand Down
46 changes: 46 additions & 0 deletions tests/testobjectselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from collections import OrderedDict

import param
import pytest

from .utils import check_defaults

Expand Down Expand Up @@ -104,6 +105,51 @@ def test_unbound_allow_None_not_dynamic(self):

assert s.allow_None is None

def test_allow_None_set_and_behavior_class(self):
class P(param.Parameterized):
a = param.ObjectSelector(objects=dict(a=1), allow_None=True)
b = param.ObjectSelector(objects=dict(a=1), allow_None=False)
c = param.ObjectSelector(default=1, objects=dict(a=1), allow_None=True)
d = param.ObjectSelector(default=1, objects=dict(a=1), allow_None=False)

assert P.param.a.allow_None is True
assert P.param.b.allow_None is False
assert P.param.c.allow_None is True
assert P.param.d.allow_None is False

P.a = None
assert P.a is None
with pytest.raises(ValueError):
P.b = None
P.c = None
assert P.c is None
with pytest.raises(ValueError):
P.d = None

def test_allow_None_set_and_behavior_instance(self):
class P(param.Parameterized):
a = param.ObjectSelector(objects=dict(a=1), allow_None=True)
b = param.ObjectSelector(objects=dict(a=1), allow_None=False)
c = param.ObjectSelector(default=1, objects=dict(a=1), allow_None=True)
d = param.ObjectSelector(default=1, objects=dict(a=1), allow_None=False)

p = P()

assert p.param.a.allow_None is True
assert p.param.b.allow_None is False
assert p.param.c.allow_None is True
assert p.param.d.allow_None is False

p.a = None
assert p.a is None
with pytest.raises(ValueError):
p.b = None
p.c = None
assert p.c is None
with pytest.raises(ValueError):
p.d = None


def test_set_object_constructor(self):
p = self.P(e=6)
self.assertEqual(p.e, 6)
Expand Down
47 changes: 46 additions & 1 deletion tests/testselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from collections import OrderedDict

import param
import pytest

from.utils import check_defaults
from .utils import check_defaults

opts=dict(A=[1,2],B=[3,4],C=dict(a=1,b=2))

Expand Down Expand Up @@ -103,6 +104,50 @@ def test_allow_None_is_None(self):
assert p.param.s.allow_None is None
assert p.param.d.allow_None is None

def test_allow_None_set_and_behavior_class(self):
class P(param.Parameterized):
a = param.Selector(objects=dict(a=1), allow_None=True)
b = param.Selector(objects=dict(a=1), allow_None=False)
c = param.Selector(default=1, objects=dict(a=1), allow_None=True)
d = param.Selector(default=1, objects=dict(a=1), allow_None=False)

assert P.param.a.allow_None is True
assert P.param.b.allow_None is False
assert P.param.c.allow_None is True
assert P.param.d.allow_None is False

P.a = None
assert P.a is None
with pytest.raises(ValueError):
P.b = None
P.c = None
assert P.c is None
with pytest.raises(ValueError):
P.d = None

def test_allow_None_set_and_behavior_instance(self):
class P(param.Parameterized):
a = param.Selector(objects=dict(a=1), allow_None=True)
b = param.Selector(objects=dict(a=1), allow_None=False)
c = param.Selector(default=1, objects=dict(a=1), allow_None=True)
d = param.Selector(default=1, objects=dict(a=1), allow_None=False)

p = P()

assert p.param.a.allow_None is True
assert p.param.b.allow_None is False
assert p.param.c.allow_None is True
assert p.param.d.allow_None is False

p.a = None
assert p.a is None
with pytest.raises(ValueError):
p.b = None
p.c = None
assert p.c is None
with pytest.raises(ValueError):
p.d = None

def test_autodefault(self):
class P(param.Parameterized):
o1 = param.Selector(objects=[6, 7])
Expand Down