Skip to content

Commit 13df0c5

Browse files
committed
feat: merging code generator work with latest changes on master
1. Modify `commands.py` to run code generation. 1. Remove comments from generated code. 1. Replaced named arguments in constructors with positional arguments. 1. Regenerate all code. Notes: The generated code is reformatted once again: this slightly increases size but makes it more readable. There is one flaky test: `tests/test_plotly_utils/validators/test_colorscale_validator.py::test_acceptance_named[Inferno_r]` It fails when the entire test suite is run but does *not* fail when only `test_colorscale_validator.py` is run (using Python 3.11.9). | branch | format | bytes | %age | | -------- | ------- | -------- | ---- | | master | .whl | 14803768 | | | codegen2 | .whl | 12215667 | -18% | | master | .tar.gz | 8100014 | | | codegen2 | .tar.gz | 6114458 | -24% |
1 parent 556dd53 commit 13df0c5

File tree

14,845 files changed

+244518
-251732
lines changed

Some content is hidden

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

14,845 files changed

+244518
-251732
lines changed

bin/get_size.py

-32
This file was deleted.

codegen/datatypes.py

+20-60
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ def build_datatype_py(node):
6969
"""
7070

7171
# Validate inputs
72-
# ---------------
7372
assert node.is_compound
7473

7574
# Handle template traces
76-
# ----------------------
75+
#
7776
# We want template trace/layout classes like
7877
# plotly.graph_objs.layout.template.data.Scatter to map to the
7978
# corresponding trace/layout class (e.g. plotly.graph_objs.Scatter).
@@ -85,17 +84,14 @@ def build_datatype_py(node):
8584
return "from plotly.graph_objs import Layout"
8685

8786
# Extract node properties
88-
# -----------------------
8987
undercase = node.name_undercase
9088
datatype_class = node.name_datatype_class
9189
literal_nodes = [n for n in node.child_literals if n.plotly_name in ["type"]]
9290

9391
# Initialze source code buffer
94-
# ----------------------------
9592
buffer = StringIO()
9693

9794
# Imports
98-
# -------
9995
buffer.write(
10096
f"from plotly.basedatatypes "
10197
f"import {node.name_base_datatype} as _{node.name_base_datatype}\n"
@@ -106,14 +102,13 @@ def build_datatype_py(node):
106102
buffer.write(f"from warnings import warn\n")
107103

108104
# Write class definition
109-
# ----------------------
110105
buffer.write(
111106
f"""
112107
113108
class {datatype_class}(_{node.name_base_datatype}):\n"""
114109
)
115110

116-
# ### Layout subplot properties ###
111+
### Layout subplot properties
117112
if datatype_class == "Layout":
118113
subplot_nodes = [
119114
node
@@ -168,17 +163,16 @@ def _subplot_re_match(self, prop):
168163
valid_props_list = sorted(
169164
[node.name_property for node in subtype_nodes + literal_nodes]
170165
)
166+
# class properties
171167
buffer.write(
172168
f"""
173-
# class properties
174-
# --------------------
175169
_parent_path_str = '{node.parent_path_str}'
176170
_path_str = '{node.path_str}'
177171
_valid_props = {{"{'", "'.join(valid_props_list)}"}}
178172
"""
179173
)
180174

181-
# ### Property definitions ###
175+
### Property definitions
182176
for subtype_node in subtype_nodes:
183177
if subtype_node.is_array_element:
184178
prop_type = (
@@ -199,7 +193,7 @@ def _subplot_re_match(self, prop):
199193
else:
200194
prop_type = get_typing_type(subtype_node.datatype, subtype_node.is_array_ok)
201195

202-
# #### Get property description ####
196+
#### Get property description ####
203197
raw_description = subtype_node.description
204198
property_description = "\n".join(
205199
textwrap.wrap(
@@ -210,12 +204,12 @@ def _subplot_re_match(self, prop):
210204
)
211205
)
212206

213-
# # #### Get validator description ####
207+
# #### Get validator description ####
214208
validator = subtype_node.get_validator_instance()
215209
if validator:
216210
validator_description = reindent_validator_description(validator, 4)
217211

218-
# #### Combine to form property docstring ####
212+
#### Combine to form property docstring ####
219213
if property_description.strip():
220214
property_docstring = f"""{property_description}
221215
@@ -225,12 +219,10 @@ def _subplot_re_match(self, prop):
225219
else:
226220
property_docstring = property_description
227221

228-
# #### Write get property ####
222+
#### Write get property ####
229223
buffer.write(
230224
f"""\
231225
232-
# {subtype_node.name_property}
233-
# {'-' * len(subtype_node.name_property)}
234226
@property
235227
def {subtype_node.name_property}(self):
236228
\"\"\"
@@ -243,7 +235,7 @@ def {subtype_node.name_property}(self):
243235
return self['{subtype_node.name_property}']"""
244236
)
245237

246-
# #### Write set property ####
238+
#### Write set property ####
247239
buffer.write(
248240
f"""
249241
@@ -252,24 +244,20 @@ def {subtype_node.name_property}(self, val):
252244
self['{subtype_node.name_property}'] = val\n"""
253245
)
254246

255-
# ### Literals ###
247+
### Literals
256248
for literal_node in literal_nodes:
257249
buffer.write(
258250
f"""\
259251
260-
# {literal_node.name_property}
261-
# {'-' * len(literal_node.name_property)}
262252
@property
263253
def {literal_node.name_property}(self):
264254
return self._props['{literal_node.name_property}']\n"""
265255
)
266256

