8000 Changed dict path error display to always ^ · mkcor/plotly.py@d6aee64 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6aee64

Browse files
Changed dict path error display to always ^
and underneath the whole offending part of the path e.g., the_badpart_of_the_path ^^^^^^^ for both property and subscripting errors.
1 parent 9e1b667 commit d6aee64

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@
2828
Undefined = object()
2929

3030

31+
def _len_dict_item(item):
32+
"""
33+
Because a parsed dict path is a tuple containings strings or integers, to
34+
know the length of the resulting string when printing we might need to
35+
convert to a string before calling len on it.
36+
"""
37+
if type(item) == type(str()):
38+
return len(item)
39+
elif type(item) == type(int()):
40+
return len("%d" % (item,))
41+
else:
42+
raise ValueError(
43+
"Cannot find string length of an item that is neither a string nor an integer."
44+
)
45+
46+
3147
def _str_to_dict_path_full(key_path_str):
3248
"""
3349
Convert a key path string into a tuple of key path elements and also
@@ -162,7 +178,7 @@ def _check_path_in_prop_tree(obj, path, error_cast=None):
162178
%s""" % (
163179
path,
164180
display_string_positions(
165-
prop_idcs, disp_i, length=len(prop[disp_i]), char="~"
181+
prop_idcs, disp_i, length=_len_dict_item(prop[disp_i]), char="^"
166182
),
167183
)
168184
else:
@@ -173,7 +189,9 @@ def _check_path_in_prop_tree(obj, path, error_cast=None):
173189
%s
174190
%s""" % (
175191
path,
176-
display_string_positions(prop_idcs, i, length=1, char="^"),
192+
display_string_positions(
193+
prop_idcs, i, length=_len_dict_item(prop[i]), char="^"
194+
),
177195
)
178196
# Make KeyError more pretty by changing it to a PlotlyKeyError,
179197
# because the Python interpreter has a special way of printing

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_raises_on_bad_dot_property(some_fig):
4949
e.args[0].find(
5050
"""Bad property path:
5151
layout.shapes[1].x2000
52-
^"""
52+
^^^^^"""
5353
)
5454
>= 0
5555
)
@@ -68,7 +68,7 @@ def test_raises_on_bad_ancestor_dot_property(some_fig):
6868
e.args[0].find(
6969
"""Bad property path:
7070
layout.shapa[1].x2000
71-
^"""
71+
^^^^^"""
7272
)
7373
>= 0
7474
)
@@ -99,7 +99,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
9999
"""
100100
Bad property path:
101101
colr
102-
^""",
102+
^^^^""",
103103
)
104104
# if the string starts with "Bad property path:" then this test cannot work
105105
# this way.
@@ -116,14 +116,14 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
116116
"""
117117
Bad property path:
118118
data[0].line_colr
119-
^""",
119+
^^^^""",
120120
)
121121
assert (
122122
(
123123
e.args[0].find(
124124
"""Bad property path:
125125
data[0].line_colr
126-
^"""
126+
^^^^"""
127127
)
128128
>= 0
129129
)
@@ -142,7 +142,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
142142
"""
143143
Bad property path:
144144
colr
145-
^""",
145+
^^^^""",
146146
)
147147
assert raised
148148

@@ -159,14 +159,14 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
159159
"""
160160
Bad property path:
161161
line_colr
162-
^""",
162+
^^^^""",
163163
)
164164
assert (
165165
(
166166
e.args[0].find(
167167
"""Bad property path:
168168
line_colr
169-
^"""
169+
^^^^"""
170170
)
171171
>= 0
172172
)
@@ -187,7 +187,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
187187
"""
188188
Bad property path:
189189
txt
190-
^""",
190+
^^^""",
191191
)
192192
assert raised
193193

@@ -204,7 +204,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
204204
"""
205205
Bad property path:
206206
layout_title_txt
207-
^""",
207+
^^^""",
208208
)
209209
# also remove the invalid Figure property string added by the Figure constructor
210210
e_substr = error_substr(
@@ -217,7 +217,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
217217
e.args[0].find(
218218
"""Bad property path:
219219
layout_title_txt
220-
^"""
220+
^^^"""
221221
)
222222
>= 0
223223
)
@@ -237,7 +237,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
237237
"""
238238
Bad property path:
239239
ltaxis
240-
^""",
240+
^^^^^^""",
241241
)
242242
assert raised
243243

@@ -251,14 +251,14 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
251251
"""
252252
Bad property path:
253253
geo_ltaxis_showgrid
254-
^""",
254+
^^^^^^""",
255255
)
256256
assert (
257257
(
258258
e.args[0].find(
259259
"""Bad property path:
260260
geo_ltaxis_showgrid
261-
^"""
261+
^^^^^^"""
262262
)
263263
>= 0
264264
)
@@ -303,15 +303,15 @@ def test_describes_subscripting_error(some_fig):
303303
304304
Property does not support subscripting:
305305
text_yo
306-
~~~~""",
306+
^^^^""",
307307
)
308308
assert (
309309
(
310310
e.args[0].find(
311311
"""
312312
Property does not support subscripting:
313313
text_yo
314-
~~~~"""
314+
^^^^"""
315315
)
316316
>= 0
317317
)
@@ -346,15 +346,15 @@ def test_describes_subscripting_error(some_fig):
346346
347347
Property does not support subscripting:
348348
textfont_family_yo
349-
~~~~~~""",
349+
^^^^^^""",
350350
)
351351
assert (
352352
(
353353
e.args[0].find(
354354
"""
355355
Property does not support subscripting:
356356
textfont_family_yo
357-
~~~~~~"""
357+
^^^^^^"""
358358
)
359359
>= 0
360360
)
@@ -389,7 +389,7 @@ def test_described_subscript_error_on_type_error(some_fig):
389389
390390
Property does not support subscripting:
391391
template_layout_plot_bgcolor_x
392-
~~~~~~~~~~~~"""
392+
^^^^^^^^^^^^"""
393393
assert raised
394394
raised = False
395395
try:

0 commit comments

Comments
 (0)
0