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

Ensure operations that are unsafe during instance initialization raise errors #834

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,14 @@ def objects(self_, instance=True):
instance parameters to be returned by setting
instance='existing'.
"""
if self_.self is not None and not self_.self._param__private.initialized and instance is True:
raise RuntimeError(
'Cannot look up instance Parameter objects before the instance '
'has been fully initialized. Ensure you have called super().__init__() '
maximlt marked this conversation as resolved.
Show resolved Hide resolved
'in the instance constructor before trying to access instance '
'parameter objects.'
)

cls = self_.cls
# We cache the parameters because this method is called often,
# and parameters are rarely added (and cannot be deleted)
Expand Down Expand Up @@ -2251,6 +2259,13 @@ def trigger(self_, *param_names):
changed for a Parameter of type Event, setting it to True so
that it is clear which Event parameter has been triggered.
"""
if self_.self is not None and not self_.self._param__private.initialized:
raise RuntimeError(
'Triggering watchers on a partially initialized instance '
'is not supported. Ensure you have called super().__init__() in '
maximlt marked this conversation as resolved.
Show resolved Hide resolved
'the instance constructor before trying to set up a watcher.'
)

trigger_params = [p for p in self_.self_or_cls.param
if hasattr(self_.self_or_cls.param[p], '_autotrigger_value')]
triggers = {p:self_.self_or_cls.param[p]._autotrigger_value
Expand Down Expand Up @@ -2694,6 +2709,13 @@ def _spec_to_obj(self_, spec, dynamic=True, intermediate=True):
return deps, dynamic_deps

def _register_watcher(self_, action, watcher, what='value'):
if self_.self is not None and not self_.self._param__private.initialized:
raise RuntimeError(
'(Un)registering a watcher on a partially initialized instance '
'is not supported. Ensure you have called super().__init__() in '
'the instance constructor before trying to set up a watcher.'
)

parameter_names = watcher.parameter_names
for parameter_name in parameter_names:
if parameter_name not in self_.cls.param:
Expand Down
Loading