8000 Key guessing before and after list of valid properties · lilleswing/plotly.py@20518c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20518c1

Browse files
Key guessing before and after list of valid properties
Also tripped when no underscore path 10000 s are used, e.g., fig.data[0].line["colr"] = "blue"
1 parent d2bc400 commit 20518c1

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

packages/python/plotly/_plotly_utils/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ def levenshtein(s1, s2):
433433

434434
def find_closest_string(string, strings):
435435
def _key(s):
436-
return levenshtein(s, string)
436+
# sort by levenshtein distance and lexographically to maintain a stable
437+
# sort for different keys with the same levenshtein distance
438+
return (levenshtein(s, string), s)
437439

438440
return sorted(strings, key=_key)[0]

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,6 @@ def _check_path_in_prop_tree(obj, path, error_cast=None):
238238
prop_idcs, i, length=_len_dict_item(prop[i]), char="^"
239239
),
240240
)
241-
guessed_prop = None
242-
# If obj has _valid_props then we can try and guess what key was intended
243-
try:
244-
guessed_prop = find_closest_string(prop[i], obj._valid_props)
245-
except Exception:
246-
pass
247-
if guessed_prop is not None:
248-
arg += """
249-
Did you mean "%s"?""" % (
250-
guessed_prop,
251-
)
252241
# Make KeyError more pretty by changing it to a PlotlyKeyError,
253242
# because the Python interpreter has a special way of printing
254243
# KeyError
@@ -4968,15 +4957,29 @@ def _ret(*args):
49684957
else:
49694958
full_obj_name = module_root + self.__class__.__name__
49704959

4960+
guessed_prop = None
4961+
if len(invalid_props) == 1:
4962+
try:
4963+
guessed_prop = find_closest_string(
4964+
invalid_props[0], self._valid_props
4965+
)
4966+
except Exception:
4967+
pass
4968+
guessed_prop_suggestion = ""
4969+
if guessed_prop is not None:
4970+
guessed_prop_suggestion = 'Did you mean "%s"?' % (guessed_prop,)
49714971
raise _error_to_raise(
49724972
"Invalid {prop_str} specified for object of type "
4973-
"{full_obj_name}: {invalid_str}\n\n"
4974-
" Valid properties:\n"
4975-
"{prop_descriptions}".format(
4973+
"{full_obj_name}: {invalid_str}\n"
4974+
"\n{guessed_prop_suggestion}\n"
4975+
"\n Valid properties:\n"
4976+
"{prop_descriptions}"
4977+
"\n{guessed_prop_suggestion}\n".format(
49764978
prop_str=prop_str,
49774979
full_obj_name=full_obj_name,
49784980
invalid_str=invalid_str,
49794981
prop_descriptions=self._prop_descriptions,
4982+
8000 guessed_prop_suggestion=guessed_prop_suggestion,
49804983
)
49814984
)
49824985

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ def test_raises_on_bad_dot_property(some_fig):
4949
e.args[0].find(
5050
"""Bad property path:
5151
layout.shapes[1].x2000
52-
^^^^^
53-
Did you mean "x0"?"""
52+
^^^^^"""
5453
)
55-
>= 0
54+
and (e.args[0].find("""Did you mean "x0"?""") >= 0) >= 0
5655
)
5756
assert raised
5857

@@ -69,10 +68,9 @@ def test_raises_on_bad_ancestor_dot_property(some_fig):
6968
e.args[0].find(
7069
"""Bad property path:
7170
layout.shapa[1].x2000
72-
^^^^^
73-
Did you mean "shapes"?"""
71+
^^^^^"""
7472
)
75-
>= 0
73+
and (e.args[0].find("""Did you mean "shapes"?""") >= 0) >= 0
7674
)
7775
assert raised
7876

