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

unwatch no longer logs a warning and idempotent behavior clarified #1018

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

maximlt
Copy link
Member

@maximlt maximlt commented Feb 11, 2025

Resolves #1003

By raising ValueError when trying to remove an already removed/never registered watcher.

Copy link

codecov bot commented Feb 11, 2025

Codecov Report

Attention: Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Project coverage is 87.04%. Comparing base (274632a) to head (016b675).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
param/parameterized.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1018      +/-   ##
==========================================
- Coverage   87.27%   87.04%   -0.23%     
==========================================
  Files           9        9              
  Lines        4936     4941       +5     
==========================================
- Hits         4308     4301       -7     
- Misses        628      640      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maximlt
Copy link
Member Author

maximlt commented Feb 11, 2025

@jlstevens I believe you're the last person who refactored unwatch, do you mind reviewing this change :) ?

@jlstevens
Copy link
Contributor

After thinking about this, I think raising a ValueError at this stage is too abrupt a change. Instead I propose the following:

  1. Improve the warning to use the official warnings module instead of self_.warning
  2. If we agree the ValueError is a better approach, update this warning to tell users that in future the warning will become an exception.
  3. Change it to a ValueError.

I believe the original intent was to make unwatching idempotent: if you ask for something to be unwatched and it exits, it gets unwatched. If it is not being watched already, then there is nothing to do.

I see arguments either way and I don't mind this behavior being deprecated, but not this suddenly.

@maximlt maximlt changed the title Raise ValueError when a watcher cannot be unwatched instead of logging a warning unwatch no longer logs a warning and idempotent behavior clarified Feb 21, 2025
@maximlt
Copy link
Member Author

maximlt commented Feb 21, 2025

Thanks for the review @jlstevens, beneficial insights. After giving it some more thought, I decided it was best to stay closer to the current implementation that aims to be idempotent. I say "aim" as it is not truly idempotent imo as it logs a warning when the watcher has already been removed. I agree there are situations in an app, even more so when it's async based, where unwatching can happen multiple times or in a non-specific order. unwatch being idempotent makes it so users don't have to wrap all their unwatch calls in a try/except.

FWIW what also pushed me to change things was that the try/except was just catching Exception which can swallow the wrong error if there's a bug in the code.

With 016b675:

  • unwatch no longer raises an error or logs a warning; internally in _register_watcher a ValueError is caught and swallowed when a 'remove' action fails
  • the docs were updated to declare this behavior (I preferred using no-op instead of idempotent)

@jlstevens could you please review again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unwatch should not log a warning when it fails
2 participants