question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

st.cache always shows a warning for mutated inputs, even if ignore_hash is True

See original GitHub issue

Steps to reproduce

Get some data files:

python -m spacy download en_core_web_sm

Then run this:

import streamlit as st
import spacy

@st.cache(ignore_hash=True)
def load_model(name):
    return spacy.load(name)

@st.cache(ignore_hash=True)
def process_text(nlp, text):
    return nlp(text)  # This mutates the nlp object. So we raise a warning.

nlp = spacy.load("en_core_web_sm")
doc = process_text(nlp, "Hello world")

What you see:

Cached function mutated its input arguments

When decorating a function with @st.cache, the arguments should not be mutated inside the function body, as that breaks the caching mechanism. Please update the code of process_text to bypass the mutation.

See the Streamlit docs for more info.

Expected

It’s OK if we have a warning for this case, but there should be a way to turn it off too.

Debug info

  • Streamlit version: 0.45.0

Possible solutions

  1. Add kwarg allow_input_mutation=True that suppresses this warning and doesn’t check inputs when pulling from the cache.
  2. This should actually be a different feature request, but if we added kwargs clone_inputs=True and clone_output=True, which allows mutation.

Addendum: should we rename or change the semantics of ignore_hash? There are different hashes in st.cache, and ignore_hash only ignores one of them.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
treuillecommented, Oct 9, 2019

@sam-qordoba : I believe this is the issue you’re seeing.

1reaction
inescommented, Oct 1, 2019

@sam-qordoba That’s interesting – I just double-checked again and the only difference between the two files are the unicode declarations (that should only be necessary for Python 2, so I didn’t bother adding them to my initial example). So I guess there’s something else going on here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Caching issues - Streamlit Docs
How to fix the Cached Object Mutated warning. By default Streamlit expects its cached values to be treated as immutable -- that cached...
Read more >
Customizing the behavior of cached fields - Apollo GraphQL
A merge function that specifies what happens when field's cached value is written; An array of key arguments that help the cache avoid...
Read more >
Advanced Streamlit Caching - Towards Data Science
When we mark a function with Streamlit's cache decorator @st.cache , whenever the function is called streamlit checks the input parameters ...
Read more >
Streamlit Tips, Tricks, and Hacks for Data Scientists - Medium
It has been more than a year since the SSENSE data science team has been using Streamlit actively. Before employing Streamlit, we were...
Read more >
Redux Essentials, Part 8: RTK Query Advanced Patterns
As with adding posts, the first step is to define a new mutation ... The <SinglePostPage> is still using the cached Post entry...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found