Skip to content

Validate component properties #264 - March 1 #452

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

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5ac225b
Add `numpy`, `pandas`, `Cerberus` to requirements.
rmarren1 Nov 8, 2018
b1ef131
Add cerberus to setup.py install_requires
rmarren1 Nov 8, 2018
9e5c370
Add a `suppress_validation_exceptions` option to `dash.Dash`.
rmarren1 Nov 8, 2018
26e425a
Add validation exception definitions.
rmarren1 Nov 8, 2018
10498d2
Add the `DashValidator` class.
rmarren1 Nov 8, 2018
d943e2e
Add a modular decode hook for loading `metadata.json` files.
rmarren1 Nov 8, 2018
b12c73d
Add a `validate` method to the base component class.
rmarren1 Nov 8, 2018
c123915
Add schema generation logic.
rmarren1 Nov 8, 2018
6381b54
Integrate schema generation with the Python class string generation.
rmarren1 Nov 8, 2018
7ac65f2
Re-name validation functions to be more explicit.
rmarren1 Nov 8, 2018
1ba24f7
Validate callback outputs.
rmarren1 Nov 8, 2018
f21372b
Validate app initial layout.
rmarren1 Nov 8, 2018
398950d
Update test component code (react 16, children PropType, more props)
rmarren1 Nov 8, 2018
83f70d7
Re-build metadata_test.py
rmarren1 Nov 8, 2018
0883290
Update base component tests that would have broken given component ch…
rmarren1 Nov 8, 2018
422c2e2
Add test for the generated schema.
rmarren1 Nov 8, 2018
3ff6dd4
Update component loader tests.
rmarren1 Nov 8, 2018
b81084a
Add component validation tests.
rmarren1 Nov 8, 2018
27d7da5
Run component validation tests in CI.
rmarren1 Nov 8, 2018
7aa7060
Add some lines forgotten during rebase.
rmarren1 Nov 8, 2018
28b8a13
Pylint fixes.
rmarren1 Nov 8, 2018
ee12470
Flake8 fixes.
rmarren1 Nov 8, 2018
e817d75
Make style changes in response to code review.
rmarren1 Nov 8, 2018
6b8cfd3
Lint fixes.
rmarren1 Nov 8, 2018
2c2faf5
Fixes for review.
rmarren1 Nov 12, 2018
5d560e6
Do not suppress callback exceptions when in `debug=False`.
rmarren1 Nov 12, 2018
1f48149
Pylint, Flake8 fixes.
rmarren1 Nov 12, 2018
29c363c
Add back circleci flake8 config.
rmarren1 Nov 17, 2018
783d2c2
Flake8 fixes in tests
rmarren1 Nov 17, 2018
8e96ed2
Lock dev-requirements `dash` version to validation RC.
rmarren1 Nov 24, 2018
79b5251
Test that sets do not validate as lists.
rmarren1 Nov 25, 2018
1bf1036
Change list validation to handle sets properly.
rmarren1 Nov 25, 2018
d842651
Test that arbitraty types can be validated as a number without error.
rmarren1 Nov 25, 2018
5936dfa
Only print validation suppression message once.
rmarren1 Nov 30, 2018
dcc3c40
Rebase on dash 31.
rmarren1 Nov 30, 2018
06d03c7
Delete duplicate files from rebase.
rmarren1 Nov 30, 2018
83f501a
Properly remove schema for python version cross compatibility.
rmarren1 Nov 30, 2018
1dae812
Fix prop filtering in validate method
rmarren1 Nov 30, 2018
7d00b80
:hocho: old method of validating required props
rmarren1 Nov 30, 2018
86d5e7c
Rebase.
rmarren1 Dec 14, 2018
f7312bf
Merge branch 'master' into validate2
rmarren1 Dec 18, 2018
5a50ee8
Merge with current master
rmarren1 Feb 21, 2019
792ef39
Add test_component_validation test to test.sh
rmarren1 Feb 21, 2019
598a432
Add generated schema to new base class, fix tests.
rmarren1 Feb 21, 2019
aa49488
Validation tests require numpy and pandas.
rmarren1 Feb 21, 2019
56c2873
Remove duplicate key
rmarren1 Feb 21, 2019
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
3 changes: 3 additions & 0 deletions .circleci/requirements/dev-requirements-py37.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ requests[security]
flake8
pylint==2.2.2
astroid==2.1.0
Cerberus
numpy
pandas
3 changes: 3 additions & 0 deletions .circleci/requirements/dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ plotly==3.6.1
requests[security]
flake8
pylint==1.9.4
Cerberus
numpy
pandas
6 changes: 6 additions & 0 deletions dash/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ def first(self, *names):
value = self.get(name)
if value:
return value


