mirror of
https://github.com/gradio-app/gradio.git
synced 2025-02-11 11:19:58 +08:00
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:
parent
c1fa13c9c0
commit
ed156e258b
5
.changeset/floppy-hornets-throw.md
Normal file
5
.changeset/floppy-hornets-throw.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"gradio": patch
|
||||
---
|
||||
|
||||
fix:Fix state serialization issue
|
@ -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:
|
||||
|
@ -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"):
|
||||
|
Loading…
Reference in New Issue
Block a user