Skip to content

Commit

Permalink
set instantiate to True when constant is True (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Jun 22, 2023
1 parent 4700b7c commit 76086ef
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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

0 comments on commit 76086ef

Please sign in to comment.