Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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 <133706+stdedos@users.noreply.github.com>
  • Loading branch information
stdedos committed Nov 13, 2023
commit 3ef033443328c014a6950b6cc219044f11021ea1
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