Skip to content

Commit 15fb6a3

Browse files
committed
tezos parser pydantic fix wip
1 parent 22edb50 commit 15fb6a3

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/dipdup/indexes/tezos_operations/parser.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def is_array_type(storage_type: type[Any]) -> bool:
5050

5151
def get_list_elt_type(list_type: type[Any]) -> type[Any]:
5252
"""Extract list item type from list type"""
53+
if list_type is Any:
54+
return type(Any)
55+
5356
# NOTE: regular list
5457
if get_origin(list_type) == list: # noqa: E721
5558
return get_args(list_type)[0] # type: ignore[no-any-return]
@@ -61,6 +64,9 @@ def get_list_elt_type(list_type: type[Any]) -> type[Any]:
6164

6265
def get_dict_value_type(dict_type: type[Any], key: str | None = None) -> type[Any]:
6366
"""Extract dict value types from field type"""
67+
if dict_type is Any:
68+
return type(Any)
69+
6470
# NOTE: Regular dict
6571
if get_origin(dict_type) == dict: # noqa: E721
6672
return get_args(dict_type)[1] # type: ignore[no-any-return]
@@ -78,7 +84,10 @@ def get_dict_value_type(dict_type: type[Any], key: str | None = None) -> type[An
7884
if key in (name, field.alias):
7985
return field.annotation # type: ignore[no-any-return]
8086

81-
# NOTE: Either we try the wrong Union path or model was modifier by user
87+
if dict_type.model_config['extra'] in ('ignore', 'allow'):
88+
return dict_type
89+
90+
# NOTE: Either we tried wrong Union path or model was modifier by user
8291
raise KeyError(f'Field `{key}` not found in {dict_type}')
8392

8493

@@ -114,18 +123,26 @@ def _apply_bigmap_diffs(
114123
) -> list[dict[str, Any]] | dict[str, Any]:
115124
"""Apply bigmap diffs to the storage"""
116125
diffs = bigmap_diffs.get(bigmap_id, ())
117-
diffs_items = ((d['content']['key'], d['content']['value']) for d in diffs)
118126

119-
if is_array:
120-
list_storage: list[dict[str, Any]] = []
121-
for key, value in diffs_items:
127+
list_storage: list[dict[str, Any]] = []
128+
dict_storage: dict[str, Any] = {}
129+
130+
for item in diffs:
131+
key, value = item['content']['key'], item['content']['value']
132+
133+
if is_array:
134+
list_storage.append({'key': key, 'value': value})
135+
continue
136+
try:
137+
dict_storage[key] = value
138+
except TypeError:
122139
list_storage.append({'key': key, 'value': value})
123-
return list_storage
140+
else:
141+
return list_storage if is_array else dict_storage
124142

125-
dict_storage: dict[str, Any] = {}
126-
for key, value in diffs_items:
127-
dict_storage[key] = value
128-
return dict_storage
143+
if list_storage and dict_storage:
144+
raise Exception
145+
return list_storage or dict_storage
129146

130147

131148
def _process_storage(

0 commit comments

Comments
 (0)