I want to override hashing functions of @st.cache
for all sub classes of a specific base class.
Though we can now change hash_funcs
of st.cache
, it seems to affect only the specified types/classes in the keys, but not their subtypes.
Are there any ways to do that?
For example, in the sample code below, some_func
decorated with @st.cache
properly caches an instance of Base
class, but not Sub
class.
import streamlit as st
import random
class Base:
id = 42
foo = "foo"
class Sub(Base):
pass
@st.cache(hash_funcs={Base: lambda o: o.id})
def some_func(obj):
return random.randint(0, 1000)
base = Base()
first = some_func(base)
base.foo = "foo" # Only .foo attr is changed. .id attr, which is used as for hashing, is not changed.
second = some_func(base)
cached = first == second
print("Is an instance of Base class cached: ", cached)
# Do the same thing on Sub class
sub = Sub()
first = some_func(sub)
sub.foo = "bar"
second = some_func(sub)
cached = first == second
print("Is an instance of Sub class cached: ", cached)
The result of this sample would be
Is an instance of Base class cached: True
Is an instance of Sub class cached: False