Skip to content

fix: Add missing unwatch() method to Callback class - #8453

Merged
philippjfr merged 2 commits into
holoviz:mainfrom
SuMayaBee:fix-missing-unwatch-method-for-callback
Mar 2, 2026
Merged

fix: Add missing unwatch() method to Callback class#8453
philippjfr merged 2 commits into
holoviz:mainfrom
SuMayaBee:fix-missing-unwatch-method-for-callback

Conversation

@SuMayaBee

Copy link
Copy Markdown
Contributor

What's the issue?

While I picked up the issue #7440, the docs for js_on_click say the returned Callback "can be used to disable the callback", so I went ahead and verified that in the docs, then tested it myself:

import panel as pn

button = pn.widgets.Button(name="Open")
cb = button.js_on_click(args=dict(url="google.com"), code="window.open(url, '_blank');")
cb.unwatch()  # AttributeError: 'Callback' object has no attribute 'unwatch'

Confirmed, so the issue persists. The docs doesn't match with the code's output itself.

Root Cause

I went into panel/links.py where the Callback class lives. Here's what I found:

  • When a Callback is created via js_on_click, it registers itself into a WeakKeyDictionary called registry — keyed by the source widget.
  • _process_callbacks later walks through this registry to attach the JS callbacks to the Bokeh model on render.
  • The subclass Link (which extends Callback) already had an unlink() method that simply removes itself from the registry.
  • But the base Callback class? Nothing. No unwatch(), no unregister(), no way to undo it at all.

Fix

Added unwatch() to the Callback class, mirroring Link.unlink():

def unwatch(self) -> None:
    source = self.source
    if source is None:
        return
    callbacks = self.registry.get(source, [])
    if self in callbacks:
        callbacks.remove(self)

Notes

  • Only prevents the callback from being applied to future renders, consistent with Link.unlink() behaviour.

Closes #7440.

AI Disclosure: I used Gemini 3 Flash to explore and understand the codebase while working on this fix.

@codecov

codecov Bot commented Mar 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.01%. Comparing base (49872f3) to head (2026b19).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
panel/links.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8453      +/-   ##
==========================================
- Coverage   86.12%   86.01%   -0.11%     
==========================================
  Files         349      349              
  Lines       54971    54996      +25     
==========================================
- Hits        47343    47307      -36     
- Misses       7628     7689      +61     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@SuMayaBee SuMayaBee changed the title Fix: Add missing unwatch() method to Callback class fix: Add missing unwatch() method to Callback class Mar 2, 2026
@philippjfr
philippjfr merged commit 622bc8d into holoviz:main Mar 2, 2026
19 of 21 checks passed
@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing unwatch method for callbacks

2 participants