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

Materialize default values based on all the parameters available #798

Merged
merged 2 commits into from
Jul 26, 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
18 changes: 5 additions & 13 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,19 +1837,13 @@ def _setup_params(self_,**params):
"""
self = self_.param.self
## Deepcopy all 'instantiate=True' parameters
# (building a set of names first to avoid redundantly
# instantiating a later-overridden parent class's parameter)
params_to_deepcopy = {}
params_to_ref = {}
for class_ in classlist(type(self)):
if not issubclass(class_, Parameterized):
continue
for (k, v) in class_.param._parameters.items():
# (avoid replacing name with the default of None)
if v.instantiate and k != "name":
params_to_deepcopy[k] = v
elif v.constant and k != 'name':
params_to_ref[k] = v
for pname, p in self.param.objects(instance=False).items():
if p.instantiate and pname != "name":
params_to_deepcopy[pname] = p
elif p.constant and pname != 'name':
params_to_ref[pname] = p

for p in params_to_deepcopy.values():
self.param._instantiate_param(p)
Expand Down Expand Up @@ -3064,8 +3058,6 @@ def __init__(mcs, name, bases, dict_):
parameters = [(n, o) for (n, o) in dict_.items()
if isinstance(o, Parameter)]

mcs._param__parameters._parameters = dict(parameters)
philippjfr marked this conversation as resolved.
Show resolved Hide resolved

for param_name,param in parameters:
mcs._initialize_parameter(param_name, param)

Expand Down
19 changes: 19 additions & 0 deletions tests/testparameterizedobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,25 @@ class B(A):
assert b.param.p.constant is False


def test_inheritance_set_Parameter_instantiate_constant_before_instantation():
# https://github.com/holoviz/param/issues/760
class A(param.Parameterized):
p0 = param.Parameter()
p1 = param.Parameter(instantiate=True)
p2 = param.Parameter(constant=True)

class B(A):
pass

B.p0 = B.p1 = B.p2 = 2

b = B()

assert b.p0 == 2
assert b.p1 == 2
assert b.p2 == 2


def test_inheritance_allow_None_behavior():
class A(param.Parameterized):
p = param.Parameter(default=1)
Expand Down