Skip to content

Commit 6901936

Browse files
committed
Don't error when a literal property is set to it's correct value.
This avoids errors when performing updates like figure.update(data=[go.Scatter(...), ...])
1 parent 5c3419b commit 6901936

38 files changed

+93
-45
lines changed

_plotly_utils/basevalidators.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,16 +1658,21 @@ class LiteralValidator(BaseValidator):
16581658
"""
16591659
Validator for readonly literal values
16601660
"""
1661-
def __init__(self, plotly_name, parent_name, **kwargs):
1662-
super(LiteralValidator, self).__init__(plotly_name=plotly_name,
1663-
parent_name=parent_name,
1664-
**kwargs)
1661+
def __init__(self, plotly_name, parent_name, val, **kwargs):
1662+
super(LiteralValidator, self).__init__(
1663+
plotly_name=plotly_name,
1664+
parent_name=parent_name,
1665+
**kwargs)
1666+
self.val = val
16651667

16661668
def validate_coerce(self, v):
1667-
raise ValueError("""\
1668-
The '{plotly_name}' property of {parent_name} is read-only""".format(
1669-
plotly_name=self.plotly_name, parent_name=self.parent_name
1670-
))
1669+
if v != self.val:
1670+
raise ValueError("""\
1671+
The '{plotly_name}' property of {parent_name} is read-only""".format(
1672+
plotly_name=self.plotly_name, parent_name=self.parent_name
1673+
))
1674+
else:
1675+
return v
16711676

16721677

16731678
class ImageUriValidator(BaseValidator):
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from _plotly_utils.basevalidators import LiteralValidator
3+
import numpy as np
4+
5+
6+
# Fixtures
7+
# --------
8+
@pytest.fixture()
9+
def validator():
10+
return LiteralValidator('prop', 'parent', 'scatter')
11+
12+
13+
# Tests
14+
# -----
15+
# ### Acceptance ###
16+
@pytest.mark.parametrize('val', ['scatter'])
17+
def test_acceptance(val, validator):
18+
assert validator.validate_coerce(val) is val
19+
20+
21+
# ### Test rejection ###
22+
@pytest.mark.parametrize('val',
23+
['hello', (), [], [1, 2, 3], set(), '34'])
24+
def test_rejection(val, validator):
25+
with pytest.raises(ValueError) as validation_failure:
26+
validator.validate_coerce(val)
27+
28+
assert 'read-only' in str(validation_failure.value)

codegen/datatypes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,12 @@ def __init__(self""")
254254
for literal_node in literal_nodes:
255255
lit_name = literal_node.name_property
256256
lit_parent = literal_node.parent_path_str
257-
lit_val = literal_node.node_data
257+
lit_val = repr(literal_node.node_data)
258258
buffer.write(f"""
259-
self._props['{lit_name}'] = '{lit_val}'
259+
self._props['{lit_name}'] = {lit_val}
260260
self._validators['{lit_name}'] =\
261-
LiteralValidator(plotly_name='{lit_name}', parent_name='{lit_parent}')""")
261+
LiteralValidator(plotly_name='{lit_name}',\
262+
parent_name='{lit_parent}', val={lit_val})""")
262263

263264
buffer.write(f"""
264265

codegen/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ def get_validator_params(self):
423423
if colorscale_node_list:
424424
colorscale_path = colorscale_node_list[0].path_str
425425
params['colorscale_path'] = repr(colorscale_path)
426+
elif self.datatype == 'literal':
427+
params['val'] = self.node_data
426428

427429
return params
428430

plotly/graph_objs/_area.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ def __init__(
785785
from _plotly_utils.basevalidators import LiteralValidator
786786
self._props['type'] = 'area'
787787
self._validators['type'] = LiteralValidator(
788-
plotly_name='type', parent_name='area'
788+
plotly_name='type', parent_name='area', val='area'
789789
)
790790

791791
# Process unknown kwargs

plotly/graph_objs/_bar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ def __init__(
21722172
from _plotly_utils.basevalidators import LiteralValidator
21732173
self._props['type'] = 'bar'
21742174
self._validators['type'] = LiteralValidator(
2175-
plotly_name='type', parent_name='bar'
2175+
plotly_name='type', parent_name='bar', val='bar'
21762176
)
21772177

21782178
# Process unknown kwargs

plotly/graph_objs/_box.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ def __init__(
15741574
from _plotly_utils.basevalidators import LiteralValidator
15751575
self._props['type'] = 'box'
15761576
self._validators['type'] = LiteralValidator(
1577-
plotly_name='type', parent_name='box'
1577+
plotly_name='type', parent_name='box', val='box'
15781578
)
15791579

15801580
# Process unknown kwargs

plotly/graph_objs/_candlestick.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ def __init__(
12341234
from _plotly_utils.basevalidators import LiteralValidator
12351235
self._props['type'] = 'candlestick'
12361236
self._validators['type'] = LiteralValidator(
1237-
plotly_name='type', parent_name='candlestick'
1237+
plotly_name='type', parent_name='candlestick', val='candlestick'
12381238
)
12391239

12401240
# Process unknown kwargs

plotly/graph_objs/_carpet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ def __init__(
17121712
from _plotly_utils.basevalidators import LiteralValidator
17131713
self._props['type'] = 'carpet'
17141714
self._validators['type'] = LiteralValidator(
1715-
plotly_name='type', parent_name='carpet'
1715+
plotly_name='type', parent_name='carpet', val='carpet'
17161716
)
17171717

17181718
# Process unknown kwargs

plotly/graph_objs/_choropleth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ def __init__(
14781478
from _plotly_utils.basevalidators import LiteralValidator
14791479
self._props['type'] = 'choropleth'
14801480
self._validators['type'] = LiteralValidator(
1481-
plotly_name='type', parent_name='choropleth'
1481+
plotly_name='type', parent_name='choropleth', val='choropleth'
14821482
)
14831483

14841484
# Process unknown kwargs

0 commit comments

Comments
 (0)