267-
# ### Private properties descriptions ###
257+
### Private properties descriptions
268258
valid_props = {node.name_property for node in subtype_nodes}
269259
buffer.write(
270260
f"""
271-
# Self properties description
272-
# ---------------------------
273261
@property
274262
def _prop_descriptions(self):
275263
return \"\"\"\\"""
@@ -291,15 +279,15 @@ def _prop_descriptions(self):
291279
_mapped_properties = {repr(mapped_properties)}"""
292280
)
293281

294-
# ### Constructor ###
282+
### Constructor
295283
buffer.write(
296284
f"""
297285
def __init__(self"""
298286
)
299287

300288
add_constructor_params(buffer, subtype_nodes, prepend_extras=["arg"])
301289

302-
# ### Constructor Docstring ###
290+
### Constructor Docstring
303291
header = f"Construct a new {datatype_class} object"
304292
class_name = (
305293
f"plotly.graph_objs" f"{node.parent_dotpath_str}." f"{node.name_datatype_class}"
@@ -331,18 +319,17 @@ def __init__(self"""
331319
)
332320

333321
if datatype_class == "Layout":
334-
buffer.write(
335-
f"""
336322
# Override _valid_props for instance so that instance can mutate set
337323
# to support subplot properties (e.g. xaxis2)
324+
buffer.write(
325+
f"""
338326
self._valid_props = {{"{'", "'.join(valid_props_list)}"}}
339327
"""
340328
)
341329

330+
# Validate arg
342331
buffer.write(
343332
f"""
344-
# Validate arg
345-
# ------------
346333
if arg is None:
347334
arg = {{}}
348335
elif isinstance(arg, self.__class__):
@@ -355,35 +342,22 @@ def __init__(self"""
355342
constructor must be a dict or
356343
an instance of :class:`{class_name}`\"\"\")
357344
358-
# Handle skip_invalid
359-
# -------------------
360345
self._skip_invalid = kwargs.pop('skip_invalid', False)
361346
self._validate = kwargs.pop('_validate', True)
362347
"""
363348
)
364349

365-
buffer.write(
366-
f"""
367-
368-
# Populate data dict with properties
369-
# ----------------------------------"""
370-
)
350+
buffer.write("\n\n")
371351
for subtype_node in subtype_nodes:
372352
name_prop = subtype_node.name_property
373353
buffer.write(
374354
f"""
375355
self._init_provided('{name_prop}', arg, {name_prop})"""
376356
)
377357

378-
# ### Literals ###
358+
### Literals
379359
if literal_nodes:
380-
buffer.write(
381-
f"""
382-
383-
# Read-only literals
384-
# ------------------
385-
"""
386-
)
360+
buffer.write("\n\n")
387361
for literal_node in literal_nodes:
388362
lit_name = literal_node.name_property
389363
lit_val = repr(literal_node.node_data)
@@ -395,13 +369,7 @@ def __init__(self"""
395369

396370
buffer.write(
397371
f"""
398-
399-
# Process unknown kwargs
400-
# ----------------------
401372
self._process_kwargs(**dict(arg, **kwargs))
402-
403-
# Reset skip_invalid
404-
# ------------------
405373
self._skip_invalid = False
406374
"""
407375
)
@@ -420,7 +388,6 @@ def __init__(self"""
420388
)
421389

422390
# Return source string
423-
# --------------------
424391
return buffer.getvalue()
425392

426393

@@ -527,11 +494,9 @@ def add_docstring(
527494
528495
"""
529496
# Validate inputs
530-
# ---------------
531497
assert node.is_compound
532498

533499
# Build wrapped description
534-
# -------------------------
535500
node_description = node.description
536501
if node_description:
537502
description_lines = textwrap.wrap(
@@ -544,7 +509,6 @@ def add_docstring(
544509
node_description = "\n".join(description_lines) + "\n\n"
545510

546511
# Write header and description
547-
# ----------------------------
548512
buffer.write(
549513
f"""
550514
\"\"\"
@@ -555,7 +519,7 @@ def add_docstring(
555519
)
556520

557521
# Write parameter descriptions
558-
# ----------------------------
522+
559523
# Write any prepend extras
560524
for p, v in prepend_extras:
561525
v_wrapped = "\n".join(
@@ -594,7 +558,6 @@ def add_docstring(
594558
)
595559

596560
# Write return block and close docstring
597-
# --------------------------------------
598561
buffer.write(
599562
f"""
600563
@@ -623,16 +586,13 @@ def write_datatype_py(outdir, node):
623586
"""
624587

625588
# Build file path
626-
# ---------------
627589
# filepath = opath.join(outdir, "graph_objs", *node.parent_path_parts, "__init__.py")
628590
filepath = opath.join(
629591
outdir, "graph_objs", *node.parent_path_parts, "_" + node.name_undercase + ".py"
630592
)
631593

632594
# Generate source code
633-
# --------------------
634595
datatype_source = build_datatype_py(node)
635596

636597
# Write file
637-
# ----------
638598
write_source_py(datatype_source, filepath, leading_newlines=2)

codegen/validators.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ def __init__(self, plotly_name={params['plotly_name']},
5454
# ### Write constructor ###
5555
buffer.write(
5656
f"""
57-
super().__init__(plotly_name=plotly_name,
58-
parent_name=parent_name"""
57+
super().__init__(plotly_name, parent_name"""
5958
)
6059

6160
# Write out remaining constructor parameters
@@ -198,10 +197,7 @@ def __init__(self, plotly_name={params['plotly_name']},
198197
parent_name={params['parent_name']},
199198
**kwargs):
200199
201-
super().__init__(class_strs_map={params['class_strs_map']},
202-
plotly_name=plotly_name,
203-
parent_name=parent_name,
204-
**kwargs)"""
200+
super().__init__({params['class_strs_map']}, plotly_name, parent_name, **kwargs)"""
205201
)
206202

207203
return buffer.getvalue()

0 commit comments

Comments
 (0)