Skip to content

Commit

Permalink
Merge pull request #692 from plotly/exact
Browse files Browse the repository at this point in the history
support `exact` just like `shape`
  • Loading branch information
byronz authored Apr 17, 2019
2 parents f62ca14 + dcc9b4a commit ac10b2d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 18 deletions.
36 changes: 22 additions & 14 deletions dash/development/_py_components_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,24 @@ def create_prop_docstring(prop_name, type_object, required, description,

def map_js_to_py_types_prop_types(type_object):
"""Mapping from the PropTypes js type object to the Python type"""

def shape_or_exact():
return 'dict containing keys {}.\n{}'.format(
', '.join(
"'{}'".format(t) for t in list(type_object['value'].keys())
),
'Those keys have the following types:\n{}'.format(
'\n'.join(
create_prop_docstring(
prop_name=prop_name,
type_object=prop,
required=prop['required'],
description=prop.get('description', ''),
indent_num=1
) for prop_name, prop in
list(type_object['value'].items())))
)

return dict(
array=lambda: 'list',
bool=lambda: 'boolean',
Expand Down Expand Up @@ -483,25 +501,15 @@ def map_js_to_py_types_prop_types(type_object):
js_to_py_type(type_object['value'])),

# React's PropTypes.shape
shape=lambda: 'dict containing keys {}.\n{}'.format(
', '.join(
"'{}'".format(t)
for t in list(type_object['value'].keys())),
'Those keys have the following types:\n{}'.format(
'\n'.join(
create_prop_docstring(
prop_name=prop_name,
type_object=prop,
required=prop['required'],
description=prop.get('description', ''),
indent_num=1
) for prop_name, prop in
list(type_object['value'].items())))),
shape=shape_or_exact,
# React's PropTypes.exact
exact=shape_or_exact,
)


def map_js_to_py_types_flow_types(type_object):
"""Mapping from the Flow js types to the Python type"""

