Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3f98b36

Browse files
committed
Include close matches when key not found
1 parentabda9dd commit3f98b36

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

‎lib/matplotlib/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,12 +740,11 @@ def __setitem__(self, key, val):
740740
andvalisrcsetup._auto_backend_sentinel
741741
and"backend"inself):
742742
return
743+
valid_key=_api.check_getitem(
744+
self.validate,rcParam=key,_suggest_close_matches=True,_error_cls=KeyError
745+
)
743746
try:
744-
cval=self.validate[key](val)
745-
exceptKeyErroraserr:
746-
raiseKeyError(
747-
f"{key} is not a valid rc parameter (see rcParams.keys() for "
748-
f"a list of valid parameters)")fromerr
747+
cval=valid_key(val)
749748
exceptValueErrorasve:
750749
raiseValueError(f"Key{key}:{ve}")fromNone
751750
self._set(key,cval)

‎lib/matplotlib/_api/__init__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
"""
1212

13+
importdifflib
1314
importfunctools
1415
importitertools
1516
importpathlib
@@ -174,12 +175,19 @@ def check_shape(shape, /, **kwargs):
174175
)
175176

176177

177-
defcheck_getitem(mapping,/,**kwargs):
178+
defcheck_getitem(
179+
mapping,/,_suggest_close_matches=False,_error_cls=ValueError,**kwargs
180+
):
178181
"""
179182
*kwargs* must consist of a single *key, value* pair. If *key* is in
180183
*mapping*, return ``mapping[value]``; else, raise an appropriate
181184
ValueError.
182185
186+
Parameters
187+
----------
188+
_suggest_close_matches
189+
If True, suggest only close matches instead of all valid values.
190+
183191
Examples
184192
--------
185193
>>> _api.check_getitem({"foo": "bar"}, arg=arg)
@@ -190,9 +198,14 @@ def check_getitem(mapping, /, **kwargs):
190198
try:
191199
returnmapping[v]
192200
exceptKeyError:
193-
raiseValueError(
194-
f"{v!r} is not a valid value for{k}; supported values are "
195-
f"{', '.join(map(repr,mapping))}")fromNone
201+
if_suggest_close_matches:
202+
iflen(best:=difflib.get_close_matches(v,mapping.keys(),cutoff=0.5)):
203+
suggestion=f"Did you mean one of{best}?"
204+
else:
205+
suggestion=""
206+
else:
207+
suggestion=f"Supported values are{', '.join(map(repr,mapping))}"
208+
raise_error_cls(f"{v!r} is not a valid value for{k}.{suggestion}")fromNone
196209

197210

198211
defcaching_module_getattr(cls):

‎lib/matplotlib/cm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ def __init__(self, cmaps):
9292
self._builtin_cmaps=tuple(cmaps)
9393

9494
def__getitem__(self,item):
95-
try:
96-
returnself._cmaps[item].copy()
97-
exceptKeyError:
98-
raiseKeyError(f"{item!r} is not a known colormap name")fromNone
95+
cmap=_api.check_getitem(
96+
self._cmaps,colormap=item,_suggest_close_matches=True,_error_cls=KeyError
97+
)
98+
returncmap.copy()
9999

100100
def__iter__(self):
101101
returniter(self._cmaps)

‎lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,3 +1828,13 @@ def test_LinearSegmentedColormap_from_list_value_color_tuple():
18281828
cmap([valueforvalue,_invalue_color_tuples]),
18291829
to_rgba_array([colorfor_,colorinvalue_color_tuples]),
18301830
)
1831+
1832+
1833+
deftest_close_error_name():
1834+
withpytest.raises(
1835+
KeyError,
1836+
match=(
1837+
"'grays' is not a valid value for colormap. "
1838+
"Did you mean one of ['gray', 'Grays', 'gray_r']?"
1839+
)):
1840+
matplotlib.colormaps["grays"]

‎lib/matplotlib/tests/test_style.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ def test_context_with_badparam():
140140
withstyle.context({PARAM:other_value}):
141141
assertmpl.rcParams[PARAM]==other_value
142142
x=style.context({PARAM:original_value,'badparam':None})
143-
withpytest.raises(KeyError):
143+
withpytest.raises(
144+
KeyError,match="\'badparam\' is not a valid value for rcParam. "
145+
):
144146
withx:
145147
pass
146148
assertmpl.rcParams[PARAM]==other_value

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp