Skip to content

Commit 4fb462f

Browse files
author
Jon M. Mease
committed
Added validator for literal properties (e.g. the 'type' prop of traces)
This fixes issue where scatter['type'] and 'type' in scatter would fail
1 parent 70e76e1 commit 4fb462f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

_plotly_utils/basevalidators.py

+16
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,22 @@ def present(self, v):
15531553
return tuple(v)
15541554

15551555

1556+
class LiteralValidator(BaseValidator):
1557+
"""
1558+
Validator for readonly literal values
1559+
"""
1560+
def __init__(self, plotly_name, parent_name, **kwargs):
1561+
super().__init__(plotly_name=plotly_name,
1562+
parent_name=parent_name,
1563+
**kwargs)
1564+
1565+
def validate_coerce(self, v):
1566+
raise ValueError("""\
1567+
The '{plotly_name}' property of {parent_name} is read-only""".format(
1568+
plotly_name=self.plotly_name, parent_name=self.parent_name
1569+
))
1570+
1571+
15561572
class ImageUriValidator(BaseValidator):
15571573
_PIL = None
15581574

codegen/datatypes.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,20 @@ def __init__(self""")
214214
self.{subtype_node.name_property} = {subtype_node.name_property}""")
215215

216216
# ### Literals ###
217-
literal_nodes = [n for n in node.child_literals if
218-
n.plotly_name in ['type']]
219217
if literal_nodes:
220218
buffer.write(f"""
221219
222220
# Read-only literals
223-
# ------------------""")
221+
# ------------------
222+
from _plotly_utils.basevalidators import LiteralValidator""")
224223
for literal_node in literal_nodes:
225224
lit_name = literal_node.name_property
225+
lit_parent = literal_node.parent_path_str
226226
lit_val = literal_node.node_data
227227
buffer.write(f"""
228-
self._props['{lit_name}'] = '{lit_val}'""")
228+
self._props['{lit_name}'] = '{lit_val}'
229+
self._validators['{lit_name}'] =\
230+
LiteralValidator(plotly_name='{lit_name}', parent_name='{lit_parent}')""")
229231

230232
buffer.write(f"""
231233

0 commit comments

Comments
 (0)