return dict(
array=lambda: 'list',
boolean=lambda: 'boolean',
Expand Down
37 changes: 37 additions & 0 deletions tests/development/metadata_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@
"required": false,
"description": ""
},
"optionalObjectWithExactAndNestedDescription": {
"type": {
"name": "exact",
"value": {
"color": {
"name": "string",
"required": false
},
"fontSize": {
"name": "number",
"required": false
},
"figure": {
"name": "shape",
"value": {
"data": {
"name": "arrayOf",
"value": {
"name": "object"
},
"description": "data is a collection of traces",
"required": false
},
"layout": {
"name": "object",
"description": "layout describes the rest of the figure",
"required": false
}
},
"description": "Figure is a plotly graph object",
"required": false
}
}
},
"required": false,
"description": ""
},
"optionalObjectWithShapeAndNestedDescription": {
"type": {
"name": "shape",
Expand Down
14 changes: 11 additions & 3 deletions tests/development/metadata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class Table(Component):
- optionalUnion (string | number; optional)
- optionalArrayOf (list; optional)
- optionalObjectOf (dict with strings as keys and values of type number; optional)
- optionalObjectWithExactAndNestedDescription (optional): . optionalObjectWithExactAndNestedDescription has the following type: dict containing keys 'color', 'fontSize', 'figure'.
Those keys have the following types:
- color (string; optional)
- fontSize (number; optional)
- figure (optional): Figure is a plotly graph object. figure has the following type: dict containing keys 'data', 'layout'.
Those keys have the following types:
- data (list; optional): data is a collection of traces
- layout (dict; optional): layout describes the rest of the figure
- optionalObjectWithShapeAndNestedDescription (optional): . optionalObjectWithShapeAndNestedDescription has the following type: dict containing keys 'color', 'fontSize', 'figure'.
Those keys have the following types:
- color (string; optional)
Expand All @@ -37,12 +45,12 @@ class Table(Component):
- in (string; optional)
- id (string; optional)"""
@_explicitize_args
def __init__(self, children=None, optionalArray=Component.UNDEFINED, optionalBool=Component.UNDEFINED, optionalFunc=Component.UNDEFINED, optionalNumber=Component.UNDEFINED, optionalObject=Component.UNDEFINED, optionalString=Component.UNDEFINED, optionalSymbol=Component.UNDEFINED, optionalNode=Component.UNDEFINED, optionalElement=Component.UNDEFINED, optionalMessage=Component.UNDEFINED, optionalEnum=Component.UNDEFINED, optionalUnion=Component.UNDEFINED, optionalArrayOf=Component.UNDEFINED, optionalObjectOf=Component.UNDEFINED, optionalObjectWithShapeAndNestedDescription=Component.UNDEFINED, optionalAny=Component.UNDEFINED, customProp=Component.UNDEFINED, customArrayProp=Component.UNDEFINED, id=Component.UNDEFINED, **kwargs):
self._prop_names = ['children', 'optionalArray', 'optionalBool', 'optionalNumber', 'optionalObject', 'optionalString', 'optionalNode', 'optionalElement', 'optionalEnum', 'optionalUnion', 'optionalArrayOf', 'optionalObjectOf', 'optionalObjectWithShapeAndNestedDescription', 'optionalAny', 'customProp', 'customArrayProp', 'data-*', 'aria-*', 'in', 'id']
def __init__(self, children=None, optionalArray=Component.UNDEFINED, optionalBool=Component.UNDEFINED, optionalFunc=Component.UNDEFINED, optionalNumber=Component.UNDEFINED, optionalObject=Component.UNDEFINED, optionalString=Component.UNDEFINED, optionalSymbol=Component.UNDEFINED, optionalNode=Component.UNDEFINED, optionalElement=Component.UNDEFINED, optionalMessage=Component.UNDEFINED, optionalEnum=Component.UNDEFINED, optionalUnion=Component.UNDEFINED, optionalArrayOf=Component.UNDEFINED, optionalObjectOf=Component.UNDEFINED, optionalObjectWithExactAndNestedDescription=Component.UNDEFINED, optionalObjectWithShapeAndNestedDescription=Component.UNDEFINED, optionalAny=Component.UNDEFINED, customProp=Component.UNDEFINED, customArrayProp=Component.UNDEFINED, id=Component.UNDEFINED, **kwargs):
self._prop_names = ['children', 'optionalArray', 'optionalBool', 'optionalNumber', 'optionalObject', 'optionalString', 'optionalNode', 'optionalElement', 'optionalEnum', 'optionalUnion', 'optionalArrayOf', 'optionalObjectOf', 'optionalObjectWithExactAndNestedDescription', 'optionalObjectWithShapeAndNestedDescription', 'optionalAny', 'customProp', 'customArrayProp', 'data-*', 'aria-*', 'in', 'id']
self._type = 'Table'
self._namespace = 'TableComponents'
self._valid_wildcard_attributes = ['data-', 'aria-']
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_properties = ['children', 'optionalArray', 'optionalBool', 'optionalNumber', 'optionalObject', 'optionalString', 'optionalNode', 'optionalElement', 'optionalEnum', 'optionalUnion', 'optionalArrayOf', 'optionalObjectOf', 'optionalObjectWithExactAndNestedDescription', 'optionalObjectWithShapeAndNestedDescription', 'optionalAny', 'customProp', 'customArrayProp', 'data-*', 'aria-*', 'in', 'id']
self.available_wildcard_properties = ['data-', 'aria-']

_explicit_args = kwargs.pop('_explicit_args')
Expand Down
35 changes: 34 additions & 1 deletion tests/development/test_base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ def test_call_signature(self):
'optionalUnion',
'optionalArrayOf',
'optionalObjectOf',
'optionalObjectWithExactAndNestedDescription',
'optionalObjectWithShapeAndNestedDescription',
'optionalAny',
'customProp',
Expand All @@ -737,7 +738,7 @@ def test_call_signature(self):
if hasattr(inspect, 'signature'):
self.assertEqual(
[str(x) for x in inspect.getargspec(__init__func).defaults],
['None'] + ['undefined'] * 19
['None'] + ['undefined'] * 20
)

def test_required_props(self):
Expand Down Expand Up @@ -794,6 +795,19 @@ def setUp(self):
['optionalObjectOf',
'dict with strings as keys and values of type number'],

['optionalObjectWithExactAndNestedDescription', '\n'.join([

"dict containing keys 'color', 'fontSize', 'figure'.",
"Those keys have the following types:",
" - color (string; optional)",
" - fontSize (number; optional)",
" - figure (optional): Figure is a plotly graph object. figure has the following type: dict containing keys 'data', 'layout'.", # noqa: E501
"Those keys have the following types:",
" - data (list; optional): data is a collection of traces",
" - layout (dict; optional): layout describes the rest of the figure" # noqa: E501

])],

['optionalObjectWithShapeAndNestedDescription', '\n'.join([

"dict containing keys 'color', 'fontSize', 'figure'.",
Expand Down Expand Up @@ -868,6 +882,25 @@ def assert_docstring(assertEqual, docstring):
"- optionalObjectOf (dict with strings as keys and values "
"of type number; optional)",

"- optionalObjectWithExactAndNestedDescription (optional): . "
"optionalObjectWithExactAndNestedDescription has the "
"following type: dict containing keys "
"'color', 'fontSize', 'figure'.",

"Those keys have the following types:",
" - color (string; optional)",
" - fontSize (number; optional)",

" - figure (optional): Figure is a plotly graph object. "
"figure has the following type: dict containing "
"keys 'data', 'layout'.",

"Those keys have the following types:",
" - data (list; optional): data is a collection of traces",

" - layout (dict; optional): layout describes "
"the rest of the figure",

"- optionalObjectWithShapeAndNestedDescription (optional): . "
"optionalObjectWithShapeAndNestedDescription has the "
"following type: dict containing keys "
Expand Down

0 comments on commit ac10b2d

Please sign in to comment.