diff --git a/.changeset/hot-ducks-end.md b/.changeset/hot-ducks-end.md new file mode 100644 index 000000000000..5755ee8883f1 --- /dev/null +++ b/.changeset/hot-ducks-end.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:added try except block in `state.py` diff --git a/gradio/components/state.py b/gradio/components/state.py index 9722fa31e524..c027875b090f 100644 --- a/gradio/components/state.py +++ b/gradio/components/state.py @@ -37,7 +37,13 @@ def __init__( value: the initial value (of arbitrary type) of the state. The provided argument is deepcopied. If a callable is provided, the function will be called whenever the app loads to set the initial value of the state. """ self.stateful = True - IOComponent.__init__(self, value=deepcopy(value), **kwargs) + try: + self.value = deepcopy(value) + except TypeError as err: + raise TypeError( + f"The initial value of `gr.State` must be able to be deepcopied. The initial value of type {type(value)} cannot be deepcopied." + ) from err + IOComponent.__init__(self, value=self.value, **kwargs) class Variable(State): diff --git a/test/test_components.py b/test/test_components.py index a7427a374a6a..34b6c929959b 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -2417,6 +2417,10 @@ def test_as_component(self): assert state.preprocess("abc") == "abc" assert state.stateful + def test_initial_value_deepcopy(self): + with pytest.raises(TypeError): + gr.State(value=gr) # modules are not deepcopyable + @pytest.mark.asyncio async def test_in_interface(self): def test(x, y=" def"):