Skip to content

Commit

Permalink
Truncate InvalidCallbackReturnValue size
Browse files Browse the repository at this point in the history
Truncate the stringified value of `bad_val`,
in order to avoid excessive output on the debug pop-up.

Fixes plotly#2658

Signed-off-by: Stavros Ntentos <[email protected]>
  • Loading branch information
stdedos committed Nov 13, 2023
1 parent 3adb9a9 commit 3ef0334
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dash/_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import re
from textwrap import dedent
from keyword import iskeyword
from typing import Any
from typing_extensions import Final

import flask

from ._grouping import grouping_len, map_grouping
Expand All @@ -15,6 +18,8 @@
clean_property_name,
)

TRUNCATE_AT: Final[int] = 100


def validate_callback(outputs, inputs, state, extra_args, types):
Input, Output, State = types
Expand Down Expand Up @@ -209,11 +214,23 @@ def validate_multi_return(output_lists, output_values, callback_id):
)


def truncate_object(obj: Any, at: int) -> str:
"""Return a truncated string representation of an object."""
obj_stringified = str(obj)
size = len(obj_stringified)

if size <= at:
return obj_stringified

return obj_stringified[:at] + f"... (truncated - {size} characters total)"


def fail_callback_output(output_value, output):
valid_children = (str, int, float, type(None), Component)
valid_props = (str, int, float, type(None), tuple, MutableSequence)

def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
bad_val_stringified = truncate_object(bad_val, TRUNCATE_AT)
bad_type = type(bad_val).__name__
outer_id = f"(id={outer_val.id:s})" if getattr(outer_val, "id", False) else ""
outer_type = type(outer_val).__name__
Expand Down Expand Up @@ -244,7 +261,7 @@ def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
{location}
and has string representation
`{bad_val}`
`{bad_val_stringified}`
In general, Dash properties can only be
dash components, strings, dictionaries, numbers, None,
Expand Down

0 comments on commit 3ef0334

Please sign in to comment.