Skip to content

Commit 998535b

Browse files
committed
Okay a complete rework
1 parent fc0c35b commit 998535b

File tree

2 files changed

+29
-60
lines changed

2 files changed

+29
-60
lines changed

chia/util/streamable.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import dataclasses
6+
import functools
67
import io
78
import os
89
import pprint
@@ -210,39 +211,33 @@ def streamable_from_dict(klass: type[_T_Streamable], item: Any) -> _T_Streamable
210211

211212

212213
def function_to_convert_one_item(
213-
f_type: type[Any], json_parsers: Optional[list[Callable[[object], Streamable]]] = None
214+
f_type: type[Any], json_parser: Optional[Callable[[object, type[object]], Streamable]] = None
214215
) -> ConvertFunctionType:
215216
if is_type_SpecificOptional(f_type):
216-
convert_inner_func = function_to_convert_one_item(get_args(f_type)[0], json_parsers)
217+
convert_inner_func = function_to_convert_one_item(get_args(f_type)[0], json_parser)
217218
return lambda item: convert_optional(convert_inner_func, item)
218219
elif is_type_Tuple(f_type):
219220
args = get_args(f_type)
220221
convert_inner_tuple_funcs = []
221222
for arg in args:
222-
convert_inner_tuple_funcs.append(function_to_convert_one_item(arg, json_parsers))
223+
convert_inner_tuple_funcs.append(function_to_convert_one_item(arg, json_parser))
223224
# Ignoring for now as the proper solution isn't obvious
224225
return lambda items: convert_tuple(convert_inner_tuple_funcs, items) # type: ignore[arg-type]
225226
elif is_type_List(f_type):
226227
inner_type = get_args(f_type)[0]
227-
convert_inner_func = function_to_convert_one_item(inner_type, json_parsers)
228+
convert_inner_func = function_to_convert_one_item(inner_type, json_parser)
228229
# Ignoring for now as the proper solution isn't obvious
229230
return lambda items: convert_list(convert_inner_func, items) # type: ignore[arg-type]
230231
elif is_type_Dict(f_type):
231232
inner_types = get_args(f_type)
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)
233+
key_converter = function_to_convert_one_item(inner_types[0], json_parser)
234+
value_converter = function_to_convert_one_item(inner_types[1], json_parser)
240235
return lambda mapping: convert_dict(key_converter, value_converter, mapping) # type: ignore[arg-type]
241236
elif hasattr(f_type, "from_json_dict"):
242-
if json_parsers is None:
237+
if json_parser is None:
243238
return f_type.from_json_dict # type: ignore[no-any-return]
244239
else:
245-
return json_parsers[0]
240+
return functools.partial(json_parser, streamable_type=f_type) # type: ignore[call-arg]
246241
elif issubclass(f_type, bytes):
247242
# Type is bytes, data is a hex string or bytes
248243
return lambda item: convert_byte_type(f_type, item)

chia/wallet/util/clvm_streamable.py

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import dataclasses
44
import functools
55
from types import MappingProxyType
6-
from typing import Any, Callable, Generic, Optional, TypeVar, Union, get_args, get_type_hints
6+
from typing import Any, Callable, Generic, Optional, TypeVar, Union, get_type_hints
77

88
from hsms.clvm_serde import from_program_for_type, to_program_for_type
99
from typing_extensions import TypeGuard
@@ -116,64 +116,38 @@ def json_deserialize_with_clvm_streamable(
116116
streamable_type: type[_T_Streamable],
117117
translation_layer: Optional[TranslationLayer] = None,
118118
) -> _T_Streamable:
119+
# This function is flawed for compound types because it's highjacking the function_to_convert_one_item func
120+
# which does not call back to it. More examination is needed.
119121
if isinstance(json_dict, str):
120122
return byte_deserialize_clvm_streamable(
121123
bytes.fromhex(json_dict), streamable_type, translation_layer=translation_layer
122124
)
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]
139-
else:
125+
elif hasattr(streamable_type, "streamable_fields"):
140126
old_streamable_fields = streamable_type.streamable_fields()
141127
new_streamable_fields = []
142128
for old_field in old_streamable_fields:
143-
if is_compound_type(old_field.type):
144-
inner_types = get_args(old_field.type)
145-
new_streamable_fields.append(
146-
dataclasses.replace(
147-
old_field,
148-
convert_function=function_to_convert_one_item(
149-
old_field.type,
150-
[
151-
functools.partial(
152-
json_deserialize_with_clvm_streamable,
153-
streamable_type=inner_type,
154-
translation_layer=translation_layer,
155-
)
156-
for inner_type in inner_types
157-
],
158-
),
159-
)
160-
)
161-
elif is_clvm_streamable_type(old_field.type):
162-
new_streamable_fields.append(
163-
dataclasses.replace(
164-
old_field,
165-
convert_function=functools.partial(
129+
new_streamable_fields.append(
130+
dataclasses.replace(
131+
old_field,
132+
convert_function=function_to_convert_one_item(
133+
old_field.type,
134+
functools.partial(
166135
json_deserialize_with_clvm_streamable,
167-
streamable_type=old_field.type,
168136
translation_layer=translation_layer,
169137
),
170-
)
138+
),
171139
)
172-
else:
173-
new_streamable_fields.append(old_field)
174-
140+
)
175141
setattr(streamable_type, "_streamable_fields", tuple(new_streamable_fields))
176142
return streamable_type.from_json_dict(json_dict)
143+
else:
144+
return function_to_convert_one_item( # type: ignore[return-value]
145+
streamable_type,
146+
functools.partial(
147+
json_deserialize_with_clvm_streamable,
148+
translation_layer=translation_layer,
149+
),
150+
)(json_dict)
177151

178152

179153
_T_ClvmStreamable = TypeVar("_T_ClvmStreamable", bound="Streamable")

0 commit comments

Comments
 (0)