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

Skip to content

Commit 51a11c2

Browse files
committed
BUG,ENH: make deprecated decorator work (and more flexibly)
1) Fix the bug in which _generate_deprecation_message was failing to substitute arguments for string formats in user-supplied message argument. 2) Make internal naming consistent: the argument is "name", and now the string format specifications also use "name", not "func". 3) Add an "addendum" kwarg so that the standard message, with its convenient argument substitution, can be used, and additional explanation can be appended as a plain text string. Closes #7647.
1 parent 58a8334 commit 51a11c2

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

lib/matplotlib/cbook.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,41 @@ class MatplotlibDeprecationWarning(UserWarning):
5050

5151
def _generate_deprecation_message(since, message='', name='',
5252
alternative='', pending=False,
53-
obj_type='attribute'):
53+
obj_type='attribute',
54+
addendum=''):
5455

5556
if not message:
56-
altmessage = ''
5757

5858
if pending:
5959
message = (
60-
'The %(func)s %(obj_type)s will be deprecated in a '
60+
'The %(name)s %(obj_type)s will be deprecated in a '
6161
'future version.')
6262
else:
6363
message = (
64-
'The %(func)s %(obj_type)s was deprecated in version '
64+
'The %(name)s %(obj_type)s was deprecated in version '
6565
'%(since)s.')
66-
if alternative:
67-
altmessage = ' Use %s instead.' % alternative
6866

69-
message = ((message % {
70-
'func': name,
71-
'name': name,
72-
'alternative': alternative,
73-
'obj_type': obj_type,
74-
'since': since}) +
75-
altmessage)
67+
altmessage = ''
68+
if alternative:
69+
altmessage = ' Use %s instead.' % alternative
70+
71+
message = ((message % {
72+
'func': name,
73+
'name': name,
74+
'alternative': alternative,
75+
'obj_type': obj_type,
76+
'since': since}) +
77+
altmessage)
78+
79+
if addendum:
80+
message += addendum
7681

7782
return message
7883

7984

8085
def warn_deprecated(
8186
since, message='', name='', alternative='', pending=False,
82-
obj_type='attribute'):
87+
obj_type='attribute', addendum=''):
8388
"""
8489
Used to display deprecation warning in a standard way.
8590

@@ -90,7 +95,7 @@ def warn_deprecated(
9095

9196
message : str, optional
9297
Override the default deprecation message. The format
93-
specifier `%(func)s` may be used for the name of the function,
98+
specifier `%(name)s` may be used for the name of the function,
9499
and `%(alternative)s` may be used in the deprecation message
95100
to insert the name of an alternative to the deprecated
96101
function. `%(obj_type)s` may be used to insert a friendly name
@@ -111,6 +116,9 @@ def warn_deprecated(
111116
obj_type : str, optional
112117
The object type being deprecated.
113118

119+
addendum : str, optional
120+
Additional text appended directly to the final message.
121+
114122
Examples
115123
--------
116124

@@ -122,13 +130,13 @@ def warn_deprecated(
122130

123131
"""
124132
message = _generate_deprecation_message(
125-
since, message, name, alternative, pending, obj_type)
133+
since, message, name, alternative, pending, obj_type)
126134

127135
warnings.warn(message, mplDeprecation, stacklevel=1)
128136

129137

130138
def deprecated(since, message='', name='', alternative='', pending=False,
131-
obj_type=None):
139+
obj_type=None, addendum=''):
132140
"""
133141
Decorator to mark a function or a class as deprecated.
134142

@@ -140,15 +148,15 @@ def deprecated(since, message='', name='', alternative='', pending=False,
140148

141149
message : str, optional
142150
Override the default deprecation message. The format
143-
specifier `%(func)s` may be used for the name of the function,
151+
specifier `%(name)s` may be used for the name of the object,
144152
and `%(alternative)s` may be used in the deprecation message
145153
to insert the name of an alternative to the deprecated
146-
function. `%(obj_type)s` may be used to insert a friendly name
154+
object. `%(obj_type)s` may be used to insert a friendly name
147155
for the type of object being deprecated.
148156

149157
name : str, optional
150-
The name of the deprecated function; if not provided the name
151-
is automatically determined from the passed in function,
158+
The name of the deprecated object; if not provided the name
159+
is automatically determined from the passed in object,
152160
though this is useful in the case of renamed functions, where
153161
the new function is just assigned to the name of the
154162
deprecated function. For example::
@@ -158,14 +166,17 @@ def new_function():
158166
oldFunction = new_function
159167

160168
alternative : str, optional
161-
An alternative function that the user may use in place of the
162-
deprecated function. The deprecation warning will tell the user
169+
An alternative object that the user may use in place of the
170+
deprecated object. The deprecation warning will tell the user
163171
about this alternative if provided.
164172

165173
pending : bool, optional
166174
If True, uses a PendingDeprecationWarning instead of a
167175
DeprecationWarning.
168176

177+
addendum : str, optional
178+
Additional text appended directly to the final message.
179+
169180
Examples
170181
--------
171182

@@ -177,7 +188,7 @@ def the_function_to_deprecate():
177188

178189
"""
179190
def deprecate(obj, message=message, name=name, alternative=alternative,
180-
pending=pending):
191+
pending=pending, addendum=addendum):
181192
import textwrap
182193

183194
if not name:
@@ -212,7 +223,8 @@ def finalize(wrapper, new_doc):
212223
return wrapper
213224

214225
message = _generate_deprecation_message(
215-
since, message, name, alternative, pending, obj_type)
226+
since, message, name, alternative, pending,
227+
obj_type, addendum)
216228

217229
def wrapper(*args, **kwargs):
218230
warnings.warn(message, mplDeprecation, stacklevel=2)

0 commit comments

Comments
 (0)