Skip to content

Commit 22b3e03

Browse files
Improved error message when subscripting types that don't support it
For example, `fig.update_layout(width_x=1)` gives the following error message: TypeError: 'NoneType' object is not subscriptable Invalid value received for the 'width' property of layout The 'width' property is a number and may be specified as: - An int or float in the interval [10, inf] Property does not support subscripting: width_x ~~~~~ because `fig.layout['width']` can't be subscripted (you can't do `fig.layout['width']['x'] = 1`)
1 parent 5695969 commit 22b3e03

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,34 @@ def _check_path_in_prop_tree(obj, path):
118118
if type(path) == type(tuple()):
119119
path = _remake_path_from_tuple(path)
120120
prop, prop_idcs = _str_to_dict_path_full(path)
121+
prev_objs = []
121122
for i, p in enumerate(prop):
122123
arg = ""
124+
prev_objs.append(obj)
123125
try:
124126
obj = obj[p]
125127
except (ValueError, KeyError, IndexError, TypeError) as e:
126128
arg = e.args[0]
127129
if obj is None:
128130
# If obj doesn't support subscripting, state that and show the
129131
# (valid) property that gives the object that doesn't support
130-
# it.
132+
# subscripting.
133+
if i > 0:
134+
validator = prev_objs[i - 1]._get_validator(prop[i - 1])
135+
arg += """
136+
137+
Invalid value received for the '{plotly_name}' property of {parent_name}
138+
139+
{description}""".format(
140+
parent_name=validator.parent_name,
141+
plotly_name=validator.plotly_name,
142+
description=validator.description(),
143+
)
131144
# In case i is 0, the best we can do is indicate the first
132145
# property in the string as having caused the error
133146
disp_i = max(i - 1, 0)
134147
arg += """
148+
135149
Property does not support subscripting:
136150
%s
137151
%s""" % (

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,18 @@ def test_describes_subscripting_error(some_fig):
289289
some_fig.update_traces(text_yo="hey")
290290
except TypeError as e:
291291
raised = True
292+
print(e.args[0])
292293
e_substr = error_substr(
293294
e.args[0],
294295
"""
296+
297+
Invalid value received for the 'text' property of scatter
298+
299+
The 'text' property is a string and must be specified as:
300+
- A string
301+
- A number that will be converted to a string
302+
- A tuple, list, or one-dimensional numpy array of the above
303+
295304
Property does not support subscripting:
296305
text_yo
297306
~~~~""",
@@ -328,6 +337,13 @@ def test_describes_subscripting_error(some_fig):
328337
e_substr = error_substr(
329338
e.args[0],
330339
"""
340+
341+
Invalid value received for the 'family' property of scatter.textfont
342+
343+
The 'family' property is a string and must be specified as:
344+
- A non-empty string
345+
- A tuple, list, or one-dimensional numpy array of the above
346+
331347
Property does not support subscripting:
332348
textfont_family_yo
333349
~~~~~~""",
@@ -345,3 +361,6 @@ def test_describes_subscripting_error(some_fig):
345361
and (e_substr == e_correct_substr)
346362
)
347363
assert raised
364+
365+
366+
test_describes_subscripting_error(some_fig())

0 commit comments

Comments
 (0)