Skip to content

Commit 651b712

Browse files
Cast some errors to PlotlyKeyError
Because lookup in subclasses of BasePlotlyType and BaseFigure should throw KeyError.
1 parent 73051a6 commit 651b712

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,21 @@ def _prepend_dot_if_not_number(s):
104104
return "".join(props_w_underscore)
105105

106106

107-
def _check_path_in_prop_tree(obj, path):
107+
def _check_path_in_prop_tree(obj, path, error_cast=None):
108108
"""
109-
obj: the object in which the first property is looked up
110-
path: the path that will be split into properties to be looked up
111-
path can also be a tuple. In this case, it is combined using . and []
112-
because it is impossible to reconstruct the string fully in order to
113-
give a decent error message.
109+
obj: the object in which the first property is looked up
110+
path: the path that will be split into properties to be looked up
111+
path can also be a tuple. In this case, it is combined using .
112+
and [] because it is impossible to reconstruct the string fully
113+
in order to give a decent error message.
114+
error_cast: this function walks down the property tree by looking up values
115+
in objects. So this will throw exceptions that are thrown by
116+
__getitem__, but in some cases we are checking the path for a
117+
different reason and would prefer throwing a more relevant
118+
exception (e.g., __getitem__ throws KeyError but __setitem__
119+
throws ValueError for subclasses of BasePlotlyType and
120+
BaseFigure). So the resulting error can be "casted" to the
121+
passed in type, if not None.
114122
returns
115123
an Exception object or None. The caller can raise this
116124
exception to see where the lookup error occurred.
@@ -169,6 +177,8 @@ def _check_path_in_prop_tree(obj, path):
169177
# KeyError
170178
if type(e) == type(KeyError()):
171179
e = PlotlyKeyError()
180+
if error_cast is not None:
181+
e = error_cast()
172182
e.args = (arg,)
173183
return e
174184
return None
@@ -537,7 +547,7 @@ def __getitem__(self, prop):
537547
# ----------------------
538548
# e.g. ('foo', 1)
539549
else:
540-
err = _check_path_in_prop_tree(self, orig_prop)
550+
err = _check_path_in_prop_tree(self, orig_prop, error_cast=PlotlyKeyError)
541551
if err is not None:
542552
raise err
543553
res = self
@@ -4035,7 +4045,7 @@ def __getitem__(self, prop):
40354045
# ----------------------
40364046
# e.g. ('foo', 1), ()
40374047
else:
4038-
err = _check_path_in_prop_tree(self, orig_prop)
4048+
err = _check_path_in_prop_tree(self, orig_prop, error_cast=PlotlyKeyError)
40394049
if err is not None:
40404050
raise err
40414051
res = self
@@ -4351,7 +4361,7 @@ def _raise_on_invalid_property_error(self, *args):
43514361
else:
43524362
full_obj_name = module_root + self.__class__.__name__
43534363

4354-
raise PlotlyKeyError(
4364+
raise ValueError(
43554365
"Invalid {prop_str} specified for object of type "
43564366
"{full_obj_name}: {invalid_str}\n\n"
43574367
" Valid properties:\n"

0 commit comments

Comments
 (0)