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

Raise ValueError when a watcher cannot be unwatched instead of logging a warning #1018

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -3568,12 +3568,17 @@ def _register_watcher(self_, action, watcher, what='value'):
watchers[parameter_name] = {}
if what not in watchers[parameter_name]:
watchers[parameter_name][what] = []
getattr(watchers[parameter_name][what], action)(watcher)
method = getattr(watchers[parameter_name][what], action)
else:
watchers = self_[parameter_name].watchers
if what not in watchers:
watchers[what] = []
getattr(watchers[what], action)(watcher)
method = getattr(watchers[what], action)
try:
method(watcher)
except ValueError:
# raised when method is 'remove' and watcher is not in the list.
raise ValueError(f"Watcher '{watcher}' has already been removed or was never added.")

def watch(
self_,
Expand Down Expand Up @@ -3724,10 +3729,7 @@ def unwatch(self_, watcher: Watcher) -> None:

>>> instance.a = 20 # No output
"""
try:
self_._register_watcher('remove', watcher, what=watcher.what)
except Exception:
self_.warning(f'No such watcher {str(watcher)} to remove.')
self_._register_watcher('remove', watcher, what=watcher.what)

def watch_values(
self_,
Expand Down
19 changes: 10 additions & 9 deletions tests/testwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from param.parameterized import Skip, discard_events

from .utils import MockLoggingHandler, warnings_as_excepts
from .utils import MockLoggingHandler


class Accumulator:
Expand Down Expand Up @@ -250,15 +250,8 @@ def accumulator(change):
obj = SimpleWatchExample()
watcher = obj.param.watch(accumulator, 'a')
obj.param.unwatch(watcher)
with warnings_as_excepts(match='No such watcher'):
with pytest.raises(ValueError, match="has already been removed or was never added"):
obj.param.unwatch(watcher)
try:
param.parameterized.warnings_as_exceptions = False
obj.param.unwatch(watcher)
self.log_handler.assertEndsWith('WARNING',
' to remove.')
finally:
param.parameterized.warnings_as_exceptions = True

def test_simple_batched_watch_setattr(self):

Expand Down Expand Up @@ -720,6 +713,14 @@ def __init__(self, **params):
P()


def test_watch_raises_bad_parameter(self):
obj = SimpleWatchExample()
with pytest.raises(
ValueError,
match="does_not_exist parameter was not found in list of parameters of class SimpleWatchExample"
):
obj.param.watch(lambda e: print(e), 'does_not_exist')


class TestWatchMethod(unittest.TestCase):

Expand Down
Loading