Preserves lists, strings, and None. Tuples and dicts are recursed. All others primitive subclasses are replaced with their types. All non primitives subclasses are passed through to be handled by `default`.
(o)
| 161 | |
| 162 | |
| 163 | def preprocess(o): |
| 164 | """ |
| 165 | Preserves lists, strings, and None. Tuples and dicts are recursed. |
| 166 | |
| 167 | All others primitive subclasses are replaced with their types. |
| 168 | |
| 169 | All non primitives subclasses are passed through to be handled by `default`. |
| 170 | """ |
| 171 | tp = type(o) |
| 172 | if tp == str and len(o) <= MAX_STRING: |
| 173 | return o |
| 174 | if tp == list: |
| 175 | return [preprocess(v) for v in o[:MAX_LIST]] |
| 176 | if tp == dict: |
| 177 | return { |
| 178 | "t": "dict", |
| 179 | "v": [ |
| 180 | [preprocess(k), preprocess(v)] for k, v in list(o.items())[:MAX_DICT] |
| 181 | ], |
| 182 | } |
| 183 | if tp == tuple: |
| 184 | return { |
| 185 | "t": "tuple", |
| 186 | "v": [preprocess(v) for v in o[:MAX_TUPLE]], |
| 187 | } |
| 188 | if isinstance(o, (int, float, bool, list, str, dict, tuple)): |
| 189 | # Don't send literals of these |
| 190 | return {"t": encode_module_value(tp)} |
| 191 | # Other types will be encoded by the `default` function. |
| 192 | return o |
| 193 | |
| 194 | |
| 195 | def default(o: object) -> object: |
no test coverage detected