|
| 1 | +from copy import deepcopy |
1 | 2 | from typing import Any, Dict, List, Optional, Tuple, Union
|
2 | 3 |
|
3 | 4 | import pydantic
|
@@ -44,41 +45,42 @@ def gather_reasks(
|
44 | 45 | reasks = []
|
45 | 46 |
|
46 | 47 | def _gather_reasks_in_dict(
|
47 |
| - output: Dict, path: Optional[List[Union[str, int]]] = None |
| 48 | + original: Dict, output: Dict, path: Optional[List[Union[str, int]]] = None |
48 | 49 | ) -> None:
|
49 | 50 | if path is None:
|
50 | 51 | path = []
|
51 |
| - for field, value in output.items(): |
| 52 | + for field, value in original.items(): |
52 | 53 | if isinstance(value, FieldReAsk):
|
53 | 54 | value.path = path + [field]
|
54 | 55 | reasks.append(value)
|
55 | 56 | del output[field]
|
56 | 57 |
|
57 | 58 | if isinstance(value, dict):
|
58 |
| - _gather_reasks_in_dict(value, path + [field]) |
| 59 | + _gather_reasks_in_dict(value, output[field], path + [field]) |
59 | 60 |
|
60 | 61 | if isinstance(value, list):
|
61 |
| - _gather_reasks_in_list(value, path + [field]) |
| 62 | + _gather_reasks_in_list(value, output[field], path + [field]) |
62 | 63 | return
|
63 | 64 |
|
64 | 65 | def _gather_reasks_in_list(
|
65 |
| - output: List, path: Optional[List[Union[str, int]]] = None |
| 66 | + original: List, output: List, path: Optional[List[Union[str, int]]] = None |
66 | 67 | ) -> None:
|
67 | 68 | if path is None:
|
68 | 69 | path = []
|
69 |
| - for idx, item in enumerate(output): |
| 70 | + for idx, item in enumerate(original): |
70 | 71 | if isinstance(item, FieldReAsk):
|
71 | 72 | item.path = path + [idx]
|
72 | 73 | reasks.append(item)
|
73 | 74 | del output[idx]
|
74 | 75 | elif isinstance(item, dict):
|
75 |
| - _gather_reasks_in_dict(item, path + [idx]) |
| 76 | + _gather_reasks_in_dict(item, output[idx], path + [idx]) |
76 | 77 | elif isinstance(item, list):
|
77 |
| - _gather_reasks_in_list(item, path + [idx]) |
| 78 | + _gather_reasks_in_list(item, output[idx], path + [idx]) |
78 | 79 | return
|
79 | 80 |
|
80 |
| - _gather_reasks_in_dict(validated_output) |
81 |
| - return reasks, validated_output |
| 81 | + output_copy = deepcopy(validated_output) |
| 82 | + _gather_reasks_in_dict(validated_output, output_copy) |
| 83 | + return reasks, output_copy |
82 | 84 |
|
83 | 85 |
|
84 | 86 | def get_pruned_tree(
|
|
0 commit comments