Replace float("nan") in a dictionary with the string "NaN" Parameters ---------- input_dict : Dict[Any, Any] Dictionary to update Returns ------- Dict[Any, Any] Updated dictionary
(input_dict: Dict[Any, Any])
| 105 | |
| 106 | |
| 107 | def dict_replace_nans(input_dict: Dict[Any, Any]) -> Dict[Any, Any]: |
| 108 | """Replace float("nan") in a dictionary with the string "NaN" |
| 109 | |
| 110 | Parameters |
| 111 | ---------- |
| 112 | input_dict : Dict[Any, Any] |
| 113 | Dictionary to update |
| 114 | |
| 115 | Returns |
| 116 | ------- |
| 117 | Dict[Any, Any] |
| 118 | Updated dictionary |
| 119 | """ |
| 120 | result = {} |
| 121 | for key, value in input_dict.items(): |
| 122 | if isinstance(value, dict): |
| 123 | value = dict_replace_nans(value) |
| 124 | elif isinstance(value, list): |
| 125 | value = list_replace_nans(value) |
| 126 | elif isinstance(value, float) and math.isnan(value): |
| 127 | value = betterproto.NAN |
| 128 | result[key] = value |
| 129 | return result |
| 130 | |
| 131 | |
| 132 | @pytest.fixture |
no test coverage detected