|
3 | 3 | import dataclasses
|
4 | 4 | import functools
|
5 | 5 | 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 |
7 | 7 |
|
8 | 8 | from hsms.clvm_serde import from_program_for_type, to_program_for_type
|
9 | 9 | from typing_extensions import TypeGuard
|
@@ -116,64 +116,38 @@ def json_deserialize_with_clvm_streamable(
|
116 | 116 | streamable_type: type[_T_Streamable],
|
117 | 117 | translation_layer: Optional[TranslationLayer] = None,
|
118 | 118 | ) -> _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. |
119 | 121 | if isinstance(json_dict, str):
|
120 | 122 | return byte_deserialize_clvm_streamable(
|
121 | 123 | bytes.fromhex(json_dict), streamable_type, translation_layer=translation_layer
|
122 | 124 | )
|
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"): |
140 | 126 | old_streamable_fields = streamable_type.streamable_fields()
|
141 | 127 | new_streamable_fields = []
|
142 | 128 | 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( |
166 | 135 | json_deserialize_with_clvm_streamable,
|
167 |
| - streamable_type=old_field.type, |
168 | 136 | translation_layer=translation_layer,
|
169 | 137 | ),
|
170 |
| - ) |
| 138 | + ), |
171 | 139 | )
|
172 |
| - else: |
173 |
| - new_streamable_fields.append(old_field) |
174 |
| - |
| 140 | + ) |
175 | 141 | setattr(streamable_type, "_streamable_fields", tuple(new_streamable_fields))
|
176 | 142 | 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) |
177 | 151 |
|
178 | 152 |
|
179 | 153 | _T_ClvmStreamable = TypeVar("_T_ClvmStreamable", bound="Streamable")
|
|
0 commit comments