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

pn.cache(hash_funcs=custom_funcs, ...) are not applied recursively (ie. at all) #4139

Closed
tomascsantos opened this issue Nov 19, 2022 · 0 comments · Fixed by #4334
Closed
Milestone

Comments

@tomascsantos
Copy link

ALL software version info

panel==0.14.1

Description of expected behavior and the observed behavior

First, feedback: custom hashing functions is super hard to debug. I'm in deep. Better error messages would be nice bonus to this fix

Complete, minimal, self-contained example code that reproduces the issue

I ran the below code, and pressed the button kicking off the stack trace..

import panel as pn
from sqlalchemy import select, text

import ptvsd
ptvsd.enable_attach(address=('localhost', 5678))
print('Ready to attach the VS Code debugger')
ptvsd.wait_for_attach()  # Only include this line if you always want to attach the debugger

sqlalchemy_select = select(text('hello world'))

# pulling strait from the function that determines matches for custom hash funcs
select_type = pn.io.cache._get_fqn(sqlalchemy_select)
print(select_type)

hash_funcs = {
    select_type: lambda x: 'tmp'.encode(),
}


@pn.cache(hash_funcs=hash_funcs)
def callback(event):
    print('we hit database')
    return 'hello'


btn = pn.widgets.Button(name="test")
btn.on_click(callback)

pn.Column(btn).servable()

Stack traceback

When I click the button, we can see in the Locals view, that args=(<sqlalchemy....>,). Note that it is a tuple
image

Stepping into compute_hash... and down a few more...
Note that the fqn_type is builtins.tuple... ok fair enough.
image
So it selected function: _contianer_hash to take it apart.
(Also note we have hash_funcs={my custom funcs} still... but not for long...)
image
Now we have lost our custom hash_funcs :(
image

So I have to always match on tuple if I want anything to work. This seems like a bug to me.

Screenshots or screencasts of the bug in action

Finally the not-very-helpful error message:

Traceback (most recent call last):
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/server/protocol_handler.py", line 97, in handle
    work = await handler(message, connection)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/server/session.py", line 95, in _needs_document_lock_wrapper
    result = func(self, *args, **kwargs)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/server/session.py", line 289, in _handle_patch
    message.apply_to_document(self.document, self)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/protocol/messages/patch_doc.py", line 115, in apply_to_document
    invoke_with_curdoc(doc, lambda: doc.apply_json_patch(self.content, setter))
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 408, in invoke_with_curdoc
    return f()
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/protocol/messages/patch_doc.py", line 115, in <lambda>
    invoke_with_curdoc(doc, lambda: doc.apply_json_patch(self.content, setter))
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/document.py", line 391, in apply_json_patch
    DocumentPatchedEvent.handle_json(self, event_json, references, setter)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/events.py", line 259, in handle_json
    handler(doc, event_json, references, setter)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/events.py", line 300, in _handle_json
    cb(event_json["msg_data"])
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 355, in trigger_json_event
    model._trigger_event(event)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/util/callback_manager.py", line 119, in _trigger_event
    self.document.callbacks.notify_event(cast(Model, self), event, invoke)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 247, in notify_event
    invoke_with_curdoc(doc, callback_invoker)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 408, in invoke_with_curdoc
    return f()
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/util/callback_manager.py", line 115, in invoke
    cast(EventCallbackWithEvent, callback)(event)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/reactive.py", line 428, in _server_event
    self._comm_event(doc, event)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/reactive.py", line 415, in _comm_event
    state._handle_exception(e)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/io/state.py", line 399, in _handle_exception
    raise exception
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/reactive.py", line 413, in _comm_event
    self._process_bokeh_event(doc, event)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/reactive.py", line 350, in _process_bokeh_event
    self._process_event(event)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/widgets/button.py", line 184, in _process_event
    self.clicks += 1
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/param/parameterized.py", line 367, in _f
    instance_param.__set__(obj, val)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/param/parameterized.py", line 369, in _f
    return f(self, obj, val)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/param/__init__.py", line 625, in __set__
    super(Dynamic,self).__set__(obj,val)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/param/parameterized.py", line 369, in _f
    return f(self, obj, val)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/param/parameterized.py", line 1248, in __set__
    obj.param._call_watcher(watcher, event)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/param/parameterized.py", line 2039, in _call_watcher
    self_._execute_watcher(watcher, (event,))
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/param/parameterized.py", line 2021, in _execute_watcher
    watcher.fn(*args, **kwargs)
  File "/Users/tcastrosantos/Documents/dashboard-portal/dashboards/bms-developers/cycle-explorer/mvp.py", line 27, in callback
    return fetch_from_db(sqlalchemy_select)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/io/cache.py", line 337, in wrapped_func
    hash_value = compute_hash(func, hash_funcs, hash_args, hash_kwargs)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/io/cache.py", line 270, in compute_hash
    hasher.update(_generate_hash(args, hash_funcs))
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/io/cache.py", line 200, in _generate_hash
    hash_value = _generate_hash_inner(obj, hash_funcs)
  File "/opt/anaconda3/envs/holoviz/lib/python3.9/site-packages/panel/io/cache.py", line 177, in _generate_hash_inner
    raise ValueError(
ValueError: User hash function <function _container_hash at 0x7f9050cccee0> failed for input (<sqlalchemy.sql.selectable.Select object at 0x7f90517cc940>,) with following error: ValueError("Could not hash object of type function").
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 a pull request may close this issue.

2 participants