Skip to content

Commit 6a64e35

Browse files
committed
WIP optional property validation
1 parent 9573384 commit 6a64e35

File tree

843 files changed

+3766
-108
lines changed

Some content is hidden

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

843 files changed

+3766
-108
lines changed

packages/python/plotly/_plotly_utils/basevalidators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2569,7 +2569,7 @@ def __init__(
25692569
)
25702570

25712571
self.class_strs_map = class_strs_map
2572-
self._class_map = None
2572+
self._class_map = {}
25732573
self.set_uid = set_uid
25742574

25752575
def description(self):

packages/python/plotly/codegen/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ def perform_codegen():
267267
root_alls = []
268268

269269
# TODO: come back to FigureWidget
270-
optional_figure_widget_import = ""
270+
optional_figure_widget_import = """
271+
from .._validate import validate
272+
"""
271273
# optional_figure_widget_import = f"""
272274
#
273275
# try:

packages/python/plotly/codegen/datatypes.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ def __init__(self"""
309309

310310
buffer.write(f"""
311311
super({datatype_class}, self).__init__('{node.name_property}')
312+
313+
if '_parent' in kwargs:
314+
self._parent = kwargs['_parent']
315+
return
312316
""")
313317

314318
if datatype_class == "Layout":
@@ -354,27 +358,23 @@ def __init__(self"""
354358
self['{name_prop}'] = _v"""
355359
)
356360

357-
# # ### Literals ###
358-
# if literal_nodes:
359-
# buffer.write(
360-
# f"""
361-
#
362-
# # Read-only literals
363-
# # ------------------
364-
# from _plotly_utils.basevalidators import LiteralValidator"""
365-
# )
366-
# for literal_node in literal_nodes:
367-
# lit_name = literal_node.name_property
368-
# lit_parent = literal_node.parent_path_str
369-
# lit_val = repr(literal_node.node_data)
370-
# buffer.write(
371-
# f"""
372-
# self._props['{lit_name}'] = {lit_val}
373-
# self._validators['{lit_name}'] =\
374-
# LiteralValidator(plotly_name='{lit_name}',\
375-
# parent_name='{lit_parent}', val={lit_val})
376-
# arg.pop('{lit_name}', None)"""
377-
# )
361+
# ### Literals ###
362+
if literal_nodes:
363+
buffer.write(
364+
f"""
365+
366+
# Read-only literals
367+
# ------------------
368+
"""
369+
)
370+
for literal_node in literal_nodes:
371+
lit_name = literal_node.name_property
372+
lit_val = repr(literal_node.node_data)
373+
buffer.write(
374+
f"""
375+
self._props['{lit_name}'] = {lit_val}
376+
arg.pop('{lit_name}', None)"""
377+
)
378378

379379
buffer.write(
380380
f"""

packages/python/plotly/plotly/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@
7777
# from plotly import _docstring_gen
7878

7979
# TODO: Set default template here to make sure import process is complete
80-
# io.templates._default = "plotly"
80+
from plotly.io import templates
81+
templates._default = "plotly"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class validate(object):
2+
_should_validate = True
3+
4+
def __init__(self, should_validate):
5+
self._old = validate._should_validate
6+
validate._should_validate = should_validate
7+
8+
def __enter__(self):
9+
pass
10+
11+
def __exit__(self, exc_type, exc_val, exc_tb):
12+
validate._should_validate = self._old

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from copy import deepcopy, copy
1111

1212
from _plotly_utils.utils import _natural_sort_strings
13+
from plotly._validate import validate
1314
from .optional_imports import get_module
1415

1516
# Create Undefined sentinel value
@@ -1933,9 +1934,11 @@ def _init_child_props(self, child):
19331934
def _initialize_layout_template(self):
19341935
import plotly.io as pio
19351936

1936-
if self._layout_obj.template is None:
1937+
if self._layout_obj._props.get('template', None) is None:
19371938
if pio.templates.default is not None:
1938-
self._layout_obj.template = pio.templates.default
1939+
with validate(False):
1940+
template_dict = pio.templates[pio.templates.default].to_plotly_json()
1941+
self._layout_obj.template = template_dict
19391942
else:
19401943
self._layout_obj.template = None
19411944

@@ -3021,6 +3024,9 @@ def _process_kwargs(self, **kwargs):
30213024
if k in self:
30223025
# e.g. underscore kwargs like marker_line_color
30233026
self[k] = v
3027+
elif not validate._should_validate:
3028+
# Set extra property as-is
3029+
self[k] = v
30243030
else:
30253031
invalid_kwargs[k] = v
30263032

@@ -3338,14 +3344,17 @@ def __getitem__(self, prop):
33383344

33393345
if isinstance(validator, CompoundValidator):
33403346
if self._compound_props.get(prop, None) is None:
3341-
self[prop] = {}
3342-
# self._compound_props[prop] = validator.validate_coerce(None)
3347+
# Init compound objects
3348+
self._compound_props[prop] = validator.data_class(_parent=self)
33433349

33443350
return validator.present(self._compound_props[prop])
33453351
elif isinstance(validator, CompoundArrayValidator):
33463352
if self._compound_array_props.get(prop, None) is None:
3347-
self[prop] = []
3348-
# self._compound_array_props[prop] = validator.validate_coerce(None)
3353+
# Init list of compound objects
3354+
self._compound_array_props[prop] = [
3355+
validator.data_class(_parent=self)
3356+
for _ in self._props.get(prop, [])
3357+
]
33493358

33503359
return validator.present(self._compound_array_props[prop])
33513360
elif self._props is not None and prop in self._props:
@@ -3450,24 +3459,29 @@ def __setitem__(self, prop, value):
34503459
# ### Unwrap scalar tuple ###
34513460
prop = prop[0]
34523461

3453-
# ### Validate prop ###
3454-
if prop not in self._valid_props:
3455-
self._raise_on_invalid_property_error(prop)
3462+
if validate._should_validate:
3463+
# ### Validate prop ###
3464+
if prop not in self._valid_props:
3465+
self._raise_on_invalid_property_error(prop)
34563466

3457-
# ### Get validator for this property ###
3458-
validator = self._get_validator(prop)
3467+
# ### Get validator for this property ###
3468+
validator = self._get_validator(prop)
34593469

3460-
# ### Handle compound property ###
3461-
if isinstance(validator, CompoundValidator):
3462-
self._set_compound_prop(prop, value)
3470+
# ### Handle compound property ###
3471+
if isinstance(validator, CompoundValidator):
3472+
self._set_compound_prop(prop, value)
34633473

3464-
# ### Handle compound array property ###
3465-
elif isinstance(validator, (CompoundArrayValidator, BaseDataValidator)):
3466-
self._set_array_prop(prop, value)
3474+
# ### Handle compound array property ###
3475+
elif isinstance(validator, (CompoundArrayValidator, BaseDataValidator)):
3476+
self._set_array_prop(prop, value)
34673477

3468-
# ### Handle simple property ###
3478+
# ### Handle simple property ###
3479+
else:
3480+
self._set_prop(prop, value)
34693481
else:
3470-
self._set_prop(prop, value)
3482+
# Make sure properties dict is initialized
3483+
self._init_props()
3484+
self._props[prop] = value
34713485

34723486
# Handle non-scalar case
34733487
# ----------------------

packages/python/plotly/plotly/graph_objs/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,6 @@
257257
"._deprecations.Histogram2dcontour",
258258
],
259259
)
260+
261+
262+
from .._validate import validate

packages/python/plotly/plotly/graph_objs/_area.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,10 @@ def __init__(
872872
"""
873873
super(Area, self).__init__("area")
874874

875+
if "_parent" in kwargs:
876+
self._parent = kwargs["_parent"]
877+
return
878+
875879
# Validate arg
876880
# ------------
877881
if arg is None:
@@ -983,6 +987,12 @@ def __init__(
983987
if _v is not None:
984988
self["visible"] = _v
985989

990+
# Read-only literals
991+
# ------------------
992+
993+
self._props["type"] = "area"
994+
arg.pop("type", None)
995+
986996
# Process unknown kwargs
987997
# ----------------------
988998
self._process_kwargs(**dict(arg, **kwargs))

packages/python/plotly/plotly/graph_objs/_bar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,10 @@ def __init__(
26262626
"""
26272627
super(Bar, self).__init__("bar")
26282628

2629+
if "_parent" in kwargs:
2630+
self._parent = kwargs["_parent"]
2631+
return
2632+
26292633
# Validate arg
26302634
# ------------
26312635
if arg is None:
@@ -2909,6 +2913,12 @@ def __init__(
29092913
if _v is not None:
29102914
self["ysrc"] = _v
29112915

2916+
# Read-only literals
2917+
# ------------------
2918+
2919+
self._props["type"] = "bar"
2920+
arg.pop("type", None)
2921+
29122922
# Process unknown kwargs
29132923
# ----------------------
29142924
self._process_kwargs(**dict(arg, **kwargs))

packages/python/plotly/plotly/graph_objs/_barpolar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,10 @@ def __init__(
16411641
"""
16421642
super(Barpolar, self).__init__("barpolar")
16431643

1644+
if "_parent" in kwargs:
1645+
self._parent = kwargs["_parent"]
1646+
return
1647+
16441648
# Validate arg
16451649
# ------------
16461650
if arg is None:
@@ -1836,6 +1840,12 @@ def __init__(
18361840
if _v is not None:
18371841
self["widthsrc"] = _v
18381842

1843+
# Read-only literals
1844+
# ------------------
1845+
1846+
self._props["type"] = "barpolar"
1847+
arg.pop("type", None)
1848+
18391849
# Process unknown kwargs
18401850
# ----------------------
18411851
self._process_kwargs(**dict(arg, **kwargs))

packages/python/plotly/plotly/graph_objs/_box.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,10 @@ def __init__(
26212621
"""
26222622
super(Box, self).__init__("box")
26232623

2624+
if "_parent" in kwargs:
2625+
self._parent = kwargs["_parent"]
2626+
return
2627+
26242628
# Validate arg
26252629
# ------------
26262630
if arg is None:
@@ -2924,6 +2928,12 @@ def __init__(
29242928
if _v is not None:
29252929
self["ysrc"] = _v
29262930

2931+
# Read-only literals
2932+
# ------------------
2933+
2934+
self._props["type"] = "box"
2935+
arg.pop("type", None)
2936+
29272937
# Process unknown kwargs
29282938
# ----------------------
29292939
self._process_kwargs(**dict(arg, **kwargs))

packages/python/plotly/plotly/graph_objs/_candlestick.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,10 @@ def __init__(
13861386
"""
13871387
super(Candlestick, self).__init__("candlestick")
13881388

1389+
if "_parent" in kwargs:
1390+
self._parent = kwargs["_parent"]
1391+
return
1392+
13891393
# Validate arg
13901394
# ------------
13911395
if arg is None:
@@ -1565,6 +1569,12 @@ def __init__(
15651569
if _v is not None:
15661570
self["yaxis"] = _v
15671571

1572+
# Read-only literals
1573+
# ------------------
1574+
1575+
self._props["type"] = "candlestick"
1576+
arg.pop("type", None)
1577+
15681578
# Process unknown kwargs
15691579
# ----------------------
15701580
self._process_kwargs(**dict(arg, **kwargs))

packages/python/plotly/plotly/graph_objs/_carpet.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,10 @@ def __init__(
15891589
"""
15901590
super(Carpet, self).__init__("carpet")
15911591

1592+
if "_parent" in kwargs:
1593+
self._parent = kwargs["_parent"]
1594+
return
1595+
15921596
# Validate arg
15931597
# ------------
15941598
if arg is None:
@@ -1740,6 +1744,12 @@ def __init__(
17401744
if _v is not None:
17411745
self["ysrc"] = _v
17421746

1747+
# Read-only literals
1748+
# ------------------
1749+
1750+
self._props["type"] = "carpet"
1751+
arg.pop("type", None)
1752+
17431753
# Process unknown kwargs
17441754
# ----------------------
17451755
self._process_kwargs(**dict(arg, **kwargs))

packages/python/plotly/plotly/graph_objs/_choropleth.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,10 @@ def __init__(
19101910
"""
19111911
super(Choropleth, self).__init__("choropleth")
19121912

1913+
if "_parent" in kwargs:
1914+
self._parent = kwargs["_parent"]
1915+
return
1916+
19131917
# Validate arg
19141918
# ------------
19151919
if arg is None:
@@ -2109,6 +2113,12 @@ def __init__(
21092113
if _v is not None:
21102114
self["zsrc"] = _v
21112115

2116+
# Read-only literals
2117+
# ------------------
2118+
2119+
self._props["type"] = "choropleth"
2120+
arg.pop("type", None)
2121+
21122122
# Process unknown kwargs
21132123
# ----------------------
21142124
self._process_kwargs(**dict(arg, **kwargs))

packages/python/plotly/plotly/graph_objs/_choroplethmapbox.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,10 @@ def __init__(
19031903
"""
19041904
super(Choroplethmapbox, self).__init__("choroplethmapbox")
19051905

1906+
if "_parent" in kwargs:
1907+
self._parent = kwargs["_parent"]
1908+
return
1909+
19061910
# Validate arg
19071911
# ------------
19081912
if arg is None:
@@ -2102,6 +2106,12 @@ def __init__(
21022106
if _v is not None:
21032107
self["zsrc"] = _v
21042108

2109+
# Read-only literals
2110+
# ------------------
2111+
2112+
self._props["type"] = "choroplethmapbox"
2113+
arg.pop("type", None)
2114+
21052115
# Process unknown kwargs
21062116
# ----------------------
21072117
self._process_kwargs(**dict(arg, **kwargs))

0 commit comments

Comments
 (0)