diff --git a/param/parameterized.py b/param/parameterized.py index d1bb8de03..88836575c 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -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'] diff --git a/tests/testparameterizedobject.py b/tests/testparameterizedobject.py index 98c60b7eb..e5477001e 100644 --- a/tests/testparameterizedobject.py +++ b/tests/testparameterizedobject.py @@ -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()