def _merge(x, y):
z = x.copy()
z.update(y)
return z
109 changes: 102 additions & 7 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import threading
import warnings
import re
import pprint
import logging

from functools import wraps
Expand All @@ -25,6 +26,8 @@
from .dependencies import Input, Output, State
from .resources import Scripts, Css
from .development.base_component import Component, ComponentRegistry
from .development.validator import (DashValidator,
generate_validation_error_message)
from . import exceptions
from ._utils import AttributeDict as _AttributeDict
from ._utils import interpolate_str as _interpolate
Expand Down Expand Up @@ -69,6 +72,27 @@
_re_index_config_id = re.compile(r'id="_dash-config"')
_re_index_scripts_id = re.compile(r'src=".*dash[-_]renderer.*"')

_callback_validation_error_template = """
A Dash Callback produced an invalid value!

Dash tried to update the `{component_property}` prop of the
`{component_name}` with id `{component_id}` by calling the
`{callback_func_name}` function with `{args}` as arguments.

This function call returned `{value}`, which did not pass
validation tests for the `{component_name}` component.

The expected schema for the `{component_property}` prop of the
`{component_name}` component is:

***************************************************************
{component_schema}
***************************************************************

The errors in validation are as follows:

"""


# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-arguments, too-many-locals
Expand All @@ -92,6 +116,7 @@ def __init__(
external_scripts=None,
external_stylesheets=None,
suppress_callback_exceptions=None,
suppress_validation_exceptions=None,
components_cache_max_age=None,
**kwargs):

Expand Down Expand Up @@ -128,6 +153,10 @@ def __init__(
'suppress_callback_exceptions',
suppress_callback_exceptions, env_configs, False
),
'suppress_validation_exceptions': _configs.get_config(
'suppress_validation_exceptions',
suppress_validation_exceptions, env_configs, False
),
'routes_pathname_prefix': routes_pathname_prefix,
'requests_pathname_prefix': requests_pathname_prefix,
'include_assets_files': _configs.get_config(
Expand Down Expand Up @@ -765,7 +794,7 @@ def _validate_callback(self, output, inputs, state):
output.component_id,
output.component_property).replace(' ', ''))

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

