Fix state serialization issue (#10036)

* Fix bug

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Freddy Boulton 2024-11-27 14:25:02 -05:00 committed by GitHub
parent c1fa13c9c0
commit ed156e258b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Fix state serialization issue

View File

@ -45,12 +45,13 @@ class State(Component):
)
self.delete_callback = delete_callback or (lambda a: None) # noqa: ARG005
try:
self.value = deepcopy(value)
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
super().__init__(value=self.value, render=render)
super().__init__(value=value, render=render)
self.value = value
@property
def stateful(self) -> bool:

View File

@ -1,8 +1,13 @@
import pytest
from pydantic import BaseModel
import gradio as gr
class TestModel(BaseModel):
name: str
class TestState:
def test_as_component(self):
state = gr.State(value=5)
@ -14,6 +19,10 @@ class TestState:
with pytest.raises(TypeError):
gr.State(value=gr) # modules are not deepcopyable
def test_initial_value_pydantic(self):
state = gr.State(value=TestModel(name="Freddy"))
assert isinstance(state.value, TestModel)
@pytest.mark.asyncio
async def test_in_interface(self):
def test(x, y=" def"):