Skip to content
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

Fix __repr__, move it to the base component #492

Merged
merged 6 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 14 additions & 32 deletions dash/development/_py_components_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,16 @@ 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.')
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))
# 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,
Expand All @@ -109,7 +83,6 @@ def __repr__(self):
if 'children' in props:
prop_keys.remove('children')
default_argtext = "children=None, "
# pylint: disable=unused-variable
argtext = 'children=children, **args'
else:
default_argtext = ""
Expand All @@ -121,11 +94,20 @@ def __repr__(self):
for p in prop_keys
if not p.endswith("-*") and
p not in python_keywords and
p != 'setProps'] + ['**kwargs']
p != '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,
default_argtext=default_argtext,
argtext=argtext,
required_props=required_args
)
Copy link
Contributor

Choose a reason for hiding this comment

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

😸



def generate_class_file(typename, props, description, namespace):
Expand Down
29 changes: 29 additions & 0 deletions dash/development/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,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
Expand Down
23 changes: 0 additions & 23 deletions tests/development/metadata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,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)) + ')')