UnhashableTypeError: Cannot hash object of type _json.Scanner...
See original GitHub issueSummary
When attempting to use st.cache decorator (with streamlit 1.10.0) on a json data loader method, streamlit returns an unhashable error for a _json.Scanner. This behavior does not exist in the previous version (1.9.2).
Steps to reproduce
- Update streamlit to version 1.10.0
- Use st.cache on any method that loads data from a json file
For example:
@st.cache(allow_output_mutation=True)
def fetch_data_json(filename: str) -> Dict[str, Any]:
with open(filename, "r") as f:
config = json.load(f)
return config
Returns:
UnhashableTypeError: Cannot hash object of type _json.Scanner, found in the body of fetch_data_json().
While caching the body of fetch_data_json(), Streamlit encountered an object of type _json.Scanner, which it does not know how to hash.
To address this, please try helping Streamlit understand how to hash that type by passing the hash_funcs argument into @st.cache. For example:
@st.cache(hash_funcs={_json.Scanner: my_hash_func})
def my_func(...):
If you don't know where the object of type _json.Scanner is coming from, try looking at the hash chain below for an object that you do recognize, then pass that to hash_funcs instead:
Object of type _json.Scanner: <_json.Scanner object at 0x7fa22470b160>
Object of type builtins.method: <bound method JSONDecoder.raw_decode of <json.decoder.JSONDecoder object at 0x7fa2247b0760>>
Object of type builtins.method: <bound method JSONDecoder.decode of <json.decoder.JSONDecoder object at 0x7fa2247b0760>>
Object of type builtins.function: <function loads at 0x7fa22470e3a0>
Object of type builtins.function: <function load at 0x7fa22470e310>
Object of type builtins.function: <function fetch_data_json at 0x7fa22128cdc0>
Please see the hash_funcs [documentation] (https://docs.streamlit.io/library/advanced-features/caching#the-hash_funcs-parameter) for more details.
Traceback:
File "app.py", line 23, in <module>
json_file = fetch_data_json(TEST_CONFIG_FOLDER + "/test_config.json")
File "/usr/lib/python3.8/copyreg.py", line 66, in _reduce_ex
raise TypeError(f"cannot pickle {cls.__name__!r} object")
Expected behavior:
In version 1.9.2 the same code would produce valid parsed json data without @st.cache attempting to has an unhashable object. This image below is the method outlined above implemented in 1.10.0 and 1.9.2:
Debug info
- Streamlit version: 1.10.0
- Python version: 3.8.12
- Using Conda? PipEnv? PyEnv? Pex? Pip via Poetry
- OS version: Ubuntu 20.04 (via Windows 11)
- Browser version: Chrome 102.0.5005.115
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:14 (6 by maintainers)
Top Results From Across the Web
Cannot hash object of type _json.Scanner - Using Streamlit
Hi there! I'm using st.cache on this function which requests data from Coin Market Cap and then parses it using Beautiful Soup.
Read more >Streamlit Unhashable TypeError when i use st.cache
after searching in issues section in streamlit repo. i found that hashing argument is not required , just need to pass this argument....
Read more >How to Handle Unhashable Type List Exceptions in Python
The Python TypeError: Unhashable Type: 'list' happens when a mutable list, instead of an immutable tuple, is used as a hash argument.
Read more >[Example code]-Recursion limit in Python when indexing
I was fooling around with the recursion limit in Python, which may be dynamically altered using sys.setrecursionlimit(limit) . The code below demonstrates that ......
Read more >mozilla-central: changeset 547573 ...
third_party/python/pipenv/pipenv/patched/notpip/_internal/commands/hash.py ... Fix unhashable type error during ``$ pipenv install --selective-upgrade`` ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello.
I’m having exactly the same issue here. I have updated Streamlit from 1.7.0 to 1.10.0 and I’m now having an UnhashableTypeError without changing anything in my code.
Weird thing is that sometimes it randomly work (let’s say 1 time out of 10).
I was able to find a temporary fix by adding
hash_funcs={'_json.Scanner': hash}
to each@st.cache
decorator that triggers this error, as stated in an old issue -> https://github.com/streamlit/streamlit/issues/1627#issuecomment-779293099. So far it seems to work.Hey @amhrasmussen,
Release 1.11.1 should be more or less the same release as 1.11.0, except with a security patch cherry-picked onto it. I believe #5023 was merged after 1.11.0 was released, so I wouldn’t expect it to appear in the patch release.
Somewhat conveniently, release 1.12.0 is being cut today (as far as I know), so we should have an official release including #5023 out on Thursday.