|
2 | 2 | import re
|
3 | 3 | from textwrap import dedent
|
4 | 4 | from keyword import iskeyword
|
| 5 | +from typing import Any, Final |
| 6 | + |
5 | 7 | import flask
|
6 | 8 |
|
7 | 9 | from ._grouping import grouping_len, map_grouping
|
|
15 | 17 | clean_property_name,
|
16 | 18 | )
|
17 | 19 |
|
| 20 | +TRUNCATE_AT: Final[int] = 100 |
| 21 | + |
18 | 22 |
|
19 | 23 | def validate_callback(outputs, inputs, state, extra_args, types):
|
20 | 24 | Input, Output, State = types
|
@@ -209,11 +213,23 @@ def validate_multi_return(output_lists, output_values, callback_id):
|
209 | 213 | )
|
210 | 214 |
|
211 | 215 |
|
| 216 | +def truncate_object(obj: Any, at: int) -> str: |
| 217 | + """Return a truncated string representation of an object.""" |
| 218 | + obj_stringified = str(obj) |
| 219 | + size = len(obj_stringified) |
| 220 | + |
| 221 | + if size <= at: |
| 222 | + return obj_stringified |
| 223 | + |
| 224 | + return obj_stringified[:at] + f"... (truncated - {size} characters total)" |
| 225 | + |
| 226 | + |
212 | 227 | def fail_callback_output(output_value, output):
|
213 | 228 | valid_children = (str, int, float, type(None), Component)
|
214 | 229 | valid_props = (str, int, float, type(None), tuple, MutableSequence)
|
215 | 230 |
|
216 | 231 | def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
|
| 232 | + bad_val_stringified = truncate_object(bad_val, TRUNCATE_AT) |
217 | 233 | bad_type = type(bad_val).__name__
|
218 | 234 | outer_id = f"(id={outer_val.id:s})" if getattr(outer_val, "id", False) else ""
|
219 | 235 | outer_type = type(outer_val).__name__
|
@@ -244,7 +260,7 @@ def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
|
244 | 260 |
|
245 | 261 | {location}
|
246 | 262 | and has string representation
|
247 |
| - `{bad_val}` |
| 263 | + `{bad_val_stringified}` |
248 | 264 |
|
249 | 265 | In general, Dash properties can only be
|
250 | 266 | dash components, strings, dictionaries, numbers, None,
|
|
0 commit comments