@@ -118,19 +116,18 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
118116
"""
119117
Bad property path:
120118
data[0].line_colr
121-
^^^^
122-
Did you mean "color"?""",
119+
^^^^""",
123120
)
124121
assert (
125122
(
126123
e.args[0].find(
127124
"""Bad property path:
128125
data[0].line_colr
129-
^^^^
130-
Did you mean "color"?"""
126+
^^^^"""
131127
)
132128
>= 0
133129
)
130+
and (e.args[0].find("""Did you mean "color"?""") >= 0)
134131
and (e_substr == e_correct_substr)
135132
)
136133
assert raised
@@ -170,10 +167,9 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
170167
e.args[0].find(
171168
"""Bad property path:
172169
line_colr
173-
^^^^
174-
Did you mean "color"?"""
170+
^^^^"""
175171
)
176-
>= 0
172+
and (e.args[0].find("""Did you mean "color"?""") >= 0) >= 0
177173
)
178174
and (e_substr == e_correct_substr)
179175
)
@@ -192,8 +188,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
192188
"""
193189
Bad property path:
194190
txt
195-
^^^
196-
Did you mean "text"?""",
191+
^^^""",
197192
)
198193
assert raised
199194

@@ -210,8 +205,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
210205
"""
211206
Bad property path:
212207
layout_title_txt
213-
^^^
214-
Did you mean "text"?""",
208+
^^^""",
215209
)
216210
# also remove the invalid Figure property string added by the Figure constructor
217211
e_substr = error_substr(
@@ -224,11 +218,11 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
224218
e.args[0].find(
225219
"""Bad property path:
226220
layout_title_txt
227-
^^^
228-
Did you mean "text"?""",
221+
^^^""",
229222
)
230223
>= 0
231224
)
225+
and (e.args[0].find("""Did you mean "text"?""") >= 0)
232226
and (e_substr == e_correct_substr)
233227
)
234228
assert raised
@@ -245,8 +239,7 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
245239
"""
246240
Bad property path:
247241
ltaxis
248-
^^^^^^
249-
Did you mean "lataxis"?""",
242+
^^^^^^""",
250243
)
251244
assert raised
252245

@@ -260,19 +253,18 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
260253
"""
261254
Bad property path:
262255
geo_ltaxis_showgrid
263-
^^^^^^
264-
Did you mean "lataxis"?""",
256+
^^^^^^""",
265257
)
266258
assert (
267259
(
268260
e.args[0].find(
269261
"""Bad property path:
270262
geo_ltaxis_showgrid
271-
^^^^^^
272-
Did you mean "lataxis"?"""
263+
^^^^^^"""
273264
)
274265
>= 0
275266
)
267+
and (e.args[0].find("""Did you mean "lataxis"?""") >= 0)
276268
and (e_substr == e_correct_substr)
277269
)
278270
assert raised
@@ -494,6 +486,7 @@ def _raise_bad_property_path_real():
494486
("bogus", "_hey_yall"),
495487
("^^^^^", "^^^^"),
496488
('Did you mean "boxgap"', 'Did you mean "geo"'),
489+
('Did you mean "boxgap"', 'Did you mean "geo"'),
497490
],
498491
)
499492
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
@@ -537,7 +530,8 @@ def _raise_bad_property_path_real():
537530
[
538531
("bogusey", "_family"),
539532
("bogusey", "_family"),
540-
('Did you mean "size"?', 'Did you mean "family"?'),
533+
('Did you mean "color"?', 'Did you mean "family"?'),
534+
('Did you mean "color"?', 'Did you mean "family"?'),
541535
],
542536
)
543537
# no need to replace ^^^^^ because bogus and font_ are same length
@@ -560,6 +554,7 @@ def _raise_bad_property_path_real():
560554
("bogus", "_"),
561555
("^^^^^", "^"),
562556
('Did you mean "boxgap"', 'Did you mean "geo"'),
557+
('Did you mean "boxgap"', 'Did you mean "geo"'),
563558
],
564559
)
565560
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
@@ -581,6 +576,7 @@ def _raise_bad_property_path_real():
581576
("bogus", "__"),
582577
("^^^^^", "^^"),
583578
('Did you mean "boxgap"', 'Did you mean "geo"'),
579+
('Did you mean "boxgap"', 'Did you mean "geo"'),
584580
],
585581
)
586582
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
@@ -645,7 +641,8 @@ def _raise_bad_property_path_real():
645641
[
646642
("bogusey", "_family"),
647643
("bogusey", "_family"),
648-
('Did you mean "size"?', 'Did you mean "family"?'),
644+
('Did you mean "color"?', 'Did you mean "family"?'),
645+
('Did you mean "color"?', 'Did you mean "family"?'),
649646
],
650647
)
651648
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])

0 commit comments

Comments
 (0)
0