|
14 | 14 |
|
15 | 15 |
|
16 | 16 | import json |
17 | | -from typing import Any, Callable, Dict, Optional, Sequence, Set, Union |
| 17 | +from typing import Any, Callable, Dict, Optional, Sequence, Set, Union, Tuple |
18 | 18 |
|
19 | 19 | Primitive = Union[bool, str, int, float] |
20 | 20 | BoolList = list[bool] |
@@ -63,32 +63,49 @@ def _get_flatten_func( |
63 | 63 | return None |
64 | 64 |
|
65 | 65 |
|
66 | | -def _flatten_compound_value( |
| 66 | +def _flatten_with_flatten_func( |
67 | 67 | key: str, |
68 | 68 | value: Any, |
69 | 69 | exclude_keys: Set[str], |
70 | 70 | rename_keys: Dict[str, str], |
71 | 71 | flatten_functions: Dict[str, Callable], |
72 | | - key_names: Set[str], |
73 | | - _from_json=False, |
74 | | -) -> FlattenedDict: |
| 72 | + key_names: Set[str]) -> Tuple[bool, Any]: |
75 | 73 | flatten_func = _get_flatten_func(flatten_functions, key_names) |
76 | | - if flatten_func is not None: |
77 | | - func_output = flatten_func( |
| 74 | + if flatten_func is None: |
| 75 | + return False, value |
| 76 | + func_output = flatten_func( |
78 | 77 | key, |
79 | 78 | value, |
80 | 79 | exclude_keys=exclude_keys, |
81 | 80 | rename_keys=rename_keys, |
82 | 81 | flatten_functions=flatten_functions, |
83 | 82 | ) |
84 | | - if func_output is None: |
85 | | - return {} |
86 | | - elif _is_primitive(func_output) or _is_homogenous_primitive_list( |
87 | | - func_output |
88 | | - ): |
89 | | - return {key: func_output} |
90 | | - else: |
91 | | - value = func_output |
| 83 | + if func_output is None: |
| 84 | + return True, {} |
| 85 | + if _is_primitive(func_output) or _is_homogenous_primitive_list( |
| 86 | + func_output): |
| 87 | + return True, {key: func_output} |
| 88 | + return False, func_output |
| 89 | + |
| 90 | + |
| 91 | +def _flatten_compound_value( |
| 92 | + key: str, |
| 93 | + value: Any, |
| 94 | + exclude_keys: Set[str], |
| 95 | + rename_keys: Dict[str, str], |
| 96 | + flatten_functions: Dict[str, Callable], |
| 97 | + key_names: Set[str], |
| 98 | + _from_json=False, |
| 99 | +) -> FlattenedDict: |
| 100 | + fully_flattened_with_flatten_func, value = _flatten_with_flatten_func( |
| 101 | + key=key, |
| 102 | + value=value, |
| 103 | + exclude_keys=exclude_keys, |
| 104 | + rename_keys=rename_keys, |
| 105 | + flatten_functions=flatten_functions, |
| 106 | + key_names=key_names) |
| 107 | + if fully_flattened_with_flatten_func: |
| 108 | + return value |
92 | 109 | if isinstance(value, dict): |
93 | 110 | return _flatten_dict( |
94 | 111 | value, |
|
0 commit comments