mirror of
https://github.com/gradio-app/gradio.git
synced 2025-04-06 12:30:29 +08:00
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:
parent
4567788bd1
commit
37e70842d5
5
.changeset/hot-ducks-end.md
Normal file
5
.changeset/hot-ducks-end.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"gradio": patch
|
||||
---
|
||||
|
||||
fix:added try except block in `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):
|
||||
|
@ -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"):
|
||||
|
Loading…
x
Reference in New Issue
Block a user