Skip to content

Commit 4c659db

Browse files
Also state when a property doesn't support subscripting
Shows the property that doesn't support it. Need to still update the tests though.
1 parent b894b89 commit 4c659db

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

packages/python/plotly/_plotly_utils/utils.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,11 @@ def split_string_positions(ss):
318318
)
319319

320320

321-
def display_string_positions(p, i=None):
321+
def display_string_positions(p, i=None, offset=0, length=1, char="^", trim=True):
322322
"""
323-
Return a string that is whitespace except at p[i] which is replaced with ^.
324-
If i is None then all the indices of the string in p are replaced with ^.
323+
Return a string that is whitespace except at p[i] which is replaced with char.
324+
If i is None then all the indices of the string in p are replaced with char.
325+
325326
Example:
326327
327328
>>> ss = ["a.string[0].with_separators"]
@@ -334,13 +335,30 @@ def display_string_positions(p, i=None):
334335
'a.string[0].with_separators'
335336
>>> display_string_positions(ss_pos,4)
336337
' ^'
338+
>>> display_string_positions(ss_pos,4,offset=1,length=3,char="~",trim=False)
339+
' ~~~ '
340+
>>> display_string_positions(ss_pos_)
341+
'^ ^ ^ ^^ ^'
337342
:param (list) p: A list of integers.
338343
:param (integer|None) i: Optional index of p to display.
344+
:param (integer) offset: Allows adding a number of spaces to the replacement.
345+
:param (integer) length: Allows adding a replacement that is the char
346+
repeated length times.
347+
:param (str) char: allows customizing the replacement character.
348+
:param (boolean) trim: trims the remaining whitespace if True.
339349
"""
340-
s = [" " for _ in range(max(p) + 1)]
350+
s = [" " for _ in range(max(p) + 1 + offset + length)]
351+
maxaddr = 0
341352
if i is None:
342353
for p_ in p:
343-
s[p_] = "^"
354+
for l in range(length):
355+
maxaddr = p_ + offset + l
356+
s[maxaddr] = char
344357
else:
345-
s[p[i]] = "^"
346-
return "".join(s)
358+
for l in range(length):
359+
maxaddr = p[i] + offset + l
360+
s[maxaddr] = char
361+
ret = "".join(s)
362+
if trim:
363+
ret = ret[: maxaddr + 1]
364+
return ret

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,37 @@ def _check_path_in_prop_tree(obj, path):
119119
path = _remake_path_from_tuple(path)
120120
prop, prop_idcs = _str_to_dict_path_full(path)
121121
for i, p in enumerate(prop):
122+
arg = ""
122123
try:
123124
obj = obj[p]
124-
except (ValueError, KeyError, IndexError) as e:
125-
arg = (
126-
e.args[0]
127-
+ """
125+
except (ValueError, KeyError, IndexError, TypeError) as e:
126+
arg = e.args[0]
127+
if obj is None:
128+
# If obj doesn't support subscripting, state that and show the
129+
# (valid) property that gives the object that doesn't support
130+
# it.
131+
# In case i is 0, the best we can do is indicate the first
132+
# property in the string as having caused the error
133+
disp_i = max(i - 1, 0)
134+
arg += """
135+
Property does not support subscripting:
136+
%s
137+
%s""" % (
138+
path,
139+
display_string_positions(
140+
prop_idcs, disp_i, length=len(prop[disp_i]), char="~"
141+
),
142+
)
143+
else:
144+
# State that the property for which subscripting was attempted
145+
# is bad and indicate the start of the bad property.
146+
arg += """
128147
Bad property path:
129-
%s"""
130-
% (path,)
131-
)
132-
arg += """
148+
%s
133149
%s""" % (
134-
display_string_positions(prop_idcs, i),
135-
)
150+
path,
151+
display_string_positions(prop_idcs, i, length=1, char="^"),
152+
)
136153
# Make KeyError more pretty by changing it to a PlotlyKeyError,
137154
# because the Python interpreter has a special way of printing
138155
# KeyError

0 commit comments

Comments
 (0)