Skip to content

Commit 61ba0bb

Browse files
authored
fix: dataclass should not modify raw dict (#425)
1 parent f6a9cfd commit 61ba0bb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

oocana/oocana/handle.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dataclasses import dataclass, field
22
from typing import Any, Optional
33
from .schema import FieldSchema, ContentMediaType
4+
import copy
45

56
__all__ = ["HandleDef", "InputHandleDef", "OutputHandleDef"]
67

@@ -45,12 +46,15 @@ class HandleDef(DataDict):
4546

4647
def __init__(self, **kwargs):
4748
for key, value in kwargs.items():
48-
object.__setattr__(self, key, value)
49+
if key == 'json_schema' and isinstance(value, dict):
50+
object.__setattr__(self, key, copy.deepcopy(value))
51+
else: # For other attributes, we can use the default behavior of object.__setattr__
52+
object.__setattr__(self, key, value)
4953
if "handle" not in kwargs:
5054
raise ValueError("missing attr key: 'handle'")
5155
json_schema = self.json_schema
5256
if json_schema is not None and not isinstance(json_schema, FieldSchema):
53-
object.__setattr__(self, "_raw_json_schema", json_schema)
57+
object.__setattr__(self, "_raw_json_schema", copy.deepcopy(json_schema))
5458
object.__setattr__(self, "json_schema", FieldSchema.generate_schema(json_schema))
5559

5660
def check_handle_type(self, type: ContentMediaType) -> bool:

oocana/tests/test_handle.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,22 @@ def test_serializable_var_output_handle(self):
208208
"need_serialize_var_for_cache": True
209209
})
210210

211+
def test_json_schema(self):
212+
"""Test that json schema can be converted to dict."""
213+
d = {
214+
"handle": "test",
215+
"json_schema": {
216+
"type": "object",
217+
"properties": {
218+
"name": { "type": "string" },
219+
"age": { "type": "number" }
220+
},
221+
}
222+
}
223+
handle_def = HandleDef(**d)
224+
self.assertEqual(handle_def.json_schema_to_dict(), d["json_schema"])
225+
self.assertIsInstance(d["json_schema"]["properties"]["name"], dict)
226+
211227
def test_var_handle(self):
212228
self.handle_test(HandleDef(**var_handle), {
213229
"handle": "test",

0 commit comments

Comments
 (0)