From 391367a80e0d01649ac3cff6458682ba6a66e3a6 Mon Sep 17 00:00:00 2001 From: Ryan Marren Date: Fri, 14 Dec 2018 11:07:38 -0500 Subject: [PATCH 1/5] Add __repr__ to base_component. --- dash/development/base_component.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/dash/development/base_component.py b/dash/development/base_component.py index ce6aec0741..2af95160da 100644 --- a/dash/development/base_component.py +++ b/dash/development/base_component.py @@ -276,6 +276,35 @@ def __len__(self): length = 1 return length + def __repr__(self): + # pylint: disable=no-member + props_with_values = [ + c for c in self._prop_names + if getattr(self, c, None) is not None + ] + [ + c for c in self.__dict__ + if any( + c.startswith(wc_attr) + for wc_attr in self._valid_wildcard_attributes + ) + ] + if any( + p != 'children' + for p in props_with_values + ): + props_string = ", ".join( + '{prop}={value}'.format( + prop=p, + value=repr(getattr(self, p)) + ) for p in props_with_values + ) + else: + props_string = repr(getattr(self, 'children', None)) + return "{type}({props_string})".format( + type=self._type, + props_string=props_string + ) + def _explicitize_args(func): # Python 2 From a1c8cd0290e60171b0b476221ed682d67aa7d894 Mon Sep 17 00:00:00 2001 From: Ryan Marren Date: Fri, 14 Dec 2018 11:09:52 -0500 Subject: [PATCH 2/5] Remove __repr__ from component generation. --- dash/development/_py_components_generation.py | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/dash/development/_py_components_generation.py b/dash/development/_py_components_generation.py index a5d9a4e05e..65dc76a07b 100644 --- a/dash/development/_py_components_generation.py +++ b/dash/development/_py_components_generation.py @@ -67,29 +67,6 @@ def __init__(self, {default_argtext}): raise TypeError( 'Required argument `' + k + '` was not specified.') super({typename}, self).__init__({argtext}) - - def __repr__(self): - if(any(getattr(self, c, None) is not None - for c in self._prop_names - if c is not self._prop_names[0]) - or any(getattr(self, c, None) is not None - for c in self.__dict__.keys() - if any(c.startswith(wc_attr) - for wc_attr in self._valid_wildcard_attributes))): - props_string = ', '.join([c+'='+repr(getattr(self, c, None)) - for c in self._prop_names - if getattr(self, c, None) is not None]) - wilds_string = ', '.join([c+'='+repr(getattr(self, c, None)) - for c in self.__dict__.keys() - if any([c.startswith(wc_attr) - for wc_attr in - self._valid_wildcard_attributes])]) - return ('{typename}(' + props_string + - (', ' + wilds_string if wilds_string != '' else '') + ')') - else: - return ( - '{typename}(' + - repr(getattr(self, self._prop_names[0], None)) + ')') ''' filtered_props = reorder_props(filter_props(props)) From 343efc5884d3a284592cf019cbe8f0f884b4c54c Mon Sep 17 00:00:00 2001 From: Ryan Marren Date: Fri, 14 Dec 2018 11:10:08 -0500 Subject: [PATCH 3/5] Use explicit arguments when calling string format. --- dash/development/_py_components_generation.py | 26 ++++++++++-------- tests/development/metadata_test.py | 27 ++----------------- 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/dash/development/_py_components_generation.py b/dash/development/_py_components_generation.py index 65dc76a07b..f6aab1553b 100644 --- a/dash/development/_py_components_generation.py +++ b/dash/development/_py_components_generation.py @@ -62,7 +62,7 @@ def __init__(self, {default_argtext}): _locals.update(kwargs) # For wildcard attrs args = {{k: _locals[k] for k in _explicit_args if k != 'children'}} - for k in {required_args}: + for k in {required_props}: if k not in args: raise TypeError( 'Required argument `' + k + '` was not specified.') @@ -70,24 +70,18 @@ def __init__(self, {default_argtext}): ''' filtered_props = reorder_props(filter_props(props)) - # pylint: disable=unused-variable - list_of_valid_wildcard_attr_prefixes = repr(parse_wildcards(props)) - # pylint: disable=unused-variable + wildcard_prefixes = repr(parse_wildcards(props)) list_of_valid_keys = repr(list(map(str, filtered_props.keys()))) - # pylint: disable=unused-variable docstring = create_docstring( component_name=typename, props=filtered_props, events=parse_events(props), description=description).replace('\r\n', '\n') - - # pylint: disable=unused-variable events = '[' + ', '.join(parse_events(props)) + ']' prop_keys = list(props.keys()) if 'children' in props: prop_keys.remove('children') default_argtext = "children=None, " - # pylint: disable=unused-variable argtext = 'children=children, **args' else: default_argtext = "" @@ -99,11 +93,21 @@ def __init__(self, {default_argtext}): for p in prop_keys if not p.endswith("-*") and p not in kwlist and - p not in ['dashEvents', 'fireEvent', 'setProps']] + ['**kwargs'] + p not in ['dashEvents', 'fireEvent', 'setProps']] + ["**kwargs"] ) - required_args = required_props(props) - return c.format(**locals()) + return c.format( + typename=typename, + namespace=namespace, + filtered_props=filtered_props, + list_of_valid_wildcard_attr_prefixes=wildcard_prefixes, + list_of_valid_keys=list_of_valid_keys, + docstring=docstring, + events=events, + default_argtext=default_argtext, + argtext=argtext, + required_props=required_args + ) def generate_class_file(typename, props, description, namespace): diff --git a/tests/development/metadata_test.py b/tests/development/metadata_test.py index 1074ff0e51..53d16d7f57 100644 --- a/tests/development/metadata_test.py +++ b/tests/development/metadata_test.py @@ -43,10 +43,10 @@ def __init__(self, children=None, optionalArray=Component.UNDEFINED, optionalBoo self._prop_names = ['children', 'optionalArray', 'optionalBool', 'optionalNumber', 'optionalObject', 'optionalString', 'optionalNode', 'optionalElement', 'optionalEnum', 'optionalUnion', 'optionalArrayOf', 'optionalObjectOf', 'optionalObjectWithShapeAndNestedDescription', 'optionalAny', 'customProp', 'customArrayProp', 'data-*', 'aria-*', 'in', 'id'] self._type = 'Table' self._namespace = 'TableComponents' - self._valid_wildcard_attributes = ['data-', 'aria-'] + self._valid_wildcard_attributes = ['data-', 'aria-'] self.available_events = ['restyle', 'relayout', 'click'] self.available_properties = ['children', 'optionalArray', 'optionalBool', 'optionalNumber', 'optionalObject', 'optionalString', 'optionalNode', 'optionalElement', 'optionalEnum', 'optionalUnion', 'optionalArrayOf', 'optionalObjectOf', 'optionalObjectWithShapeAndNestedDescription', 'optionalAny', 'customProp', 'customArrayProp', 'data-*', 'aria-*', 'in', 'id'] - self.available_wildcard_properties = ['data-', 'aria-'] + self.available_wildcard_properties = ['data-', 'aria-'] _explicit_args = kwargs.pop('_explicit_args') _locals = locals() @@ -58,26 +58,3 @@ def __init__(self, children=None, optionalArray=Component.UNDEFINED, optionalBoo raise TypeError( 'Required argument `' + k + '` was not specified.') super(Table, self).__init__(children=children, **args) - - def __repr__(self): - if(any(getattr(self, c, None) is not None - for c in self._prop_names - if c is not self._prop_names[0]) - or any(getattr(self, c, None) is not None - for c in self.__dict__.keys() - if any(c.startswith(wc_attr) - for wc_attr in self._valid_wildcard_attributes))): - props_string = ', '.join([c+'='+repr(getattr(self, c, None)) - for c in self._prop_names - if getattr(self, c, None) is not None]) - wilds_string = ', '.join([c+'='+repr(getattr(self, c, None)) - for c in self.__dict__.keys() - if any([c.startswith(wc_attr) - for wc_attr in - self._valid_wildcard_attributes])]) - return ('Table(' + props_string + - (', ' + wilds_string if wilds_string != '' else '') + ')') - else: - return ( - 'Table(' + - repr(getattr(self, self._prop_names[0], None)) + ')') From c722266fcb1f2c55828745effba08fbb0aa2cb6a Mon Sep 17 00:00:00 2001 From: Ryan Marren Date: Fri, 14 Dec 2018 11:21:10 -0500 Subject: [PATCH 4/5] Fix metadata_test --- tests/development/metadata_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/development/metadata_test.py b/tests/development/metadata_test.py index 53d16d7f57..b9f51529e4 100644 --- a/tests/development/metadata_test.py +++ b/tests/development/metadata_test.py @@ -43,10 +43,10 @@ def __init__(self, children=None, optionalArray=Component.UNDEFINED, optionalBoo self._prop_names = ['children', 'optionalArray', 'optionalBool', 'optionalNumber', 'optionalObject', 'optionalString', 'optionalNode', 'optionalElement', 'optionalEnum', 'optionalUnion', 'optionalArrayOf', 'optionalObjectOf', 'optionalObjectWithShapeAndNestedDescription', 'optionalAny', 'customProp', 'customArrayProp', 'data-*', 'aria-*', 'in', 'id'] self._type = 'Table' self._namespace = 'TableComponents' - self._valid_wildcard_attributes = ['data-', 'aria-'] + self._valid_wildcard_attributes = ['data-', 'aria-'] self.available_events = ['restyle', 'relayout', 'click'] self.available_properties = ['children', 'optionalArray', 'optionalBool', 'optionalNumber', 'optionalObject', 'optionalString', 'optionalNode', 'optionalElement', 'optionalEnum', 'optionalUnion', 'optionalArrayOf', 'optionalObjectOf', 'optionalObjectWithShapeAndNestedDescription', 'optionalAny', 'customProp', 'customArrayProp', 'data-*', 'aria-*', 'in', 'id'] - self.available_wildcard_properties = ['data-', 'aria-'] + self.available_wildcard_properties = ['data-', 'aria-'] _explicit_args = kwargs.pop('_explicit_args') _locals = locals() From 09f80c9b3f835af4819b040173ba560cba41bbbc Mon Sep 17 00:00:00 2001 From: t4rk1n Date: Mon, 11 Feb 2019 14:14:34 -0500 Subject: [PATCH 5/5] :pencil: Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54efb82c0c..5fbb8e87ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Fixed - Fix missing indentation for generated metadata.json [#600](https://github.com/plotly/dash/issues/600) - Fix missing component prop docstring error [#598](https://github.com/plotly/dash/issues/598) +- Moved `__repr__` to base component instead of being generated. [#492](https://github.com/plotly/dash/pull/492) ## [0.37.0] - 2019-02-11 ## Fixed