def _raise_invalid(bad_val, outer_val, bad_type, path, index=None,
Expand Down Expand Up @@ -901,13 +930,11 @@ def callback(self, output, inputs=[], state=[]):

def wrap_func(func):
@wraps(func)
def add_context(*args, **kwargs):

output_value = func(*args, **kwargs)
def add_context(validated_output):
response = {
'response': {
'props': {
output.component_property: output_value
output.component_property: validated_output
}
}
}
Expand All @@ -918,7 +945,10 @@ def add_context(*args, **kwargs):
cls=plotly.utils.PlotlyJSONEncoder
)
except TypeError:
self._validate_callback_output(output_value, output)
self._debug_callback_serialization_error(
validated_output,
output
)
raise exceptions.InvalidCallbackReturnValue('''
The callback for property `{property:s}`
of component `{id:s}` returned a value
Expand All @@ -935,6 +965,7 @@ def add_context(*args, **kwargs):
mimetype='application/json'
)

self.callback_map[callback_id]['func'] = func
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do that in two difference part ? Couldn't your additions in dispatch be in add_context ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We would have to pass namespace, component_id, component_property, to this since that info is available in dispatch. I originally thought this might clash with the other arguments (e.g. if you had a property called namespace) although I now think this would be fine, but it might make it difficult if we want to support passing props with keyword arguments in the future.

self.callback_map[callback_id]['callback'] = add_context

return add_context
Expand Down Expand Up @@ -978,7 +1009,62 @@ def dispatch(self):
c['id'] == component_registration['id']
][0])

return self.callback_map[target_id]['callback'](*args)
output_value = self.callback_map[target_id]['func'](*args)
Copy link
Contributor

Choose a reason for hiding this comment

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

The old call to the functions also had **kwargs, is that handled somewhere else or just not needed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it did, this was the last change to that line and it had *args


# Only validate if we get required information from renderer
# and validation is not turned off by user
if not self.config.suppress_validation_exceptions and \
'namespace' in output and 'type' in output:
# Python2.7 might make these keys and values unicode
namespace = str(output['namespace'])
component_type = str(output['type'])
component_id = str(output['id'])
component_property = str(output['property'])
callback_func_name = self.callback_map[target_id]['func'].__name__
self._validate_callback_output(namespace, component_type,
component_id, component_property,
callback_func_name,
args, output_value)

return self.callback_map[target_id]['callback'](output_value)

def _validate_callback_output(self, namespace, component_type,
component_id, component_property,
callback_func_name, args, value):
module = sys.modules[namespace]
component = getattr(module, component_type)
# pylint: disable=protected-access
validator = DashValidator({
component_property: component._schema.get(component_property, {})
})
valid = validator.validate({component_property: value})
if not valid:
error_message = _callback_validation_error_template.format(
component_property=component_property,
component_name=component.__name__,
component_id=component_id,
callback_func_name=callback_func_name,
args='({})'.format(", ".join(map(repr, args))),
Copy link
Contributor

Choose a reason for hiding this comment

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

😸

value=value,
component_schema=pprint.pformat(
component._schema[component_property]
)
)

raise exceptions.CallbackOutputValidationError(
generate_validation_error_message(
validator.errors,
0,
error_message
)
)
# Must also validate initialization of newly created components
if component_property == 'children':
if isinstance(value, Component):
value.validate()
for component in value.traverse():
if isinstance(component, Component):
component.validate()

def _validate_layout(self):
if self.layout is None:
Expand All @@ -995,6 +1081,11 @@ def _validate_layout(self):

component_ids = {layout_id} if layout_id else set()
for component in to_validate.traverse():
if (
not self.config.suppress_validation_exceptions and
isinstance(component, Component)
):
component.validate()
component_id = getattr(component, 'id', None)
if component_id and component_id in component_ids:
raise exceptions.DuplicateIdError(
Expand Down Expand Up @@ -1292,6 +1383,10 @@ def run_server(self,
dev_tools_silence_routes_logging,
)

if not debug:
# Do not throw validation exceptions in production.
self.config.suppress_validation_exceptions = True

if self._dev_tools.silence_routes_logging:
# Since it's silenced, the address don't show anymore.
host = flask_run_options.get('host', '127.0.0.1')
Expand Down
23 changes: 17 additions & 6 deletions dash/development/_py_components_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import copy
import os

from dash.development.base_component import _explicitize_args
from dash.development.base_component import (
_explicitize_args,
generate_property_schema
)
from dash.exceptions import NonExistentEventException
from ._all_keywords import python_keywords
from .base_component import Component


# pylint: disable=unused-argument
# pylint: disable=unused-argument,too-many-locals
def generate_class_string(typename, props, description, namespace):
"""
Dynamically generate class strings to have nicely formatted docstrings,
Expand Down Expand Up @@ -43,8 +46,12 @@ def generate_class_string(typename, props, description, namespace):
# it to be `null` or whether that was just the default value.
# The solution might be to deal with default values better although
# not all component authors will supply those.
c = '''class {typename}(Component):
c = '''\
schema = {schema}

class {typename}(Component):
"""{docstring}"""
_schema = schema
@_explicitize_args
def __init__(self, {default_argtext}):
self._prop_names = {list_of_valid_keys}
Expand All @@ -55,7 +62,6 @@ def __init__(self, {default_argtext}):
self.available_properties = {list_of_valid_keys}
self.available_wildcard_properties =\
{list_of_valid_wildcard_attr_prefixes}

_explicit_args = kwargs.pop('_explicit_args')
_locals = locals()
_locals.update(kwargs) # For wildcard attrs
Expand Down Expand Up @@ -85,7 +91,7 @@ def __init__(self, {default_argtext}):
default_argtext = "children=None, "
argtext = 'children=children, **args'
else:
default_argtext = ""
default_argtext = ''
argtext = '**args'
default_argtext += ", ".join(
[('{:s}=Component.REQUIRED'.format(p)
Expand All @@ -97,6 +103,10 @@ def __init__(self, {default_argtext}):
p != 'setProps'] + ["**kwargs"]
)
required_args = required_props(props)
schema = {
k: generate_property_schema(v)
for k, v in props.items() if not k.endswith("-*")
}
return c.format(
typename=typename,
namespace=namespace,
Expand All @@ -106,7 +116,8 @@ def __init__(self, {default_argtext}):
docstring=docstring,
default_argtext=default_argtext,
argtext=argtext,
required_props=required_args
required_props=required_args,
schema=schema
)


Expand Down
Loading