Skip to content

Commit 49a6879

Browse files
committed
Accomodate dicts
1 parent bc0d047 commit 49a6879

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

chia/util/streamable.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,32 +210,39 @@ def streamable_from_dict(klass: type[_T_Streamable], item: Any) -> _T_Streamable
210210

211211

212212
def function_to_convert_one_item(
213-
f_type: type[Any], json_parser: Optional[Callable[[object], Streamable]] = None
213+
f_type: type[Any], json_parsers: Optional[list[Callable[[object], Streamable]]] = None
214214
) -> ConvertFunctionType:
215215
if is_type_SpecificOptional(f_type):
216-
convert_inner_func = function_to_convert_one_item(get_args(f_type)[0], json_parser)
216+
convert_inner_func = function_to_convert_one_item(get_args(f_type)[0], json_parsers)
217217
return lambda item: convert_optional(convert_inner_func, item)
218218
elif is_type_Tuple(f_type):
219219
args = get_args(f_type)
220220
convert_inner_tuple_funcs = []
221221
for arg in args:
222-
convert_inner_tuple_funcs.append(function_to_convert_one_item(arg, json_parser))
222+
convert_inner_tuple_funcs.append(function_to_convert_one_item(arg, json_parsers))
223223
# Ignoring for now as the proper solution isn't obvious
224224
return lambda items: convert_tuple(convert_inner_tuple_funcs, items) # type: ignore[arg-type]
225225
elif is_type_List(f_type):
226226
inner_type = get_args(f_type)[0]
227-
convert_inner_func = function_to_convert_one_item(inner_type, json_parser)
227+
convert_inner_func = function_to_convert_one_item(inner_type, json_parsers)
228228
# Ignoring for now as the proper solution isn't obvious
229229
return lambda items: convert_list(convert_inner_func, items) # type: ignore[arg-type]
230230
elif is_type_Dict(f_type):
231231
inner_types = get_args(f_type)
232-
key_converter = function_to_convert_one_item(inner_types[0], json_parser)
233-
value_converter = function_to_convert_one_item(inner_types[1], json_parser)
232+
if json_parsers is None:
233+
key_parsers = None
234+
value_parsers = None
235+
else:
236+
key_parsers = [json_parsers[0]]
237+
value_parsers = [json_parsers[1]]
238+
key_converter = function_to_convert_one_item(inner_types[0], key_parsers)
239+
value_converter = function_to_convert_one_item(inner_types[1], value_parsers)
234240
return lambda mapping: convert_dict(key_converter, value_converter, mapping) # type: ignore[arg-type]
235241
elif hasattr(f_type, "from_json_dict"):
236-
if json_parser is None:
237-
json_parser = f_type.from_json_dict
238-
return json_parser
242+
if json_parsers is None:
243+
return f_type.from_json_dict # type: ignore[no-any-return]
244+
else:
245+
return json_parsers[0]
239246
elif issubclass(f_type, bytes):
240247
# Type is bytes, data is a hex string or bytes
241248
return lambda item: convert_byte_type(f_type, item)

chia/wallet/util/clvm_streamable.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from chia.util.streamable import (
1313
Streamable,
1414
function_to_convert_one_item,
15+
is_type_Dict,
1516
is_type_List,
1617
is_type_SpecificOptional,
1718
is_type_Tuple,
@@ -95,7 +96,7 @@ def byte_deserialize_clvm_streamable(
9596

9697

9798
def is_compound_type(typ: Any) -> bool:
98-
return is_type_SpecificOptional(typ) or is_type_Tuple(typ) or is_type_List(typ)
99+
return is_type_SpecificOptional(typ) or is_type_Tuple(typ) or is_type_List(typ) or is_type_Dict(typ)
99100

100101

101102
# TODO: this is more than _just_ a Streamable, but it is also a Streamable and that's
@@ -127,17 +128,20 @@ def json_deserialize_with_clvm_streamable(
127128
new_streamable_fields = []
128129
for old_field in old_streamable_fields:
129130
if is_compound_type(old_field.type):
130-
inner_type = get_args(old_field.type)[0]
131+
inner_types = get_args(old_field.type)
131132
new_streamable_fields.append(
132133
dataclasses.replace(
133134
old_field,
134135
convert_function=function_to_convert_one_item(
135136
old_field.type,
136-
functools.partial(
137-
json_deserialize_with_clvm_streamable,
138-
streamable_type=inner_type,
139-
translation_layer=translation_layer,
140-
),
137+
[
138+
functools.partial(
139+
json_deserialize_with_clvm_streamable,
140+
streamable_type=inner_type,
141+
translation_layer=translation_layer,
142+
)
143+
for inner_type in inner_types
144+
],
141145
),
142146
)
143147
)

0 commit comments

Comments
 (0)