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

Regression: Re-set instantiate to True when constant is True #771

Merged
merged 2 commits into from
Jun 22, 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
4 changes: 3 additions & 1 deletion param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,10 @@ def _set_instantiate(self,instantiate):
# having this code avoids needless instantiation.
if self.readonly:
self.instantiate = False
elif self.constant is True:
self.instantiate = True
elif instantiate is not Undefined:
self.instantiate = instantiate or self.constant # pylint: disable-msg=W0201
self.instantiate = instantiate
else:
# Default value
self.instantiate = self._slot_defaults['instantiate']
Expand Down
46 changes: 46 additions & 0 deletions tests/testparameterizedobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,52 @@ def test_constant_parameter(self):
testpo = TestPO()
self.assertEqual(testpo.const,9)

def test_parameter_constant_instantiate(self):
# instantiate is automatically set to True when constant=True
assert TestPO.param.const.instantiate is True

class C(param.Parameterized):
# instantiate takes precedence when True
a = param.Parameter(instantiate=True, constant=False)
b = param.Parameter(instantiate=False, constant=False)
c = param.Parameter(instantiate=False, constant=True)
d = param.Parameter(constant=True)
e = param.Parameter(constant=False)
f = param.Parameter()

assert C.param.a.constant is False
assert C.param.a.instantiate is True
assert C.param.b.constant is False
assert C.param.b.instantiate is False
assert C.param.c.constant is True
assert C.param.c.instantiate is True
assert C.param.d.constant is True
assert C.param.d.instantiate is True
assert C.param.e.constant is False
assert C.param.e.instantiate is False
assert C.param.f.constant is False
assert C.param.f.instantiate is False

def test_parameter_constant_instantiate_subclass(self):

obj = object()

class A(param.Parameterized):
x = param.Parameter(obj)

class B(param.Parameterized):
x = param.Parameter(constant=True)

assert A.param.x.constant is False
assert A.param.x.instantiate is False
assert B.param.x.constant is True
assert B.param.x.instantiate is True

a = A()
b = B()
assert a.x is obj
assert b.x is not obj

def test_readonly_parameter(self):
"""Test that you can't set a read-only parameter on construction or as an attribute."""
testpo = TestPO()
Expand Down