Skip to content

Commit 9bb2470

Browse files
Updated the tests to reflect the new Exception behaviour
I believe the new behaviour is consistent with the previous exception behaviour, but we will run the CI tests to be sure.
1 parent 651b712 commit 9bb2470

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def __setitem__(self, prop, value):
489489
# ----------------------
490490
# e.g. ('foo', 1)
491491
else:
492-
err = _check_path_in_prop_tree(self, orig_prop)
492+
err = _check_path_in_prop_tree(self, orig_prop, error_cast=ValueError)
493493
if err is not None:
494494
raise err
495495
res = self
@@ -3456,7 +3456,7 @@ def _perform_update(plotly_obj, update_obj, overwrite=False):
34563456
# This should be valid even if xaxis2 hasn't been initialized:
34573457
# >>> layout.update(xaxis2={'title': 'xaxis 2'})
34583458
for key in update_obj:
3459-
err = _check_path_in_prop_tree(plotly_obj, key)
3459+
err = _check_path_in_prop_tree(plotly_obj, key, error_cast=ValueError)
34603460
if err is not None:
34613461
if isinstance(plotly_obj, BaseLayoutType):
34623462
# try _subplot_re_match
@@ -3672,7 +3672,7 @@ def _process_kwargs(self, **kwargs):
36723672
"""
36733673
invalid_kwargs = {}
36743674
for k, v in kwargs.items():
3675-
err = _check_path_in_prop_tree(self, k)
3675+
err = _check_path_in_prop_tree(self, k, error_cast=ValueError)
36763676
if err is None:
36773677
# e.g. underscore kwargs like marker_line_color
36783678
self[k] = v
@@ -4007,7 +4007,9 @@ def __getitem__(self, prop):
40074007
# Unwrap scalar tuple
40084008
prop = prop[0]
40094009
if prop not in self._valid_props:
4010-
self._raise_on_invalid_property_error(prop)
4010+
self._raise_on_invalid_property_error(_error_to_raise=PlotlyKeyError)(
4011+
prop
4012+
)
40114013

40124014
validator = self._get_validator(prop)
40134015

@@ -4145,7 +4147,7 @@ def __setitem__(self, prop, value):
41454147

41464148
if self._validate:
41474149
if prop not in self._valid_props:
4148-
self._raise_on_invalid_property_error(prop)
4150+
self._raise_on_invalid_property_error()(prop)
41494151

41504152
# ### Get validator for this property ###
41514153
validator = self._get_validator(prop)
@@ -4219,7 +4221,7 @@ def __setattr__(self, prop, value):
42194221
super(BasePlotlyType, self).__setattr__(prop, value)
42204222
else:
42214223
# Raise error on unknown public properties
4222-
self._raise_on_invalid_property_error(prop)
4224+
self._raise_on_invalid_property_error()(prop)
42234225

42244226
def __iter__(self):
42254227
"""
@@ -4328,10 +4330,11 @@ def __repr__(self):
43284330

43294331
return repr_str
43304332

4331-
def _raise_on_invalid_property_error(self, *args):
4333+
def _raise_on_invalid_property_error(self, _error_to_raise=None):
43324334
"""
4333-
Raise informative exception when invalid property names are
4334-
encountered
4335+
Returns a function that raises informative exception when invalid
4336+
property names are encountered. The _error_to_raise argument allows
4337+
specifying the exception to raise, which is ValueError if None.
43354338
43364339
Parameters
43374340
----------
@@ -4341,37 +4344,45 @@ def _raise_on_invalid_property_error(self, *args):
43414344
43424345
Raises
43434346
------
4344-
PlotlyKeyError
4345-
Always
4346-
"""
4347-
invalid_props = args
4348-
if invalid_props:
4349-
if len(invalid_props) == 1:
4350-
prop_str = "property"
4351-
invalid_str = repr(invalid_props[0])
4352-
else:
4353-
prop_str = "properties"
4354-
invalid_str = repr(invalid_props)
4347+
ValueError by default, or _error_to_raise if not None
4348+
"""
4349+
if _error_to_raise is None:
4350+
_error_to_raise = ValueError
43554351

4356-
module_root = "plotly.graph_objs."
4357-
if self._parent_path_str:
4358-
full_obj_name = (
4359-
module_root + self._parent_path_str + "." + self.__class__.__name__
4352+
def _ret(*args):
4353+
invalid_props = args
4354+
if invalid_props:
4355+
if len(invalid_props) == 1:
4356+
prop_str = "property"
4357+
invalid_str = repr(invalid_props[0])
4358+
else:
4359+
prop_str = "properties"
4360+
invalid_str = repr(invalid_props)
4361+
4362+
module_root = "plotly.graph_objs."
4363+
if self._parent_path_str:
4364+
full_obj_name = (
4365+
module_root
4366+
+ self._parent_path_str
4367+
+ "."
4368+
+ self.__class__.__name__
4369+
)
4370+
else:
4371+
full_obj_name = module_root + self.__class__.__name__
4372+
4373+
raise _error_to_raise(
4374+
"Invalid {prop_str} specified for object of type "
4375+
"{full_obj_name}: {invalid_str}\n\n"
4376+
" Valid properties:\n"
4377+
"{prop_descriptions}".format(
4378+
prop_str=prop_str,
4379+
full_obj_name=full_obj_name,
4380+
invalid_str=invalid_str,
4381+
prop_descriptions=self._prop_descriptions,
4382+
)
43604383
)
4361-
else:
4362-
full_obj_name = module_root + self.__class__.__name__
43634384

4364-
raise ValueError(
4365-
"Invalid {prop_str} specified for object of type "
4366-
"{full_obj_name}: {invalid_str}\n\n"
4367-
" Valid properties:\n"
4368-
"{prop_descriptions}".format(
4369-
prop_str=prop_str,
4370-
full_obj_name=full_obj_name,
4371-
invalid_str=invalid_str,
4372-
prop_descriptions=self._prop_descriptions,
4373-
)
4374-
)
4385+
return _ret
43754386

43764387
def update(self, dict1=None, overwrite=False, **kwargs):
43774388
"""

