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
10
10
11
11
from chia .types .blockchain_format .program import Program
12
+ from chia .util .byte_types import hexstr_to_bytes
12
13
from chia .util .streamable import (
13
14
Streamable ,
14
15
function_to_convert_one_item ,
15
- is_type_List ,
16
- is_type_SpecificOptional ,
17
16
is_type_Tuple ,
18
17
recurse_jsonify ,
19
18
streamable ,
@@ -94,14 +93,10 @@ def byte_deserialize_clvm_streamable(
94
93
)
95
94
96
95
97
- def is_compound_type (typ : Any ) -> bool :
98
- return is_type_SpecificOptional (typ ) or is_type_Tuple (typ ) or is_type_List (typ )
99
-
100
-
101
96
# TODO: this is more than _just_ a Streamable, but it is also a Streamable and that's
102
97
# useful for now
103
- def is_clvm_streamable_type (v : type [object ]) -> TypeGuard [ type [ Streamable ]] :
104
- return issubclass (v , Streamable ) and hasattr (v , "_clvm_streamable" )
98
+ def is_clvm_streamable_type (v : type [object ]) -> bool :
99
+ return isinstance ( v , type ) and issubclass (v , Streamable ) and hasattr (v , "_clvm_streamable" )
105
100
106
101
107
102
# TODO: this is more than _just_ a Streamable, but it is also a Streamable and that's
@@ -115,48 +110,40 @@ def json_deserialize_with_clvm_streamable(
115
110
streamable_type : type [_T_Streamable ],
116
111
translation_layer : Optional [TranslationLayer ] = None ,
117
112
) -> _T_Streamable :
118
- if isinstance (json_dict , str ):
113
+ # This function is flawed for compound types because it's highjacking the function_to_convert_one_item func
114
+ # which does not call back to it. More examination is needed.
115
+ if is_clvm_streamable_type (streamable_type ) and isinstance (json_dict , str ):
119
116
return byte_deserialize_clvm_streamable (
120
- bytes . fromhex (json_dict ), streamable_type , translation_layer = translation_layer
117
+ hexstr_to_bytes (json_dict ), streamable_type , translation_layer = translation_layer
121
118
)
122
- else :
119
+ elif hasattr ( streamable_type , "streamable_fields" ) :
123
120
old_streamable_fields = streamable_type .streamable_fields ()
124
121
new_streamable_fields = []
125
122
for old_field in old_streamable_fields :
126
- if is_compound_type (old_field .type ):
127
- inner_type = get_args (old_field .type )[0 ]
128
- if is_clvm_streamable_type (inner_type ):
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 (
135
- json_deserialize_with_clvm_streamable ,
136
- streamable_type = inner_type ,
137
- translation_layer = translation_layer ,
138
- ),
139
- ),
140
- )
141
- )
142
- else :
143
- new_streamable_fields .append (old_field )
144
- elif is_clvm_streamable_type (old_field .type ):
145
- new_streamable_fields .append (
146
- dataclasses .replace (
147
- old_field ,
148
- convert_function = functools .partial (
123
+ new_streamable_fields .append (
124
+ dataclasses .replace (
125
+ old_field ,
126
+ convert_function = function_to_convert_one_item (
127
+ old_field .type ,
128
+ functools .partial (
149
129
json_deserialize_with_clvm_streamable ,
150
- streamable_type = old_field .type ,
151
130
translation_layer = translation_layer ,
152
131
),
153
- )
132
+ ),
154
133
)
155
- else :
156
- new_streamable_fields .append (old_field )
157
-
134
+ )
158
135
setattr (streamable_type , "_streamable_fields" , tuple (new_streamable_fields ))
159
- return streamable_type .from_json_dict (json_dict )
136
+ return streamable_type .from_json_dict (json_dict ) # type: ignore[arg-type]
137
+ elif hasattr (streamable_type , "from_json_dict" ):
138
+ return streamable_type .from_json_dict (json_dict ) # type: ignore[arg-type]
139
+ else :
140
+ return function_to_convert_one_item ( # type: ignore[return-value]
141
+ streamable_type ,
142
+ functools .partial (
143
+ json_deserialize_with_clvm_streamable ,
144
+ translation_layer = translation_layer ,
145
+ ),
146
+ )(json_dict )
160
147
161
148
162
149
_T_ClvmStreamable = TypeVar ("_T_ClvmStreamable" , bound = "Streamable" )
0 commit comments