Skip to content

Commit 8364f87

Browse files
committed
Use new warn_deprecated helper function and decorator where appropriate.
1 parent 84ffb60 commit 8364f87

File tree

4 files changed

+95
-50
lines changed

4 files changed

+95
-50
lines changed

lib/matplotlib/axes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import matplotlib.ticker as mticker
3838
import matplotlib.transforms as mtransforms
3939
import matplotlib.tri as mtri
40-
from matplotlib.cbook import mplDeprecation
4140
from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer
4241

4342
iterable = cbook.iterable
@@ -1062,8 +1061,8 @@ def set_aspect(self, aspect, adjustable=None, anchor=None):
10621061
the option 'normal' for aspect is deprecated. Use 'auto' instead.
10631062
"""
10641063
if aspect == 'normal':
1065-
warnings.warn("Use 'auto' instead of 'normal' for aspect. Will "
1066-
"be removed in 1.4.x", mplDeprecation)
1064+
cbook.warn_deprecated(
1065+
'1.2', name='normal', alternative='auto', obj_type='aspect')
10671066
self._aspect = 'auto'
10681067

10691068
elif aspect in ('equal', 'auto'):
@@ -6276,9 +6275,8 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
62766275
faceted = kwargs.pop('faceted', None)
62776276
edgecolors = kwargs.get('edgecolors', None)
62786277
if faceted is not None:
6279-
warnings.warn("The faceted option is deprecated. "
6280-
"Please use edgecolor instead. Will "
6281-
"be removed in 1.4", mplDeprecation)
6278+
cbook.warn_deprecated(
6279+
'1.2', 'faceted', alternative='edgecolor', obj_type='option')
62826280
if faceted:
62836281
edgecolors = None
62846282
else:
@@ -7500,8 +7498,8 @@ def pcolor(self, *args, **kwargs):
75007498
vmin = kwargs.pop('vmin', None)
75017499
vmax = kwargs.pop('vmax', None)
75027500
if 'shading' in kwargs:
7503-
warnings.warn("Use edgecolors instead of shading. "
7504-
"Will be removed in 1.4", mplDeprecation)
7501+
cbook.warn_deprecated(
7502+
'1.2', 'shading', alternative='edgecolors', obj_type='option')
75057503
shading = kwargs.pop('shading', 'flat')
75067504

75077505
X, Y, C = self._pcolorargs('pcolor', *args)

lib/matplotlib/axis.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import numpy as np
1919
import warnings
2020

21-
from cbook import mplDeprecation
22-
2321
GRIDLINE_INTERPOLATION_STEPS = 180
2422

2523

@@ -684,15 +682,11 @@ def get_transform(self):
684682
def get_scale(self):
685683
return self._scale.name
686684

685+
@cbook.deprecated('1.3')
687686
def set_scale(self, value, **kwargs):
688687
"""
689-
Deprecated 1.3.
690-
691688
This should be a private function (moved to _set_scale)
692689
"""
693-
warnings.warn("This function has been made private and moved"
694-
"to `_set_scale`. This wrapper function will be "
695-
"removed in 1.4", mplDeprecation)
696690
self._set_scale(value, **kwargs)
697691

698692
def _set_scale(self, value, **kwargs):

lib/matplotlib/cbook.py

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,84 @@ class MatplotlibDeprecationWarning(UserWarning):
4545
mplDeprecation = MatplotlibDeprecationWarning
4646

4747

48+
def _generate_deprecation_message(
49+
since, message='', name='', alternative='', pending=False,
50+
obj_type='attribute'):
51+
52+
if not message:
53+
altmessage = ''
54+
55+
if pending:
56+
message = (
57+
'The %(func)s %(obj_type)s will be deprecated in a '
58+
'future version.')
59+
else:
60+
message = (
61+
'The %(func)s %(obj_type)s was deprecated in version '
62+
'%(since)s.')
63+
if alternative:
64+
altmessage = ' Use %s instead.' % alternative
65+
66+
message = ((message % {
67+
'func': name,
68+
'name': name,
69+
'alternative': alternative,
70+
'obj_type': obj_type,
71+
'since': since}) +
72+
altmessage)
73+
74+
return message
75+
76+
77+
def warn_deprecated(
78+
since, message='', name='', alternative='', pending=False,
79+
obj_type='attribute'):
80+
"""
81+
Used display deprecation warning in a standard way.
82+
83+
Parameters
84+
------------
85+
since : str
86+
The release at which this API became deprecated. This is
87+
required.
88+
89+
message : str, optional
90+
Override the default deprecation message. The format
91+
specifier `%(func)s` may be used for the name of the function,
92+
and `%(alternative)s` may be used in the deprecation message
93+
to insert the name of an alternative to the deprecated
94+
function. `%(obj_type)` may be used to insert a friendly name
95+
for the type of object being deprecated.
96+
97+
name : str, optional
98+
The name of the deprecated function; if not provided the name
99+
is automatically determined from the passed in function,
100+
though this is useful in the case of renamed functions, where
101+
the new function is just assigned to the name of the
102+
deprecated function. For example::
103+
104+
def new_function():
105+
...
106+
oldFunction = new_function
107+
108+
alternative : str, optional
109+
An alternative function that the user may use in place of the
110+
deprecated function. The deprecation warning will tell the user about
111+
this alternative if provided.
112+
113+
pending : bool, optional
114+
If True, uses a PendingDeprecationWarning instead of a
115+
DeprecationWarning.
116+
117+
obj_type : str, optional
118+
The object type being deprecated.
119+
"""
120+
message = _generate_deprecation_message(
121+
since, message, name, alternative, pending, 'function')
122+
123+
warnings.warn(message, mplDeprecation, stacklevel=1)
124+
125+
48126
def deprecated(since, message='', name='', alternative='', pending=False,
49127
obj_type='function'):
50128
"""
@@ -84,7 +162,6 @@ def new_function():
84162
If True, uses a PendingDeprecationWarning instead of a
85163
DeprecationWarning.
86164
"""
87-
88165
def deprecate(func, message=message, name=name, alternative=alternative,
89166
pending=pending):
90167
import functools
@@ -112,23 +189,8 @@ def deprecate(func, message=message, name=name, alternative=alternative,
112189
if not name:
113190
name = func.__name__
114191

115-
altmessage = ''
116-
if not message or type(message) == type(deprecate):
117-
if pending:
118-
message = ('The %(func)s %(obj_type)s will be deprecated in a '
119-
'future version.')
120-
else:
121-
message = ('The %(func)s %(obj_type)s is deprecated and may '
122-
'be removed in a future version.')
123-
if alternative:
124-
altmessage = '\n Use %s instead.' % alternative
125-
126-
message = ((message % {
127-
'func': name,
128-
'name': name,
129-
'alternative': alternative,
130-
'obj_type': obj_type}) +
131-
altmessage)
192+
message = _generate_deprecation_message(
193+
since, message, name, alternative, pending, 'function')
132194

133195
@functools.wraps(func)
134196
def deprecated_func(*args, **kwargs):
@@ -140,12 +202,10 @@ def deprecated_func(*args, **kwargs):
140202
if not old_doc:
141203
old_doc = ''
142204
old_doc = textwrap.dedent(old_doc).strip('\n')
143-
altmessage = altmessage.strip()
144-
if not altmessage:
145-
altmessage = message.strip()
205+
message = message.strip()
146206
new_doc = (('\n.. deprecated:: %(since)s'
147207
'\n %(message)s\n\n' %
148-
{'since': since, 'message': altmessage.strip()}) + old_doc)
208+
{'since': since, 'message': message}) + old_doc)
149209
if not old_doc:
150210
# This is to prevent a spurious 'unexected unindent' warning from
151211
# docutils when the original docstring was blank.
@@ -157,9 +217,6 @@ def deprecated_func(*args, **kwargs):
157217
deprecated_func = classmethod(deprecated_func)
158218
return deprecated_func
159219

160-
if type(message) == type(deprecate):
161-
return deprecate(message)
162-
163220
return deprecate
164221

165222

@@ -405,15 +462,12 @@ class CallbackRegistry:
405462
functions). This technique was shared by Peter Parente on his
406463
`"Mindtrove" blog
407464
<http://mindtrove.info/articles/python-weak-references/>`_.
408-
409-
.. deprecated:: 1.3.0
410465
"""
411466
def __init__(self, *args):
412467
if len(args):
413-
warnings.warn(
468+
warn_deprecated('1.3', message=
414469
"CallbackRegistry no longer requires a list of callback "
415-
"types. Ignoring arguments. *args will be removed in 1.5",
416-
mplDeprecation)
470+
"types. Ignoring arguments. *args will be removed in 1.5")
417471
self.callbacks = dict()
418472
self._cid = 0
419473
self._func_cid_map = {}

lib/matplotlib/mpl.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
.. note:: Deprecated in 1.3
33
"""
44
import warnings
5-
from matplotlib.cbook import mplDeprecation
6-
warnings.warn(
7-
"matplotlib.mpl is deprecated and will be removed in version 1.4."
8-
"Please use `import matplotlib as mpl` instead", mplDeprecation)
5+
from matplotlib import cbook
6+
cbook.warn_deprecated(
7+
'1.3', 'matplotlib.mpl', alterative='`import matplotlib as mpl`',
8+
obj_type='module')
99
from matplotlib import artist
1010
from matplotlib import axis
1111
from matplotlib import axes
12-
from matplotlib import cbook
1312
from matplotlib import collections
1413
from matplotlib import colors
1514
from matplotlib import colorbar

0 commit comments

Comments
 (0)