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 <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Srijan Sahay Srivastava 2023-10-06 01:03:15 +05:30 committed by GitHub
parent 4567788bd1
commit 37e70842d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:added try except block in `state.py`

View File

@ -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):

View File

@ -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"):