Skip to content

Commit 3ef0334

Browse files
committed
Truncate InvalidCallbackReturnValue size
Truncate the stringified value of `bad_val`, in order to avoid excessive output on the debug pop-up. Fixes #2658 Signed-off-by: Stavros Ntentos <[email protected]>
1 parent 3adb9a9 commit 3ef0334

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

dash/_validate.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import re
33
from textwrap import dedent
44
from keyword import iskeyword
5+
from typing import Any
6+
from typing_extensions import Final
7+
58
import flask
69

710
from ._grouping import grouping_len, map_grouping
@@ -15,6 +18,8 @@
1518
clean_property_name,
1619
)
1720

21+
TRUNCATE_AT: Final[int] = 100
22+
1823

1924
def validate_callback(outputs, inputs, state, extra_args, types):
2025
Input, Output, State = types
@@ -209,11 +214,23 @@ def validate_multi_return(output_lists, output_values, callback_id):
209214
)
210215

211216

217+
def truncate_object(obj: Any, at: int) -> str:
218+
"""Return a truncated string representation of an object."""
219+
obj_stringified = str(obj)
220+
size = len(obj_stringified)
221+
222+
if size <= at:
223+
return obj_stringified
224+
225+
return obj_stringified[:at] + f"... (truncated - {size} characters total)"
226+
227+
212228
def fail_callback_output(output_value, output):
213229
valid_children = (str, int, float, type(None), Component)
214230
valid_props = (str, int, float, type(None), tuple, MutableSequence)
215231

216232
def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
233+
bad_val_stringified = truncate_object(bad_val, TRUNCATE_AT)
217234
bad_type = type(bad_val).__name__
218235
outer_id = f"(id={outer_val.id:s})" if getattr(outer_val, "id", False) else ""
219236
outer_type = type(outer_val).__name__
@@ -244,7 +261,7 @@ def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
244261
245262
{location}
246263
and has string representation
247-
`{bad_val}`
264+
`{bad_val_stringified}`
248265
249266
In general, Dash properties can only be
250267
dash components, strings, dictionaries, numbers, None,

0 commit comments

Comments
 (0)