Skip to content

Give a more informative error for JSON not serializable. (#269) #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 1, 2018
105 changes: 105 additions & 0 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,110 @@ def _validate_callback(self, output, inputs, state, events):
output.component_id,
output.component_property).replace(' ', ''))

def _validate_callback_output(self, output_value, output):
valid = [str, dict, int, float, type(None), Component]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we JSON serialize with the json.dumps(obj, cls=plotly.utils.PlotlyJSONEncoder), this list actually has a few other items: https://github.com/plotly/plotly.py/blob/6b3a0135977b92b3f7e0be2a1e8b418d995c70e3/plotly/utils.py#L137-L332

So, if there was a component property that accepted a list of numbers, then technically the user could return a numpy array or a dataframe.

The only example that immediately comes to mind is updating the figure property in a callback with the plotly.graph_objs. These objects get serialized because they have a to_plotly_json method.

So, I wonder if instead of validating up-front, we should only run this routine only if our json.dumps(resp, plotly.utils.PlotlyJSONEncoder) call fails.

This would have the other advantage of being faster for the non-error case. If the user returns a huge object (e.g. some of my callbacks in apps return a 2MB nested component), doing this recursive validation might be slow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I am not sure how large of a speed up that would be since validating a large callback output is likely much faster than the network delay of sending that large output, but it definitely makes it easier than crafting a perfect validator (that would need to update every time we want to extend PlotlyJSONEncoder).


def _raise_invalid(bad_val, outer_val, bad_type, path, index=None,
toplevel=False):
outer_id = "(id={:s})".format(outer_val.id) \
if getattr(outer_val, 'id', False) else ''
outer_type = type(outer_val).__name__
raise exceptions.ReturnValueNotJSONSerializable('''
The callback for property `{property:s}` of component `{id:s}`
returned a {object:s} having type `{type:s}`
which is not JSON serializable.

{location_header:s}{location:s}
and has string representation
`{bad_val}`

In general, Dash properties can only be
dash components, strings, dictionaries, numbers, None,
or lists of those.
'''.format(
property=output.component_property,
id=output.component_id,
object='tree with one value' if not toplevel else 'value',
type=bad_type,
location_header=(
'The value in question is located at'
if not toplevel else
'''The value in question is either the only value returned,
or is in the top level of the returned list,'''
),
location=(
"\n" +
("[{:d}] {:s} {:s}".format(index, outer_type, outer_id)
if index is not None
else ('[*] ' + outer_type + ' ' + outer_id))
+ "\n" + path + "\n"
) if not toplevel else '',
bad_val=bad_val).replace(' ', ''))

def _value_is_valid(val):
return (
# pylint: disable=unused-variable
any([isinstance(val, x) for x in valid]) or
type(val).__name__ == 'unicode'
)

def _validate_value(val, index=None):
# val is a Component
if isinstance(val, Component):
for p, j in val.traverse_with_paths():
# check each component value in the tree
if not _value_is_valid(j):
_raise_invalid(
bad_val=j,
outer_val=val,
bad_type=type(j).__name__,
path=p,
index=index
)

# Children that are not of type Component or
# collections.MutableSequence not returned by traverse
child = getattr(j, 'children', None)
if not isinstance(child, collections.MutableSequence):
if child and not _value_is_valid(child):
_raise_invalid(
bad_val=child,
outer_val=val,
bad_type=type(child).__name__,
path=p + "\n" + "[*] " + type(child).__name__,
index=index
)

# Also check the child of val, as it will not be returned
child = getattr(val, 'children', None)
if not isinstance(child, collections.MutableSequence):
if child and not _value_is_valid(child):
_raise_invalid(
bad_val=child,
outer_val=val,
bad_type=type(child).__name__,
path=type(child).__name__,
index=index
)

# val is not a Component, but is at the top level of tree
else:
if not _value_is_valid(val):
_raise_invalid(
bad_val=val,
outer_val=type(val).__name__,
bad_type=type(val).__name__,
path='',
index=index,
toplevel=True
)

if isinstance(output_value, list):
for i, val in enumerate(output_value):
_validate_value(val, index=i)
else:
_validate_value(output_value)

# TODO - Update nomenclature.
# "Parents" and "Children" should refer to the DOM tree
# and not the dependency tree.
Expand Down Expand Up @@ -513,6 +617,7 @@ def wrap_func(func):
def add_context(*args, **kwargs):

output_value = func(*args, **kwargs)
self._validate_callback_output(output_value, output)
response = {
'response': {
'props': {
Expand Down
28 changes: 21 additions & 7 deletions dash/development/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,36 @@ def __delitem__(self, id): # pylint: disable=redefined-builtin

def traverse(self):
"""Yield each item in the tree."""
for t in self.traverse_with_paths():
yield t[1]

def traverse_with_paths(self):
"""Yield each item with its path in the tree."""
children = getattr(self, 'children', None)
children_type = type(children).__name__
children_id = "(id={:s})".format(children.id) \
if getattr(children, 'id', False) else ''
children_string = children_type + ' ' + children_id

# children is just a component
if isinstance(children, Component):
yield children
for t in children.traverse():
yield t
yield "[*] " + children_string, children
for p, t in children.traverse_with_paths():
yield "\n".join(["[*] " + children_string, p]), t

# children is a list of components
elif isinstance(children, collections.MutableSequence):
for i in children: # pylint: disable=not-an-iterable
yield i
for idx, i in enumerate(children):
list_path = "[{:d}] {:s} {}".format(
idx,
type(i).__name__,
"(id={:s})".format(i.id) if getattr(i, 'id', False) else ''
)
yield list_path, i

if isinstance(i, Component):
for t in i.traverse():
yield t
for p, t in i.traverse_with_paths():
yield "\n".join([list_path, p]), t

def __iter__(self):
"""Yield IDs in the tree of children."""
Expand Down
4 changes: 4 additions & 0 deletions dash/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ class CantHaveMultipleOutputs(CallbackException):

class PreventUpdate(CallbackException):
pass


class ReturnValueNotJSONSerializable(CallbackException):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just me, but I don't like that exception name, it gives too much info on the cause while not giving the true nature of the error, that is the return value of a callback is invalid. I would change it to InvalidCallbackReturnValue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, pushed those changes.

pass