Skip to content

Commit 76086ef

Browse files
authored
set instantiate to True when constant is True (#771)
1 parent 4700b7c commit 76086ef

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

param/parameterized.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,8 +1240,10 @@ def _set_instantiate(self,instantiate):
12401240
# having this code avoids needless instantiation.
12411241
if self.readonly:
12421242
self.instantiate = False
1243+
elif self.constant is True:
1244+
self.instantiate = True
12431245
elif instantiate is not Undefined:
1244-
self.instantiate = instantiate or self.constant # pylint: disable-msg=W0201
1246+
self.instantiate = instantiate
12451247
else:
12461248
# Default value
12471249
self.instantiate = self._slot_defaults['instantiate']

tests/testparameterizedobject.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,52 @@ def test_constant_parameter(self):
274274
testpo = TestPO()
275275
self.assertEqual(testpo.const,9)
276276

277+
def test_parameter_constant_instantiate(self):
278+
# instantiate is automatically set to True when constant=True
279+
assert TestPO.param.const.instantiate is True
280+
281+
class C(param.Parameterized):
282+
# instantiate takes precedence when True
283+
a = param.Parameter(instantiate=True, constant=False)
284+
b = param.Parameter(instantiate=False, constant=False)
285+
c = param.Parameter(instantiate=False, constant=True)
286+
d = param.Parameter(constant=True)
287+
e = param.Parameter(constant=False)
288+
f = param.Parameter()
289+
290+
assert C.param.a.constant is False
291+
assert C.param.a.instantiate is True
292+
assert C.param.b.constant is False
293+
assert C.param.b.instantiate is False
294+
assert C.param.c.constant is True
295+
assert C.param.c.instantiate is True
296+
assert C.param.d.constant is True
297+
assert C.param.d.instantiate is True
298+
assert C.param.e.constant is False
299+
assert C.param.e.instantiate is False
300+
assert C.param.f.constant is False
301+
assert C.param.f.instantiate is False
302+
303+
def test_parameter_constant_instantiate_subclass(self):
304+
305+
obj = object()
306+
307+
class A(param.Parameterized):
308+
x = param.Parameter(obj)
309+
310+
class B(param.Parameterized):
311+
x = param.Parameter(constant=True)
312+
313+
assert A.param.x.constant is False
314+
assert A.param.x.instantiate is False
315+
assert B.param.x.constant is True
316+
assert B.param.x.instantiate is True
317+
318+
a = A()
319+
b = B()
320+
assert a.x is obj
321+
assert b.x is not obj
322+
277323
def test_readonly_parameter(self):
278324
"""Test that you can't set a read-only parameter on construction or as an attribute."""
279325
testpo = TestPO()

0 commit comments

Comments
 (0)