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
hash_funcs for st.cache_data and st.cache_resource #6502
Conversation
This looks great from product perspective. I noted a couple of questions / possible concerns here but they are fairly minor: |
If you think this is actually a Streamlit bug, please [file a bug report here.] | ||
(https://github.com/streamlit/streamlit/issues/new/choose) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't render as a link, it just shows the raw markdown. Not sure if it's a limitation in st.exception or because of the line break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thank you @sfc-gh-jcarroll !
Removing line break works :)
Only last question from me is whether there's any low lift thing to make this easier to use for cached methods in a class. class MyCustomClass:
def __init__(self, initial_score: int):
self.my_score = initial_score
@st.cache_data(hash_funcs={"__main__.MyCustomClass": id})
def multiply_score(self, x: int) -> int:
return self.my_score * x It would be nice at least if This was a common pain point in many of the forum threads (not sure how it was solved before with st.cache), so if it's difficult to discover / use I think we will continue to get feedback about it. Otherwise the behavior all looks good from product side based on my testing of the whl file ✅ |
my_obj = MyObj() | ||
user_hash_error_func(my_obj) | ||
|
||
expected_message = """unsupported operand type(s) for +=: 'MyObj' and 'int' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote the same tests cases for st.cache_data
and st.cache_resource
and did not use parameterized common cache tests because expected_message
are very verbose and different, so it would look ugly if parametrized.
@sfc-gh-jcarroll related to method decoration and For example in the case of a function that uses an argument of type For module names starting with
import streamlit as st
import hello_world
x = hello_world.MyHelloWorld()
st.write(x.my_method())
st.write(x.my_method()) and import streamlit as st
class MyHelloWorld:
@st.cache_data(hash_funcs={"hello_world.MyHelloWorld": id})
def my_method(self):
print("IN METHOD")
return 45 |
Sounds good. That works for this initial update and we can decide later whether we need to improve it. Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
def pretty_print(self): | ||
def to_str(v): | ||
try: | ||
return "Object of type %s: %s" % (type_util.get_fqn_type(v), str(v)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we use an f-string here, instead of %
?
@@ -84,6 +180,15 @@ def pop(self): | |||
def __contains__(self, val: Any): | |||
return id(val) in self._stack | |||
|
|||
def pretty_print(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs return type annotation
@parameterized.expand( | ||
[("cache_data", cache_data), ("cache_resource", cache_resource)] | ||
) | ||
def test_recursive_hash_func(self, _, cache_decorator): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a short docstring explaining what this test is doing?
@@ -391,6 +413,72 @@ def test_generator_not_hashable(self): | |||
with self.assertRaises(UnhashableTypeError): | |||
get_hash((x for x in range(1))) | |||
|
|||
def test_hash_funcs_acceptable_keys(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also use a docstring, because the purpose of the test isn't very clear from its name. (Maybe the comment in the middle of the function is sufficient?)
* develop: Bump dependencies of component-lib (streamlit#6830) Make `streamlit config show` honor newlines (streamlit#6758) Update comments on magic.py (streamlit#6833) Add tests for component-lib (streamlit#6580) Fix button height (streamlit#6738) Add `to_pandas` method to convert inputs to pandas DataFrames (streamlit#6668) hash_funcs for st.cache_data and st.cache_resource (streamlit#6502)
hash_funcs parameter existed previously in st.cache decorator, in this PR we bring back hash_funcs functionality to new cache primitives st.cache_data and st.cache_resource mostly using behavior from st.cache.
hash_funcs parameter existed previously in st.cache decorator, in this PR we bring back hash_funcs functionality to new cache primitives st.cache_data and st.cache_resource mostly using behavior from st.cache.
hash_funcs parameter existed previously in st.cache decorator, in this PR we bring back hash_funcs functionality to new cache primitives st.cache_data and st.cache_resource mostly using behavior from st.cache.
📚 Context
hash_funcs
parameter existed previously inst.cache
decorator, and we had multiple requests to return it to new cache_primitives: #6295 , #5939 , #6290In this PR we return back
hash_funcs
functionality mostly using behaviour fromst.cache
.What kind of change does this PR introduce?
🧠 Description of Changes
Add bullet points summarizing your changes here
Revised:
Insert screenshot of your updated UI/code here
Current:
Insert screenshot of existing UI/code here
🧪 Testing Done
🌐 References
Does this depend on other work, documents, or tickets?
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.