Skip to content

Commit 73051a6

Browse files
BasePlotlyType._raise_on_invalid_property_error raises PlotlyKeyError
It used to raise ValueError but PlotlyKeyError subclasses KeyError and is the more relevant exception. PlotlyKeyError maintains the nice printing of the errors, unlike KeyError.
1 parent f4be2b1 commit 73051a6

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,7 +4331,7 @@ def _raise_on_invalid_property_error(self, *args):
43314331
43324332
Raises
43334333
------
4334-
ValueError
4334+
PlotlyKeyError
43354335
Always
43364336
"""
43374337
invalid_props = args
@@ -4351,7 +4351,7 @@ def _raise_on_invalid_property_error(self, *args):
43514351
else:
43524352
full_obj_name = module_root + self.__class__.__name__
43534353

4354-
raise ValueError(
4354+
raise PlotlyKeyError(
43554355
"Invalid {prop_str} specified for object of type "
43564356
"{full_obj_name}: {invalid_str}\n\n"
43574357
" Valid properties:\n"

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_raises_on_bad_dot_property(some_fig):
4343
raised = False
4444
try:
4545
x2000 = some_fig["layout.shapes[1].x2000"]
46-
except ValueError as e:
46+
except KeyError as e:
4747
raised = True
4848
assert (
4949
e.args[0].find(
@@ -62,7 +62,7 @@ def test_raises_on_bad_ancestor_dot_property(some_fig):
6262
raised = False
6363
try:
6464
x2000 = some_fig["layout.shapa[1].x2000"]
65-
except ValueError as e:
65+
except KeyError as e:
6666
raised = True
6767
assert (
6868
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 ValueError as e_correct:
94+
except KeyError 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 ValueError as e:
112+
except KeyError 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 ValueError as e_correct:
138+
except KeyError 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 ValueError as e:
155+
except KeyError 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 ValueError as e_correct:
183+
except KeyError 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 ValueError above, a
199+
# when the Figure constructor sees the same KeyError above, a
200200
# TypeError is raised and adds an error message in front of the same
201-
# ValueError thrown above
201+
# KeyError 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 ValueError as e_correct:
233+
except KeyError 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 ValueError as e:
247+
except KeyError as e:
248248
raised = True
249249
e_substr = error_substr(
250250
e.args[0],

packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_layout_subplots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_initial_access_subplot2(self):
3939
self.layout.xaxis2
4040

4141
def test_initial_access_subplot2(self):
42-
with pytest.raises(ValueError):
42+
with pytest.raises(KeyError):
4343
self.layout["xaxis2"]
4444

4545
def test_assign_subplots(self):

0 commit comments

Comments
 (0)