Skip to content

Commit 4f8ece4

Browse files
committed
MNT: raise ValueError and TypeError rather than KeyError
1 parent d0a240a commit 4f8ece4

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

doc/api/prev_api_changes/api_changes_3.6.0/deprecations.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ functions have been marked for pending deprecation:
5757

5858
**Added 3.6.1** Use `matplotlib.cm.ColormapRegistry.get_cmap` if you
5959
have a string, `None` or a `matplotlib.colors.Colormap` object that you want
60-
to convert to a `matplotlib.colors.Colormap` instance. Raises `KeyError`
61-
rather than `ValueError` for missing strings.
60+
to convert to a `matplotlib.colors.Colormap` instance.
6261
- ``matplotlib.cm.register_cmap``; use `matplotlib.colormaps.register
6362
<.ColormapRegistry.register>` instead
6463
- ``matplotlib.cm.unregister_cmap``; use `matplotlib.colormaps.unregister

lib/matplotlib/cm.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,6 @@ def get_cmap(self, cmap):
202202
Returns
203203
-------
204204
Colormap
205-
206-
Raises
207-
------
208-
KeyError
209205
"""
210206
# get the default color map
211207
if cmap is None:
@@ -214,9 +210,14 @@ def get_cmap(self, cmap):
214210
# if the user passed in a Colormap, simply return it
215211
if isinstance(cmap, colors.Colormap):
216212
return cmap
217-
218-
# otherwise, it must be a string so look it up
219-
return self[cmap]
213+
if isinstance(cmap, str):
214+
_api.check_in_list(sorted(_colormaps), cmap=cmap)
215+
# otherwise, it must be a string so look it up
216+
return self[cmap]
217+
raise TypeError(
218+
'get_cmap expects None or an instance of a str or Colormap . ' +
219+
f'you passed {cmap!r} of type {type(cmap)}'
220+
)
220221

221222

222223
# public access to the colormaps should be via `matplotlib.colormaps`. For now,

lib/matplotlib/tests/test_colors.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,23 @@ def test_register_cmap():
111111

112112
def test_colormaps_get_cmap():
113113
cr = mpl.colormaps
114-
new_cm = mcolors.ListedColormap(cr["viridis"].colors, name='v2')
115114

116-
# check None, str, and Colormap pass
115+
# check str, and Colormap pass
117116
assert cr.get_cmap('plasma') == cr["plasma"]
118117
assert cr.get_cmap(cr["magma"]) == cr["magma"]
119118

120-
# check default default
119+
# check default
121120
assert cr.get_cmap(None) == cr[mpl.rcParams['image.cmap']]
121+
122+
# check ValueError on bad name
122123
bad_cmap = 'AardvarksAreAwkward'
123-
with pytest.raises(KeyError, match=bad_cmap):
124+
with pytest.raises(ValueError, match=bad_cmap):
124125
cr.get_cmap(bad_cmap)
125126

127+
# check TypeError on bad type
128+
with pytest.raises(TypeError, match='object'):
129+
cr.get_cmap(object())
130+
126131

127132
def test_double_register_builtin_cmap():
128133
name = "viridis"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy