Skip to content

Commit 5f28a34

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 aa0473e commit 5f28a34

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

dash/_validate.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import re
33
from textwrap import dedent
44
from keyword import iskeyword
5+
from typing import Any, Final
6+
57
import flask
68

79
from ._grouping import grouping_len, map_grouping
@@ -15,6 +17,8 @@
1517
clean_property_name,
1618
)
1719

20+
TRUNCATE_AT: Final[int] = 100
21+
1822

1923
def validate_callback(outputs, inputs, state, extra_args, types):
2024
Input, Output, State = types
@@ -209,11 +213,23 @@ def validate_multi_return(output_lists, output_values, callback_id):
209213
)
210214

211215

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+
212227
def fail_callback_output(output_value, output):
213228
valid_children = (str, int, float, type(None), Component)
214229
valid_props = (str, int, float, type(None), tuple, MutableSequence)
215230

216231
def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
232+
bad_val_stringified = truncate_object(bad_val, TRUNCATE_AT)
217233
bad_type = type(bad_val).__name__
218234
outer_id = f"(id={outer_val.id:s})" if getattr(outer_val, "id", False) else ""
219235
outer_type = type(outer_val).__name__
@@ -244,7 +260,7 @@ def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
244260
245261
{location}
246262
and has string representation
247-
`{bad_val}`
263+
`{bad_val_stringified}`
248264
249265
In general, Dash properties can only be
250266
dash components, strings, dictionaries, numbers, None,

0 commit comments

Comments
 (0)