From 37e70842d59f5aed6fab0086b1abf4b8d991f1c9 Mon Sep 17 00:00:00 2001 From: Srijan Sahay Srivastava <70461251+SrijanSahaySrivastava@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:03:15 +0530 Subject: [PATCH] added try except block in `state.py` (#5790) * added try except block in `state.py` added try except block in `state.py` which will raise a "ValueError" * add changeset * updated `state.py` and added test for deepcopy updated `state.py` and added test for deepcopy named test_initial_value_deepcopy in `test/test_components.py` * lint * test fix * explain test --------- Co-authored-by: Abubakar Abid Co-authored-by: gradio-pr-bot --- .changeset/hot-ducks-end.md | 5 +++++ gradio/components/state.py | 8 +++++++- test/test_components.py | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/hot-ducks-end.md diff --git a/.changeset/hot-ducks-end.md b/.changeset/hot-ducks-end.md new file mode 100644 index 0000000000..5755ee8883 --- /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 9722fa31e5..c027875b09 100644 --- a/gradio/components/state.py +++ b/gradio/components/state.py @@ -37,7 +37,13 @@ class State(IOComponent, SimpleSerializable): 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 a7427a374a..34b6c92995 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -2417,6 +2417,10 @@ class TestState: 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"):