8000 Promote pending cm deprecations to full deprecations · matplotlib/matplotlib@a8eb350 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8eb350

Browse files
committed
Promote pending cm deprecations to full deprecations
This is the next stage of #20853.
1 parent bf68676 commit a8eb350

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Deprecation of top-level cmap registration and access functions in ``mpl.cm``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
As part of a `multi-step process
5+
<https://github.com/matplotlib/matplotlib/issues/20853>`_ we are refactoring
6+
the global state for managing the registered colormaps.
7+
8+
In Matplotlib 3.5 we added a `.ColormapRegistry` class and exposed an instance
9+
at the top level as ``matplotlib.colormaps``. The existing top level functions
10+
in `matplotlib.cm` (``get_cmap``, ``register_cmap``, ``unregister_cmap``) were
11+
changed to be aliases around the same instance. In Matplotlib 3.6 we have
12+
marked those top level functions as pending deprecation.
13+
14+
In Matplotlib 3.7, the following functions have been marked for deprecation:
15+
16+
- ``matplotlib.cm.get_cmap``; use ``matplotlib.colormaps[name]`` instead if you
17+
have a `str`.
18+
19+
**Added 3.6.1** Use `matplotlib.cm.ColormapRegistry.get_cmap` if you
20+
have a string, `None` or a `matplotlib.colors.Colormap` object that you want
21+
to convert to a `matplotlib.colors.Colormap` instance.
22+
- ``matplotlib.cm.register_cmap``; use `matplotlib.colormaps.register
23+
<.ColormapRegistry.register>` instead
24+
- ``matplotlib.cm.unregister_cmap``; use `matplotlib.colormaps.unregister
25+
<.ColormapRegistry.unregister>` instead
26+
- ``matplotlib.pyplot.register_cmap``; use `matplotlib.colormaps.register
27+
<.ColormapRegistry.register>` instead
28+
29+
The `matplotlib.pyplot.get_cmap` function will stay available for backward
30+
compatibility.

lib/matplotlib/cm.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,7 @@ def get_cmap(self, cmap):
220220
globals().update(_colormaps)
221221

222222

223-
@_api.deprecated(
224-
'3.6',
225-
pending=True,
226-
alternative="``matplotlib.colormaps.register(name)``"
227-
)
223+
@_api.deprecated("3.7", alternative="``matplotlib.colormaps.register(name)``")
228224
def register_cmap(name=None, cmap=None, *, override_builtin=False):
229225
"""
230226
Add a colormap to the set recognized by :func:`get_cmap`.
@@ -301,19 +297,15 @@ def _get_cmap(name=None, lut=None):
301297
get_cmap = _api.deprecated(
302298
'3.6',
303299
name='get_cmap',
304-
pending=True,
305300
alternative=(
306301
"``matplotlib.colormaps[name]`` " +
307302
"or ``matplotlib.colormaps.get_cmap(obj)``"
308303
)
309304
)(_get_cmap)
310305

311306

312-
@_api.deprecated(
313-
'3.6',
314-
pending=True,
315-
alternative="``matplotlib.colormaps.unregister(name)``"
316-
)
307+
@_api.deprecated("3.7",
308+
alternative="``matplotlib.colormaps.unregister(name)``")
317309
def unregister_cmap(name):
318310
"""
319311
Remove a colormap recognized by :func:`get_cmap`.

lib/matplotlib/tests/test_colors.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_register_cmap():
6868
new_cm = mpl.colormaps["viridis"]
6969
target = "viridis2"
7070
with pytest.warns(
71-
PendingDeprecationWarning,
71+
mpl.MatplotlibDeprecationWarning,
7272
match=r"matplotlib\.colormaps\.register\(name\)"
7373
):
7474
cm.register_cmap(target, new_cm)
@@ -77,33 +77,33 @@ def test_register_cmap():
7777
with pytest.raises(ValueError,
7878
match="Arguments must include a name or a Colormap"):
7979
with pytest.warns(
80-
PendingDeprecationWarning,
80+
mpl.MatplotlibDeprecationWarning,
8181
match=r"matplotlib\.colormaps\.register\(name\)"
8282
):
8383
cm.register_cmap()
8484

8585
with pytest.warns(
86-
PendingDeprecationWarning,
86+
mpl.MatplotlibDeprecationWarning,
8787
match=r"matplotlib\.colormaps\.unregister\(name\)"
8888
):
8989
cm.unregister_cmap(target)
9090
with pytest.raises(ValueError,
9191
match=f'{target!r} is not a valid value for name;'):
9292
with pytest.warns(
93-
PendingDeprecationWarning,
93+
mpl.MatplotlibDeprecationWarning,
9494
match=r"matplotlib\.colormaps\[name\]"
9595
):
9696
cm.get_cmap(target)
9797
with pytest.warns(
98-
PendingDeprecationWarning,
98+
mpl.MatplotlibDeprecationWarning,
9999
match=r"matplotlib\.colormaps\.unregister\(name\)"
100100
):
101101
# test that second time is error free
102102
cm.unregister_cmap(target)
103103

104104
with pytest.raises(TypeError, match="'cmap' must be"):
105105
with pytest.warns(
106-
PendingDeprecationWarning,
106+
mpl.MatplotlibDeprecationWarning,
107107
match=r"matplotlib\.colormaps\.register\(name\)"
108108
):
109109
cm.register_cmap('nome', cmap='not a cmap')
@@ -137,7 +137,7 @@ def test_double_register_builtin_cmap():
137137
mpl.colormaps[name], name=name, force=True
138138
)
139139
with pytest.raises(ValueError, match='A colormap named "viridis"'):
140-
with pytest.warns(PendingDeprecationWarning):
140+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
141141
cm.register_cmap(name, mpl.colormaps[name])
142142
with pytest.warns(UserWarning):
143143
# TODO is warning more than once!
@@ -148,7 +148,7 @@ def test_unregister_builtin_cmap():
148148
name = "viridis"
149149
match = f'cannot unregister {name!r} which is a builtin colormap.'
150150
with pytest.raises(ValueError, match=match):
151-
with pytest.warns(PendingDeprecationWarning):
151+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
152152
cm.unregister_cmap(name)
153153

154154

0 commit comments

Comments
 (0)
0