8000 Now subscripting errors triggered on types throwing TypeError · mkcor/plotly.py@4bda7b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4bda7b2

Browse files
Now subscripting errors triggered on types throwing TypeError
Before the subscripting error was only thrown for NoneType objects, but because stuff like `"hello"["hi"]` and `1["hey"]` also are invalid, and these also throw TypeError, then these throw the subscripting error message too. HOWEVER, this error is casted to ValueError on property assignment and PlotlyKeyError on property read, to keep consistency among the exception types.
1 parent a18b341 commit 4bda7b2

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _check_path_in_prop_tree(obj, path, error_cast=None):
137137
obj = obj[p]
138138
except (ValueError, KeyError, IndexError, TypeError) as e:
139139
arg = e.args[0]
140-
if obj is None:
140+
if issubclass(e.__class__, TypeError):
141141
# If obj doesn't support subscripting, state that and show the
142142
# (valid) property that gives the object that doesn't support
143143
# subscripting.

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,61 @@ def test_describes_subscripting_error(some_fig):
361361
and (e_substr == e_correct_substr)
362362
)
363363
assert raised
364+
365+
366+
def test_described_subscript_error_on_type_error(some_fig):
367+
# The above tests for subscripting errors did not test for when we attempt
368+
# to subscript an object that is not None, such as a string or a number.
369+
# These do that.
370+
raised = False
371+
try:
372+
# Trying to address with a key an object that doesn't support it (as we
373+
# do below) reports an error listing what are valid assignments to the
374+
# object, like when we try and assign a number to something that expects as string.
375+
some_fig["layout_template_layout_plot_bgcolor"] = 1
376+
except ValueError as e:
377+
raised = True
378+
e_correct_substr = error_substr(
379+
e.args[0],
380+
"""
381+
Invalid value of type 'builtins.int' received for the 'plot_bgcolor' property of layout
382+
Received value: 1
383+
""",
384+
)
385+
e_correct_substr += """
386+
387+
Property does not support subscripting:
388+
template_layout_plot_bgcolor_x
389+
~~~~~~~~~~~~"""
390+
assert raised
391+
raised = False
392+
try:
393+
some_fig.update_layout(template_layout_plot_bgcolor_x=1)
394+
except ValueError as e:
395+
raised = True
396+
print(e.args[0])
397+
e_substr = error_substr(
398+
e.args[0],
399+
"""string indices must be integers
400+
401+
Invalid value received for the 'plot_bgcolor' property of layout
402+
""",
403+
)
404+
assert e_substr == e_correct_substr
405+
assert raised
406+
407+
408+
def test_subscript_error_exception_types(some_fig):
409+
# Assert that these raise the expected error types
410+
# when width is None
411+
with pytest.raises(ValueError):
412+
some_fig.update_layout(width_yo=100)
413+
with pytest.raises(KeyError):
414+
yo = some_fig["layout_width_yo"]
415+
416+
some_fig.update_layout(width=100)
417+
# when width is specified
418+
with pytest.raises(ValueError):
419+
some_fig.update_layout(width_yo=100)
420+
with pytest.raises(KeyError):
421+
yo = some_fig["layout_width_yo"]

0 commit comments

Comments
 (0)
0