Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3799e6b

Browse files
committed
Tweak wording of suggestion message from getitem_checked
No need for 'one of' if there's only one option, add a colon to match the syntax of Python's own suggestion message, and remove the trailing space if there's no suggestion.
1 parent 6dc2b5a commit 3799e6b

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

lib/matplotlib/_api/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,17 @@ def getitem_checked(mapping, /, _error_cls=ValueError, **kwargs):
211211
return mapping[v]
212212
except KeyError:
213213
if len(mapping) > 5:
214-
if len(best := difflib.get_close_matches(v, mapping.keys(), cutoff=0.5)):
215-
suggestion = f"Did you mean one of {best}?"
216-
else:
217-
suggestion = ""
214+
best = difflib.get_close_matches(v, mapping.keys(), cutoff=0.5)
215+
match len(best):
216+
case 0:
217+
suggestion = ""
218+
case 1:
219+
suggestion = f" Did you mean: {best[0]!r}?"
220+
case _:
221+
suggestion = f" Did you mean one of: {', '.join(map(repr, best))}?"
218222
else:
219-
suggestion = f"Supported values are {', '.join(map(repr, mapping))}"
220-
raise _error_cls(f"{v!r} is not a valid value for {k}. {suggestion}") from None
223+
suggestion = f" Supported values are {', '.join(map(repr, mapping))}"
224+
raise _error_cls(f"{v!r} is not a valid value for {k}.{suggestion}") from None
221225

222226

223227
def caching_module_getattr(cls):

lib/matplotlib/tests/test_colors.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,9 +1882,8 @@ def test_close_error_name():
18821882
with pytest.raises(
18831883
KeyError,
18841884
match=(
1885-
"'grays' is not a valid value for colormap. "
1886-
"Did you mean one of ['gray', 'Grays', 'gray_r']?"
1887-
)):
1885+
r"'grays' is not a valid value for colormap\. "
1886+
r"Did you mean one of: 'gray', 'Grays', 'gray_r'\?")):
18881887
matplotlib.colormaps["grays"]
18891888

18901889

lib/matplotlib/tests/test_style.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ def test_context_with_badparam():
150150
with style.context({PARAM: other_value}):
151151
assert mpl.rcParams[PARAM] == other_value
152152
x = style.context({PARAM: original_value, 'badparam': None})
153-
with pytest.raises(
154-
KeyError, match="\'badparam\' is not a valid value for rcParam. "
155-
):
153+
with pytest.raises(KeyError,
154+
match=r"'badparam' is not a valid value for rcParam\."):
156155
with x:
157156
pass
158157
assert mpl.rcParams[PARAM] == other_value

0 commit comments

Comments
 (0)