Skip to content

Commit f949f65

Browse files
committedFeb 5, 2025·
feat: adding type annotations to some constructor parameters
1. Add `from __future__ import annotations` as the first line of generated files so that modern Python type annotation syntax can be used in older versions of Python. 1. Add `from typing import Any` and `from numpy.types import NDArray` to all generated files rather than trying to figure out which of these imports are needed on a file-by-file basis. 1. Rename `get_typing_type` in `codegen/datatypes.py` to `get_python_type` to make purpose clearer. 1. Add additional optional parameter to `get_python_type` so that `compound` and `compound_array` types in the schema are converted to `None` rather than causing an exception. 1. Modify `codegen/datatypes.py` to add type annotations to constructor parameters. 1. Modify `codegen/figure.py` to add `bool` type to one figure constructor parameter. FIXME: figure out what type should actually be returned for `compound` and `compound_array`. FIXME: figure out what types to use for other standard parameters of figures generated by `codegen/figure.py`.
1 parent 3cb9303 commit f949f65

File tree

1,064 files changed

+22600
-19407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,064 files changed

+22600
-19407
lines changed
 

Diff for: ‎codegen/datatypes.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
]
1313

1414

15-
def get_typing_type(plotly_type, array_ok=False):
15+
def get_python_type(plotly_type, array_ok=False, compound_as_none=False):
1616
"""
1717
Get Python type corresponding to a valType string from the plotly schema
1818
@@ -28,7 +28,7 @@ def get_typing_type(plotly_type, array_ok=False):
2828
Python type string
2929
"""
3030
if plotly_type == "data_array":
31-
pytype = "numpy.ndarray"
31+
pytype = "NDArray"
3232
elif plotly_type == "info_array":
3333
pytype = "list"
3434
elif plotly_type == "colorlist":
@@ -43,11 +43,13 @@ def get_typing_type(plotly_type, array_ok=False):
4343
pytype = "int"
4444
elif plotly_type == "boolean":
4545
pytype = "bool"
46+
elif (plotly_type in ("compound", "compound_array")) and compound_as_none:
47+
pytype = None
4648
else:
4749
raise ValueError("Unknown plotly type: %s" % plotly_type)
4850

4951
if array_ok:
50-
return f"{pytype}|numpy.ndarray"
52+
return f"{pytype}|NDArray"
5153
else:
5254
return pytype
5355

@@ -96,11 +98,14 @@ def build_datatype_py(node):
9698

9799
# Imports
98100
# -------
101+
buffer.write("from __future__ import annotations\n")
102+
buffer.write("from typing import Any\n")
103+
buffer.write("from numpy.typing import NDArray\n")
99104
buffer.write(
100-
f"from plotly.basedatatypes "
105+
"from plotly.basedatatypes "
101106
f"import {node.name_base_datatype} as _{node.name_base_datatype}\n"
102107
)
103-
buffer.write(f"import copy as _copy\n")
108+
buffer.write("import copy as _copy\n")
104109

105110
if node.name_property in deprecated_mapbox_traces:
106111
buffer.write(f"from warnings import warn\n")
@@ -127,7 +132,7 @@ class {datatype_class}(_{node.name_base_datatype}):\n"""
127132
128133
import re
129134
_subplotid_prop_re = re.compile(
130-
'^(' + '|'.join(_subplotid_prop_names) + r')(\\d+)$')
135+
'^(' + '|'.join(_subplotid_prop_names) + r')(\d+)$')
131136
"""
132137
)
133138

@@ -197,7 +202,7 @@ def _subplot_re_match(self, prop):
197202
elif subtype_node.is_mapped:
198203
prop_type = ""
199204
else:
200-
prop_type = get_typing_type(subtype_node.datatype, subtype_node.is_array_ok)
205+
prop_type = get_python_type(subtype_node.datatype, array_ok=subtype_node.is_array_ok)
201206

202207
# #### Get property description ####
203208
raw_description = subtype_node.description
@@ -474,10 +479,11 @@ def add_constructor_params(
474479
{extra}=None"""
475480
)
476481

477-
for i, subtype_node in enumerate(subtype_nodes):
482+
for subtype_node in subtype_nodes:
483+
py_type = get_python_type(subtype_node.datatype, compound_as_none=True)
478484
buffer.write(
479485
f""",
480-
{subtype_node.name_property}=None"""
486+
{subtype_node.name_property}: {py_type}|None = None"""
481487
)
482488

483489
for extra in append_extras:

Diff for: ‎codegen/figure.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ def build_figure_py(
6161
trace_nodes = trace_node.child_compound_datatypes
6262

6363
# Write imports
64-
# -------------
65-
# ### Import base class ###
64+
buffer.write("from __future__ import annotations\n")
65+
buffer.write("from typing import Any\n")
66+
buffer.write("from numpy.typing import NDArray\n")
6667
buffer.write(f"from plotly.{base_package} import {base_classname}\n")
6768

6869
# Write class definition
@@ -82,7 +83,7 @@ class {fig_classname}({base_classname}):\n"""
8283
buffer.write(
8384
f"""
8485
def __init__(self, data=None, layout=None,
85-
frames=None, skip_invalid=False, **kwargs):
86+
frames=None, skip_invalid: bool = False, **kwargs):
8687
\"\"\"
8788
Create a new :class:{fig_classname} instance
8889

0 commit comments

Comments
 (0)
Please sign in to comment.