Skip to content

Commit f949f65

Browse files
committed
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

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:

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

plotly/graph_objs/_bar.py

+91-88
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22

3+
from __future__ import annotations
4+
from typing import Any
5+
from numpy.typing import NDArray
36
from plotly.basedatatypes import BaseTraceType as _BaseTraceType
47
import copy as _copy
58

@@ -48,7 +51,7 @@ def base(self):
4851
4952
Returns
5053
-------
51-
Any|numpy.ndarray
54+
Any|NDArray
5255
"""
5356
return self['base']
5457

@@ -136,7 +139,7 @@ def customdata(self):
136139
137140
Returns
138141
-------
139-
numpy.ndarray
142+
NDArray
140143
"""
141144
return self['customdata']
142145

@@ -265,7 +268,7 @@ def hoverinfo(self):
265268
266269
Returns
267270
-------
268-
Any|numpy.ndarray
271+
Any|NDArray
269272
"""
270273
return self['hoverinfo']
271274

@@ -353,7 +356,7 @@ def hovertemplate(self):
353356
354357
Returns
355358
-------
356-
str|numpy.ndarray
359+
str|NDArray
357360
"""
358361
return self['hovertemplate']
359362

@@ -400,7 +403,7 @@ def hovertext(self):
400403
401404
Returns
402405
-------
403-
str|numpy.ndarray
406+
str|NDArray
404407
"""
405408
return self['hovertext']
406409

@@ -443,7 +446,7 @@ def ids(self):
443446
444447
Returns
445448
-------
446-
numpy.ndarray
449+
NDArray
447450
"""
448451
return self['ids']
449452

@@ -674,7 +677,7 @@ def meta(self):
674677
675678
Returns
676679
-------
677-
Any|numpy.ndarray
680+
Any|NDArray
678681
"""
679682
return self['meta']
680683

@@ -739,7 +742,7 @@ def offset(self):
739742
740743
Returns
741744
-------
742-
int|float|numpy.ndarray
745+
int|float|NDArray
743746
"""
744747
return self['offset']
745748

@@ -961,7 +964,7 @@ def text(self):
961964
962965
Returns
963966
-------
964-
str|numpy.ndarray
967+
str|NDArray
965968
"""
966969
return self['text']
967970

@@ -1038,7 +1041,7 @@ def textposition(self):
10381041
10391042
Returns
10401043
-------
1041-
Any|numpy.ndarray
1044+
Any|NDArray
10421045
"""
10431046
return self['textposition']
10441047

@@ -1114,7 +1117,7 @@ def texttemplate(self):
11141117
11151118
Returns
11161119
-------
1117-
str|numpy.ndarray
1120+
str|NDArray
11181121
"""
11191122
return self['texttemplate']
11201123

@@ -1255,7 +1258,7 @@ def width(self):
12551258
12561259
Returns
12571260
-------
1258-
int|float|numpy.ndarray
1261+
int|float|NDArray
12591262
"""
12601263
return self['width']
12611264

@@ -1295,7 +1298,7 @@ def x(self):
12951298
12961299
Returns
12971300
-------
1298-
numpy.ndarray
1301+
NDArray
12991302
"""
13001303
return self['x']
13011304

@@ -1503,7 +1506,7 @@ def y(self):
15031506
15041507
Returns
15051508
-------
1506-
numpy.ndarray
1509+
NDArray
15071510
"""
15081511
return self['y']
15091512

