Skip to content

Commit

Permalink
Force instance parameter creation when setting up watchers before ini…
Browse files Browse the repository at this point in the history
…tialization
  • Loading branch information
philippjfr committed Sep 15, 2023
1 parent 18839ee commit b20cd95
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,13 @@ def _instantiate_param_obj(paramobj, owner=None):
return p


def _instantiated_parameter(parameterized, param):
def _instantiated_parameter(parameterized, param, force=False):
"""
Given a Parameterized object and one of its class Parameter objects,
return the appropriate Parameter object for this instance, instantiating
it if need be.
"""
if (getattr(parameterized._param__private, 'initialized', False) and param.per_instance and
if ((getattr(parameterized._param__private, 'initialized', False) or force) and param.per_instance and
not getattr(type(parameterized)._param__private, 'disable_instance_params', False)):
key = param.name

Expand Down Expand Up @@ -2693,15 +2693,21 @@ def _register_watcher(self_, action, watcher, what='value'):
raise ValueError("{} parameter was not found in list of "
"parameters of class {}".format(parameter_name, self_.cls.__name__))

if self_.self is not None and what == "value":
watchers = self_.self._param__private.watchers
inst = self_.self
if inst is not None and what == "value":
watchers = inst._param__private.watchers
if parameter_name not in watchers:
watchers[parameter_name] = {}
if what not in watchers[parameter_name]:
watchers[parameter_name][what] = []
getattr(watchers[parameter_name][what], action)(watcher)
else:
watchers = self_[parameter_name].watchers
# If watcher is registered before instance is set up
# we must force instance parameter creation
param_obj = self_[parameter_name]
if action == 'append' and inst is not None and not inst._param__private.initialized:
param_obj = _instantiated_parameter(inst, param_obj, force=True)
watchers = param_obj.watchers
if what not in watchers:
watchers[what] = []
getattr(watchers[what], action)(watcher)
Expand Down

0 comments on commit b20cd95

Please sign in to comment.