@@ -104,13 +104,21 @@ def _prepend_dot_if_not_number(s):
104
104
return "" .join (props_w_underscore )
105
105
106
106
107
- def _check_path_in_prop_tree (obj , path ):
107
+ def _check_path_in_prop_tree (obj , path , error_cast = None ):
108
108
"""
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.
114
122
returns
115
123
an Exception object or None. The caller can raise this
116
124
exception to see where the lookup error occurred.
@@ -169,6 +177,8 @@ def _check_path_in_prop_tree(obj, path):
169
177
# KeyError
170
178
if type (e ) == type (KeyError ()):
171
179
e = PlotlyKeyError ()
180
+ if error_cast is not None :
181
+ e = error_cast ()
172
182
e .args = (arg ,)
173
183
return e
174
184
return None
@@ -537,7 +547,7 @@ def __getitem__(self, prop):
537
547
# ----------------------
538
548
# e.g. ('foo', 1)
539
549
else :
540
- err = _check_path_in_prop_tree (self , orig_prop )
550
+ err = _check_path_in_prop_tree (self , orig_prop , error_cast = PlotlyKeyError )
541
551
if err is not None :
542
552
raise err
543
553
res = self
@@ -4035,7 +4045,7 @@ def __getitem__(self, prop):
4035
4045
# ----------------------
4036
4046
# e.g. ('foo', 1), ()
4037
4047
else :
4038
- err = _check_path_in_prop_tree (self , orig_prop )
4048
+ err = _check_path_in_prop_tree (self , orig_prop , error_cast = PlotlyKeyError )
4039
4049
if err is not None :
4040
4050
raise err
4041
4051
res = self
@@ -4351,7 +4361,7 @@ def _raise_on_invalid_property_error(self, *args):
4351
4361
else :
4352
4362
full_obj_name = module_root + self .__class__ .__name__
4353
4363
4354
- raise PlotlyKeyError (
4364
+ raise ValueError (
4355
4365
"Invalid {prop_str} specified for object of type "
4356
4366
"{full_obj_name}: {invalid_str}\n \n "
4357
4367
" Valid properties:\n "
0 commit comments