Skip to content

Commit 9324643

Browse files
authored
Merge pull request #492 from plotly/simplify-py-classes
Fix __repr__, move it to the base component
2 parents 8bfa031 + 09f80c9 commit 9324643

File tree

4 files changed

+44
-55
lines changed

4 files changed

+44
-55
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
## Fixed
33
- Fix missing indentation for generated metadata.json [#600](https://github.com/plotly/dash/issues/600)
44
- Fix missing component prop docstring error [#598](https://github.com/plotly/dash/issues/598)
5+
- Moved `__repr__` to base component instead of being generated. [#492](https://github.com/plotly/dash/pull/492)
56

67
## [0.37.0] - 2019-02-11
78
## Fixed

Diff for: dash/development/_py_components_generation.py

+14-32
Original file line numberDiff line numberDiff line change
@@ -61,42 +61,16 @@ def __init__(self, {default_argtext}):
6161
_locals.update(kwargs) # For wildcard attrs
6262
args = {{k: _locals[k] for k in _explicit_args if k != 'children'}}
6363
64-
for k in {required_args}:
64+
for k in {required_props}:
6565
if k not in args:
6666
raise TypeError(
6767
'Required argument `' + k + '` was not specified.')
6868
super({typename}, self).__init__({argtext})
69-
70-
def __repr__(self):
71-
if(any(getattr(self, c, None) is not None
72-
for c in self._prop_names
73-
if c is not self._prop_names[0])
74-
or any(getattr(self, c, None) is not None
75-
for c in self.__dict__.keys()
76-
if any(c.startswith(wc_attr)
77-
for wc_attr in self._valid_wildcard_attributes))):
78-
props_string = ', '.join([c+'='+repr(getattr(self, c, None))
79-
for c in self._prop_names
80-
if getattr(self, c, None) is not None])
81-
wilds_string = ', '.join([c+'='+repr(getattr(self, c, None))
82-
for c in self.__dict__.keys()
83-
if any([c.startswith(wc_attr)
84-
for wc_attr in
85-
self._valid_wildcard_attributes])])
86-
return ('{typename}(' + props_string +
87-
(', ' + wilds_string if wilds_string != '' else '') + ')')
88-
else:
89-
return (
90-
'{typename}(' +
91-
repr(getattr(self, self._prop_names[0], None)) + ')')
9269
'''
9370

9471
filtered_props = reorder_props(filter_props(props))
95-
# pylint: disable=unused-variable
96-
list_of_valid_wildcard_attr_prefixes = repr(parse_wildcards(props))
97-
# pylint: disable=unused-variable
72+
wildcard_prefixes = repr(parse_wildcards(props))
9873
list_of_valid_keys = repr(list(map(str, filtered_props.keys())))
99-
# pylint: disable=unused-variable
10074
docstring = create_docstring(
10175
component_name=typename,
10276
props=filtered_props,
@@ -109,7 +83,6 @@ def __repr__(self):
10983
if 'children' in props:
11084
prop_keys.remove('children')
11185
default_argtext = "children=None, "
112-
# pylint: disable=unused-variable
11386
argtext = 'children=children, **args'
11487
else:
11588
default_argtext = ""
@@ -121,11 +94,20 @@ def __repr__(self):
12194
for p in prop_keys
12295
if not p.endswith("-*") and
12396
p not in python_keywords and
124-
p != 'setProps'] + ['**kwargs']
97+
p != 'setProps'] + ["**kwargs"]
12598
)
126-
12799
required_args = required_props(props)
128-
return c.format(**locals())
100+
return c.format(
101+
typename=typename,
102+
namespace=namespace,
103+
filtered_props=filtered_props,
104+
list_of_valid_wildcard_attr_prefixes=wildcard_prefixes,
105+
list_of_valid_keys=list_of_valid_keys,
106+
docstring=docstring,
107+
default_argtext=default_argtext,
108+
argtext=argtext,
109+
required_props=required_args
110+
)
129111

130112

131113
def generate_class_file(typename, props, description, namespace):

Diff for: dash/development/base_component.py

+29
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,35 @@ def __len__(self):
272272
length = 1
273273
return length
274274

275+
def __repr__(self):
276+
# pylint: disable=no-member
277+
props_with_values = [
278+
c for c in self._prop_names
279+
if getattr(self, c, None) is not None
280+
] + [
281+
c for c in self.__dict__
282+
if any(
283+
c.startswith(wc_attr)
284+
for wc_attr in self._valid_wildcard_attributes
285+
)
286+
]
287+
if any(
288+
p != 'children'
289+
for p in props_with_values
290+
):
291+
props_string = ", ".join(
292+
'{prop}={value}'.format(
293+
prop=p,
294+
value=repr(getattr(self, p))
295+
) for p in props_with_values
296+
)
297+
else:
298+
props_string = repr(getattr(self, 'children', None))
299+
return "{type}({props_string})".format(
300+
type=self._type,
301+
props_string=props_string
302+
)
303+
275304

276305
def _explicitize_args(func):
277306
# Python 2

Diff for: tests/development/metadata_test.py

-23
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,3 @@ def __init__(self, children=None, optionalArray=Component.UNDEFINED, optionalBoo
5555
raise TypeError(
5656
'Required argument `' + k + '` was not specified.')
5757
super(Table, self).__init__(children=children, **args)
58-
59-
def __repr__(self):
60-
if(any(getattr(self, c, None) is not None
61-
for c in self._prop_names
62-
if c is not self._prop_names[0])
63-
or any(getattr(self, c, None) is not None
64-
for c in self.__dict__.keys()
65-
if any(c.startswith(wc_attr)
66-
for wc_attr in self._valid_wildcard_attributes))):
67-
props_string = ', '.join([c+'='+repr(getattr(self, c, None))
68-
for c in self._prop_names
69-
if getattr(self, c, None) is not None])
70-
wilds_string = ', '.join([c+'='+repr(getattr(self, c, None))
71-
for c in self.__dict__.keys()
72-
if any([c.startswith(wc_attr)
73-
for wc_attr in
74-
self._valid_wildcard_attributes])])
75-
return ('Table(' + props_string +
76-
(', ' + wilds_string if wilds_string != '' else '') + ')')
77-
else:
78-
return (
79-
'Table(' +
80-
repr(getattr(self, self._prop_names[0], None)) + ')')

0 commit comments

Comments
 (0)