@@ -2107,80 +2110,80 @@ def _prop_descriptions(self):
21072110
"""
21082111
def __init__(self,
21092112
arg=None,
2110-
alignmentgroup=None,
2111-
base=None,
2112-
basesrc=None,
2113-
cliponaxis=None,
2114-
constraintext=None,
2115-
customdata=None,
2116-
customdatasrc=None,
2117-
dx=None,
2118-
dy=None,
2119-
error_x=None,
2120-
error_y=None,
2121-
hoverinfo=None,
2122-
hoverinfosrc=None,
2123-
hoverlabel=None,
2124-
hovertemplate=None,
2125-
hovertemplatesrc=None,
2126-
hovertext=None,
2127-
hovertextsrc=None,
2128-
ids=None,
2129-
idssrc=None,
2130-
insidetextanchor=None,
2131-
insidetextfont=None,
2132-
legend=None,
2133-
legendgroup=None,
2134-
legendgrouptitle=None,
2135-
legendrank=None,
2136-
legendwidth=None,
2137-
marker=None,
2138-
meta=None,
2139-
metasrc=None,
2140-
name=None,
2141-
offset=None,
2142-
offsetgroup=None,
2143-
offsetsrc=None,
2144-
opacity=None,
2145-
orientation=None,
2146-
outsidetextfont=None,
2147-
selected=None,
2148-
selectedpoints=None,
2149-
showlegend=None,
2150-
stream=None,
2151-
text=None,
2152-
textangle=None,
2153-
textfont=None,
2154-
textposition=None,
2155-
textpositionsrc=None,
2156-
textsrc=None,
2157-
texttemplate=None,
2158-
texttemplatesrc=None,
2159-
uid=None,
2160-
uirevision=None,
2161-
unselected=None,
2162-
visible=None,
2163-
width=None,
2164-
widthsrc=None,
2165-
x=None,
2166-
x0=None,
2167-
xaxis=None,
2168-
xcalendar=None,
2169-
xhoverformat=None,
2170-
xperiod=None,
2171-
xperiod0=None,
2172-
xperiodalignment=None,
2173-
xsrc=None,
2174-
y=None,
2175-
y0=None,
2176-
yaxis=None,
2177-
ycalendar=None,
2178-
yhoverformat=None,
2179-
yperiod=None,
2180-
yperiod0=None,
2181-
yperiodalignment=None,
2182-
ysrc=None,
2183-
zorder=None,
2113+
alignmentgroup: str|None = None,
2114+
base: Any|None = None,
2115+
basesrc: str|None = None,
2116+
cliponaxis: bool|None = None,
2117+
constraintext: Any|None = None,
2118+
customdata: NDArray|None = None,
2119+
customdatasrc: str|None = None,
2120+
dx: int|float|None = None,
2121+
dy: int|float|None = None,
2122+
error_x: None|None = None,
2123+
error_y: None|None = None,
2124+
hoverinfo: Any|None = None,
2125+
hoverinfosrc: str|None = None,
2126+
hoverlabel: None|None = None,
2127+
hovertemplate: str|None = None,
2128+
hovertemplatesrc: str|None = None,
2129+
hovertext: str|None = None,
2130+
hovertextsrc: str|None = None,
2131+
ids: NDArray|None = None,
2132+
idssrc: str|None = None,
2133+
insidetextanchor: Any|None = None,
2134+
insidetextfont: None|None = None,
2135+
legend: str|None = None,
2136+
legendgroup: str|None = None,
2137+
legendgrouptitle: None|None = None,
2138+
legendrank: int|float|None = None,
2139+
legendwidth: int|float|None = None,
2140+
marker: None|None = None,
2141+
meta: Any|None = None,
2142+
metasrc: str|None = None,
2143+
name: str|None = None,
2144+
offset: int|float|None = None,
2145+
offsetgroup: str|None = None,
2146+
offsetsrc: str|None = None,
2147+
opacity: int|float|None = None,
2148+
orientation: Any|None = None,
2149+
outsidetextfont: None|None = None,
2150+
selected: None|None = None,
2151+
selectedpoints: Any|None = None,
2152+
showlegend: bool|None = None,
2153+
stream: None|None = None,
2154+
text: str|None = None,
2155+
textangle: int|float|None = None,
2156+
textfont: None|None = None,
2157+
textposition: Any|None = None,
2158+
textpositionsrc: str|None = None,
2159+
textsrc: str|None = None,
2160+
texttemplate: str|None = None,
2161+
texttemplatesrc: str|None = None,
2162+
uid: str|None = None,
2163+
uirevision: Any|None = None,
2164+
unselected: None|None = None,
2165+
visible: Any|None = None,
2166+
width: int|float|None = None,
2167+
widthsrc: str|None = None,
2168+
x: NDArray|None = None,
2169+
x0: Any|None = None,
2170+
xaxis: str|None = None,
2171+
xcalendar: Any|None = None,
2172+
xhoverformat: str|None = None,
2173+
xperiod: Any|None = None,
2174+
xperiod0: Any|None = None,
2175+
xperiodalignment: Any|None = None,
2176+
xsrc: str|None = None,
2177+
y: NDArray|None = None,
2178+
y0: Any|None = None,
2179+
yaxis: str|None = None,
2180+
ycalendar: Any|None = None,
2181+
yhoverformat: str|None = None,
2182+
yperiod: Any|None = None,
2183+
yperiod0: Any|None = None,
2184+
yperiodalignment: Any|None = None,
2185+
ysrc: str|None = None,
2186+
zorder: int|None = None,
21842187
**kwargs
21852188
):
21862189
"""

0 commit comments

Comments
 (0)