Skip to content

Commit ddac08e

Browse files
committed
Broaden function to handle type[object] instead of type[Streamable] (with a little mypy workaround)
1 parent a39253f commit ddac08e

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

chia/wallet/util/clvm_streamable.py

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,27 @@ def json_deserialize_with_clvm_streamable(
120120
return byte_deserialize_clvm_streamable(
121121
bytes.fromhex(json_dict), streamable_type, translation_layer=translation_layer
122122
)
123+
elif not hasattr(streamable_type, "streamable_fields"):
124+
if is_compound_type(streamable_type):
125+
inner_types = get_args(streamable_type)
126+
return function_to_convert_one_item( # type: ignore[return-value]
127+
streamable_type,
128+
[
129+
functools.partial(
130+
json_deserialize_with_clvm_streamable,
131+
streamable_type=inner_type,
132+
translation_layer=translation_layer,
133+
)
134+
for inner_type in inner_types
135+
],
136+
)(json_dict)
137+
else:
138+
return function_to_convert_one_item(streamable_type)(json_dict) # type: ignore[return-value]
123139
else:
124-
if not hasattr(streamable_type, "streamable_fields"):
125-
# This is a rust streamable and therefore cannot be a clvm_streamable
126-
return streamable_type.from_json_dict(json_dict)
127140
old_streamable_fields = streamable_type.streamable_fields()
128141
new_streamable_fields = []
129142
for old_field in old_streamable_fields:
130-
if is_compound_type(old_field.type):
143+
if is_compound_type(old_field):
131144
inner_types = get_args(old_field.type)
132145
new_streamable_fields.append(
133146
dataclasses.replace(
@@ -163,6 +176,58 @@ def json_deserialize_with_clvm_streamable(
163176
return streamable_type.from_json_dict(json_dict)
164177

165178

179+
# def json_deserialize_with_clvm_streamable(
180+
# json_dict: Union[str, dict[str, Any]],
181+
# streamable_type: type[_T_Streamable],
182+
# translation_layer: Optional[TranslationLayer] = None,
183+
# ) -> _T_Streamable:
184+
# if isinstance(json_dict, str):
185+
# return byte_deserialize_clvm_streamable(
186+
# bytes.fromhex(json_dict), streamable_type, translation_layer=translation_layer
187+
# )
188+
# else:
189+
# if not hasattr(streamable_type, "streamable_fields"):
190+
# # This is a rust streamable and therefore cannot be a clvm_streamable
191+
# return streamable_type.from_json_dict(json_dict)
192+
# old_streamable_fields = streamable_type.streamable_fields()
193+
# new_streamable_fields = []
194+
# for old_field in old_streamable_fields:
195+
# if is_compound_type(old_field.type):
196+
# inner_types = get_args(old_field.type)
197+
# new_streamable_fields.append(
198+
# dataclasses.replace(
199+
# old_field,
200+
# convert_function=function_to_convert_one_item(
201+
# old_field.type,
202+
# [
203+
# functools.partial(
204+
# json_deserialize_with_clvm_streamable,
205+
# streamable_type=inner_type,
206+
# translation_layer=translation_layer,
207+
# )
208+
# for inner_type in inner_types
209+
# ],
210+
# ),
211+
# )
212+
# )
213+
# elif is_clvm_streamable_type(old_field.type):
214+
# new_streamable_fields.append(
215+
# dataclasses.replace(
216+
# old_field,
217+
# convert_function=functools.partial(
218+
# json_deserialize_with_clvm_streamable,
219+
# streamable_type=old_field.type,
220+
# translation_layer=translation_layer,
221+
# ),
222+
# )
223+
# )
224+
# else:
225+
# new_streamable_fields.append(old_field)
226+
227+
# setattr(streamable_type, "_streamable_fields", tuple(new_streamable_fields))
228+
# return streamable_type.from_json_dict(json_dict)
229+
230+
166231
_T_ClvmStreamable = TypeVar("_T_ClvmStreamable", bound="Streamable")
167232
_T_TLClvmStreamable = TypeVar("_T_TLClvmStreamable", bound="Streamable")
168233

0 commit comments

Comments
 (0)