Skip to content

Commit f919a37

Browse files
Merge pull request #3708 from plotly/first_types
type annotations for chainable Figure methods
2 parents cbfa30d + 5dc67fa commit f919a37

File tree

1,001 files changed

+5021
-3423
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,001 files changed

+5021
-3423
lines changed

packages/python/plotly/codegen/datatypes.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class {datatype_class}(_{node.name_base_datatype}):\n"""
114114
buffer.write(
115115
f"""
116116
_subplotid_prop_names = {repr(subplot_names)}
117-
117+
118118
import re
119119
_subplotid_prop_re = re.compile(
120120
'^(' + '|'.join(_subplotid_prop_names) + r')(\d+)$')
@@ -147,7 +147,7 @@ def _subplotid_validators(self):
147147
from plotly.validators.layout import ({validator_csv})
148148
149149
return {subplot_dict_str}
150-
150+
151151
def _subplot_re_match(self, prop):
152152
return self._subplotid_prop_re.match(prop)
153153
"""
@@ -208,7 +208,7 @@ def _subplot_re_match(self, prop):
208208
# #### Combine to form property docstring ####
209209
if property_description.strip():
210210
property_docstring = f"""{property_description}
211-
211+
212212
{validator_description}"""
213213
else:
214214
property_docstring = f" {validator_description}"
@@ -342,8 +342,8 @@ def __init__(self"""
342342
arg = _copy.copy(arg)
343343
else:
344344
raise ValueError(\"\"\"\\
345-
The first argument to the {class_name}
346-
constructor must be a dict or
345+
The first argument to the {class_name}
346+
constructor must be a dict or
347347
an instance of :class:`{class_name}`\"\"\")
348348
349349
# Handle skip_invalid
@@ -389,11 +389,11 @@ def __init__(self"""
389389

390390
buffer.write(
391391
f"""
392-
392+
393393
# Process unknown kwargs
394394
# ----------------------
395395
self._process_kwargs(**dict(arg, **kwargs))
396-
396+
397397
# Reset skip_invalid
398398
# ------------------
399399
self._skip_invalid = False
@@ -429,7 +429,9 @@ def reindent_validator_description(validator, extra_indent):
429429
return ("\n" + " " * extra_indent).join(validator.description().strip().split("\n"))
430430

431431

432-
def add_constructor_params(buffer, subtype_nodes, prepend_extras=(), append_extras=()):
432+
def add_constructor_params(
433+
buffer, subtype_nodes, prepend_extras=(), append_extras=(), output_type=None
434+
):
433435
"""
434436
Write datatype constructor params to a buffer
435437
@@ -470,9 +472,12 @@ def add_constructor_params(buffer, subtype_nodes, prepend_extras=(), append_extr
470472
**kwargs"""
471473
)
472474
buffer.write(
473-
f"""
474-
):"""
475+
"""
476+
)"""
475477
)
478+
if output_type:
479+
buffer.write(f"-> '{output_type}'")
480+
buffer.write(":")
476481

477482

478483
def add_docstring(
@@ -525,7 +530,7 @@ def add_docstring(
525530
f"""
526531
\"\"\"
527532
{header}
528-
533+
529534
{node_description} Parameters
530535
----------"""
531536
)

packages/python/plotly/codegen/figure.py

+93-18
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
from io import StringIO
22
from os import path as opath
33

4-
from _plotly_utils.basevalidators import (
5-
BaseDataValidator,
6-
CompoundValidator,
7-
CompoundArrayValidator,
8-
)
94
from codegen.datatypes import (
105
reindent_validator_description,
116
add_constructor_params,
127
add_docstring,
138
)
14-
from codegen.utils import PlotlyNode, write_source_py
9+
from codegen.utils import write_source_py
1510

1611
import inflect
12+
from plotly.basedatatypes import BaseFigure
1713

1814

1915
def build_figure_py(
@@ -118,6 +114,82 @@ def __init__(self, data=None, layout=None,
118114
"""
119115
)
120116

117+
def add_wrapper(wrapped_name, full_params, param_list):
118+
buffer.write(
119+
f"""
120+
def {wrapped_name}(self, {full_params}) -> "{fig_classname}":
121+
'''
122+
{getattr(BaseFigure, wrapped_name).__doc__}
123+
'''
124+
return super({fig_classname}, self).{wrapped_name}({param_list})
125+
"""
126+
)
127+
128+
add_wrapper(
129+
"update",
130+
"dict1=None, overwrite=False, **kwargs",
131+
"dict1, overwrite, **kwargs",
132+
)
133+
134+
add_wrapper(
135+
"update_traces",
136+
"patch=None, selector=None, row=None, col=None, secondary_y=None, overwrite=False, **kwargs",
137+
"patch, selector, row, col, secondary_y, overwrite, **kwargs",
138+
)
139+
140+
add_wrapper(
141+
"update_layout",
142+
"dict1=None, overwrite=False, **kwargs",
143+
"dict1, overwrite, **kwargs",
144+
)
145+
146+
add_wrapper(
147+
"for_each_trace",
148+
"fn, selector=None, row=None, col=None, secondary_y=None",
149+
"fn, selector, row, col, secondary_y",
150+
)
151+
152+
add_wrapper(
153+
"add_trace",
154+
"trace, row=None, col=None, secondary_y=None, exclude_empty_subplots=False",
155+
"trace, row, col, secondary_y, exclude_empty_subplots",
156+
)
157+
158+
add_wrapper(
159+
"add_traces",
160+
"data,rows=None,cols=None,secondary_ys=None,exclude_empty_subplots=False",
161+
"data,rows,cols,secondary_ys,exclude_empty_subplots",
162+
)
163+
164+
add_wrapper(
165+
"add_vline",
166+
'x,row="all",col="all",exclude_empty_subplots=True,annotation=None,**kwargs',
167+
"x,row,col,exclude_empty_subplots,annotation,**kwargs",
168+
)
169+
170+
add_wrapper(
171+
"add_hline",
172+
'y,row="all",col="all",exclude_empty_subplots=True,annotation=None,**kwargs',
173+
"y,row,col,exclude_empty_subplots,annotation,**kwargs",
174+
)
175+
176+
add_wrapper(
177+
"add_vrect",
178+
'x0,x1,row="all",col="all",exclude_empty_subplots=True,annotation=None,**kwargs',
179+
"x0,x1,row,col,exclude_empty_subplots,annotation,**kwargs",
180+
)
181+
182+
add_wrapper(
183+
"add_hrect",
184+
'y0,y1,row="all",col="all",exclude_empty_subplots=True,annotation=None,**kwargs',
185+
"y0,y1,row,col,exclude_empty_subplots,annotation,**kwargs",
186+
)
187+
add_wrapper(
188+
"set_subplots",
189+
"rows=None, cols=None, **make_subplots_args",
190+
"rows, cols, **make_subplots_args",
191+
)
192+
121193
# ### add_trace methods for each trace type ###
122194
for trace_node in trace_nodes:
123195

@@ -136,7 +208,10 @@ def add_{trace_node.plotly_name}(self"""
136208
if include_secondary_y:
137209
param_extras.append("secondary_y")
138210
add_constructor_params(
139-
buffer, trace_node.child_datatypes, append_extras=param_extras
211+
buffer,
212+
trace_node.child_datatypes,
213+
append_extras=param_extras,
214+
output_type=fig_classname,
140215
)
141216

142217
# #### Docstring ####
@@ -193,15 +268,15 @@ def add_{trace_node.plotly_name}(self"""
193268
"""
194269
)
195270

196-
for i, subtype_node in enumerate(trace_node.child_datatypes):
271+
for _, subtype_node in enumerate(trace_node.child_datatypes):
197272
subtype_prop_name = subtype_node.name_property
198273
buffer.write(
199274
f"""
200275
{subtype_prop_name}={subtype_prop_name},"""
201276
)
202277

203278
buffer.write(
204-
f"""
279+
"""
205280
**kwargs)"""
206281
)
207282

@@ -226,7 +301,7 @@ def add_{trace_node.plotly_name}(self"""
226301
if singular_name == "yaxis":
227302
secondary_y_1 = ", secondary_y=None"
228303
secondary_y_2 = ", secondary_y=secondary_y"
229-
secondary_y_docstring = f"""
304+
secondary_y_docstring = """
230305
secondary_y: boolean or None (default None)
231306
* If True, only select yaxis objects associated with the secondary
232307
y-axis of the subplot.
@@ -283,7 +358,7 @@ def select_{plural_name}(
283358
'{singular_name}', selector, row, col{secondary_y_2})
284359
285360
def for_each_{singular_name}(
286-
self, fn, selector=None, row=None, col=None{secondary_y_1}):
361+
self, fn, selector=None, row=None, col=None{secondary_y_1}) -> '{fig_classname}':
287362
\"\"\"
288363
Apply a function to all {singular_name} objects that satisfy the
289364
specified selection criteria
@@ -311,7 +386,7 @@ def for_each_{singular_name}(
311386
Returns
312387
-------
313388
self
314-
Returns the Figure object that the method was called on
389+
Returns the {fig_classname} object that the method was called on
315390
\"\"\"
316391
for obj in self.select_{plural_name}(
317392
selector=selector, row=row, col=col{secondary_y_2}):
@@ -325,7 +400,7 @@ def update_{plural_name}(
325400
selector=None,
326401
overwrite=False,
327402
row=None, col=None{secondary_y_1},
328-
**kwargs):
403+
**kwargs) -> '{fig_classname}':
329404
\"\"\"
330405
Perform a property update operation on all {singular_name} objects
331406
that satisfy the specified selection criteria
@@ -363,7 +438,7 @@ def update_{plural_name}(
363438
Returns
364439
-------
365440
self
366-
Returns the Figure object that the method was called on
441+
Returns the {fig_classname} object that the method was called on
367442
\"\"\"
368443
for obj in self.select_{plural_name}(
369444
selector=selector, row=row, col=col{secondary_y_2}):
@@ -477,7 +552,7 @@ def for_each_{method_prefix}{singular_name}(
477552
Returns
478553
-------
479554
self
480-
Returns the Figure object that the method was called on
555+
Returns the {fig_classname} object that the method was called on
481556
\"\"\"
482557
for obj in self._select_annotations_like(
483558
prop='{plural_name}',
@@ -498,7 +573,7 @@ def update_{method_prefix}{plural_name}(
498573
col=None,
499574
secondary_y=None,
500575
**kwargs
501-
):
576+
) -> '{fig_classname}':
502577
\"\"\"
503578
Perform a property update operation on all {plural_name} that satisfy the
504579
specified selection criteria
@@ -545,7 +620,7 @@ def update_{method_prefix}{plural_name}(
545620
Returns
546621
-------
547622
self
548-
Returns the Figure object that the method was called on
623+
Returns the {fig_classname} object that the method was called on
549624
\"\"\"
550625
for obj in self._select_annotations_like(
551626
prop='{plural_name}',
@@ -610,7 +685,7 @@ def add_{method_prefix}{singular_name}(self"""
610685
"""
611686
)
612687

613-
for i, subtype_node in enumerate(node.child_datatypes):
688+
for _, subtype_node in enumerate(node.child_datatypes):
614689
subtype_prop_name = subtype_node.name_property
615690
buffer.write(
616691
f"""

0 commit comments

Comments
 (0)