Skip to content

Commit

Permalink
Fix state serialization issue (#10036)
Browse files Browse the repository at this point in the history
* Fix bug

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
freddyaboulton and gradio-pr-bot authored Nov 27, 2024
1 parent c1fa13c commit ed156e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/floppy-hornets-throw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Fix state serialization issue
5 changes: 3 additions & 2 deletions gradio/components/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def __init__(
)
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:
Expand Down
9 changes: 9 additions & 0 deletions test/components/test_state.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -14,6 +19,10 @@ def test_initial_value_deepcopy(self):
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"):
Expand Down

0 comments on commit ed156e2

Please sign in to comment.