Skip to content

Commit e5eed19

Browse files
author
Jon M. Mease
committed
Remove PEP484 type annotations from codegen properties (docstring instead)
1 parent 45ec084 commit e5eed19

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

codegen/datatypes.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ def get_typing_type(plotly_type, array_ok=False):
2121
str
2222
Python type string
2323
"""
24-
if plotly_type in ('data_array', 'info_array', 'colorlist'):
25-
pytype = 'List'
24+
if plotly_type == 'data_array':
25+
pytype = 'numpy.ndarray'
26+
elif plotly_type == 'info_array':
27+
pytype = 'list'
28+
elif plotly_type == 'colorlist':
29+
pytype = 'list'
2630
elif plotly_type in ('string', 'color', 'colorscale', 'subplotid'):
2731
pytype = 'str'
2832
elif plotly_type in ('enumerated', 'flaglist', 'any'):
2933
pytype = 'Any'
3034
elif plotly_type in ('number', 'angle'):
31-
pytype = 'Number'
35+
pytype = 'int|float'
3236
elif plotly_type == 'integer':
3337
pytype = 'int'
3438
elif plotly_type == 'boolean':
@@ -37,7 +41,7 @@ def get_typing_type(plotly_type, array_ok=False):
3741
raise ValueError('Unknown plotly type: %s' % plotly_type)
3842

3943
if array_ok:
40-
return f'Union[{pytype}, List[{pytype}]]'
44+
return f'{pytype}|numpy.ndarray'
4145
else:
4246
return pytype
4347

@@ -85,14 +89,6 @@ def build_datatype_py(node):
8589
f'from plotly.validators{node.parent_dotpath_str} import '
8690
f'{undercase} as v_{undercase}\n')
8791

88-
# ### Import type's graph_objs package with rename ###
89-
# If type has any compound children, then import that package that
90-
# holds them
91-
if node.child_compound_datatypes:
92-
buffer.write(
93-
f'from plotly.graph_objs{node.parent_dotpath_str} import '
94-
f'{undercase} as d_{undercase}\n')
95-
9692
# Write class definition
9793
# ----------------------
9894
buffer.write(f"""
@@ -104,13 +100,16 @@ class {datatype_class}({node.name_base_datatype}):\n""")
104100

105101
subtype_nodes = child_datatype_nodes
106102
for subtype_node in subtype_nodes:
107-
sub_datatype_class = subtype_node.name_datatype_class
108103
if subtype_node.is_array_element:
109-
prop_type = f'Tuple[d_{undercase}.{sub_datatype_class}]'
104+
prop_type = (f"tuple[plotly.graph_objs{node.dotpath_str}." +
105+
f"{subtype_node.name_datatype_class}]")
106+
110107
elif subtype_node.is_compound:
111-
prop_type = f'd_{undercase}.{sub_datatype_class}'
108+
prop_type = (f"plotly.graph_objs{node.dotpath_str}." +
109+
f"{subtype_node.name_datatype_class}")
112110
else:
113-
prop_type = get_typing_type(subtype_node.datatype)
111+
prop_type = get_typing_type(
112+
subtype_node.datatype, subtype_node.is_array_ok)
114113

115114
# #### Get property description ####
116115
raw_description = subtype_node.description
@@ -142,9 +141,13 @@ class {datatype_class}({node.name_base_datatype}):\n""")
142141
# {subtype_node.name_property}
143142
# {'-' * len(subtype_node.name_property)}
144143
@property
145-
def {subtype_node.name_property}(self) -> {prop_type}:
144+
def {subtype_node.name_property}(self):
146145
\"\"\"
147146
{property_docstring}
147+
148+
Returns
149+
-------
150+
{prop_type}
148151
\"\"\"
149152
return self['{subtype_node.name_property}']""")
150153

codegen/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,17 @@ def datatype(self) -> str:
461461
else:
462462
return 'literal'
463463

464+
@property
465+
def is_array_ok(self) -> bool:
466+
"""
467+
Return true if arrays of datatype are acceptable
468+
469+
Returns
470+
-------
471+
bool
472+
"""
473+
return self.node_data.get('arrayOk', False)
474+
464475
@property
465476
def is_compound(self) -> bool:
466477
"""

0 commit comments

Comments
 (0)