packages/python/plotly/plotly/tests/test_core/test_errors/test_dict_path_errors.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_raises_on_bad_index(some_fig):
2323
raised = False
2424
try:
2525
x0 = some_fig["layout.shapes[2].x0"]
26-
except IndexError as e:
26+
except KeyError as e:
2727
raised = True
2828
assert (
2929
e.args[0].find(
@@ -91,7 +91,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
9191
try:
9292
# get the error without using a path-like key, we compare with this error
9393
some_fig.data[0].line["colr"] = "blue"
94-
except KeyError as e_correct:
94+
except ValueError as e_correct:
9595
raised = True
9696
# remove "Bad property path:
9797
e_correct_substr = error_substr(
@@ -109,7 +109,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
109109
raised = False
110110
try:
111111
some_fig["data[0].line_colr"] = "blue"
112-
except KeyError as e:
112+
except ValueError as e:
113113
raised = True
114114
e_substr = error_substr(
115115
e.args[0],
@@ -135,7 +135,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
135135
try:
136136
# get the error without using a path-like key
137137
some_fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4], line=dict(colr="blue")))
138-
except KeyError as e_correct:
138+
except ValueError as e_correct:
139139
raised = True
140140
e_correct_substr = error_substr(
141141
e_correct.args[0],
@@ -152,7 +152,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
152152
# the path
153153
try:
154154
some_fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4], line_colr="blue"))
155-
except KeyError as e:
155+
except ValueError as e:
156156
raised = True
157157
e_substr = error_substr(
158158
e.args[0],
@@ -180,7 +180,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
180180
# the path
181181
try:
182182
fig2 = go.Figure(layout=dict(title=dict(txt="two")))
183-
except KeyError as e_correct:
183+
except ValueError as e_correct:
184184
raised = True
185185
e_correct_substr = error_substr(
186186
e_correct.args[0],
@@ -196,9 +196,9 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
196196
fig2 = go.Figure(layout_title_txt="two")
197197
except TypeError as e:
198198
raised = True
199-
# when the Figure constructor sees the same KeyError above, a
200-
# TypeError is raised and adds an error message in front of the same
201-
# KeyError thrown above
199+
# when the Figure constructor sees the same ValueError above, a
200+
# ValueError is raised and adds an error message in front of the same
201+
# ValueError thrown above
202202
e_substr = error_substr(
203203
e.args[0],
204204
"""
@@ -230,7 +230,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
230230
# works when the bad part is not the last part in the path
231231
try:
232232
some_fig.update_layout(geo=dict(ltaxis=dict(showgrid=True)))
233-
except KeyError as e_correct:
233+
except ValueError as e_correct:
234234
raised = True
235235
e_correct_substr = error_substr(
236236
e_correct.args[0],
@@ -244,7 +244,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
244244
raised = False
245245
try:
246246
some_fig.update_layout(geo_ltaxis_showgrid=True)
247-
except KeyError as e:
247+
except ValueError as e:
248248
raised = True
249249
e_substr = error_substr(
250250
e.args[0],
@@ -287,7 +287,7 @@ def test_describes_subscripting_error(some_fig):
287287
raised = False
288288
try:
289289
some_fig.update_traces(text_yo="hey")
290-
except TypeError as e:
290+
except ValueError as e:
291291
raised = True
292292
print(e.args[0])
293293
e_substr = error_substr(
@@ -332,7 +332,7 @@ def test_describes_subscripting_error(some_fig):
332332
raised = False
333333
try:
334334
go.Figure(go.Scatter()).update_traces(textfont_family_yo="hey")
335-
except TypeError as e:
335+
except ValueError as e:
336336
raised = True
337337
e_substr = error_substr(
338338
e.args[0],

0 commit comments

Comments
 (0)