@@ -14,7 +14,6 @@ class MatplotlibDeprecationWarning(UserWarning):
14
14
15
15
https://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x
16
16
"""
17
- pass
18
17
19
18
20
19
mplDeprecation = MatplotlibDeprecationWarning
@@ -28,29 +27,34 @@ def _generate_deprecation_message(
28
27
removal = {"2.2" : "in 3.1" , "3.0" : "in 3.2" }.get (
29
28
since , "two minor releases later" )
30
29
elif removal :
30
+ if pending :
31
+ raise ValueError (
32
+ "A pending deprecation cannot have a scheduled removal" )
31
33
removal = "in {}" .format (removal )
32
34
33
35
if not message :
34
36
message = (
35
- "The { name} { obj_type} "
37
+ "The %( name)s %( obj_type)s "
36
38
+ (" will be deprecated in a future version"
37
39
if pending else
38
- (" was deprecated in Matplotlib { since} "
39
- + (" and will be removed { removal} "
40
+ (" was deprecated in Matplotlib %( since)s "
41
+ + (" and will be removed %( removal)s "
40
42
if removal else
41
43
"" )))
42
44
+ "."
43
- + (" Use { alternative} instead." if alternative else "" ))
45
+ + (" Use %( alternative)s instead." if alternative else "" ))
44
46
45
- return message .format (func = name , name = name , obj_type = obj_type , since = since ,
46
- removal = removal , alternative = alternative )
47
+ return (
48
+ message % dict (func = name , name = name , obj_type = obj_type , since = since ,
49
+ removal = removal , alternative = alternative )
50
+ + addendum )
47
51
48
52
49
53
def warn_deprecated (
50
54
since , message = '' , name = '' , alternative = '' , pending = False ,
51
55
obj_type = 'attribute' , addendum = '' , * , removal = '' ):
52
56
"""
53
- Used to display deprecation warning in a standard way.
57
+ Used to display deprecation in a standard way.
54
58
55
59
Parameters
56
60
----------
@@ -69,18 +73,19 @@ def warn_deprecated(
69
73
The name of the deprecated object.
70
74
71
75
alternative : str, optional
72
- An alternative function that the user may use in place of the
73
- deprecated function . The deprecation warning will tell the user
74
- about this alternative if provided.
76
+ An alternative API that the user may use in place of the deprecated
77
+ API . The deprecation warning will tell the user about this alternative
78
+ if provided.
75
79
76
80
pending : bool, optional
77
81
If True, uses a PendingDeprecationWarning instead of a
78
- DeprecationWarning.
82
+ DeprecationWarning. Cannot be used together with *removal*.
79
83
80
84
removal : str, optional
81
85
The expected removal version. With the default (an empty string), a
82
86
removal version is automatically computed from *since*. Set to other
83
- Falsy values to not schedule a removal date.
87
+ Falsy values to not schedule a removal date. Cannot be used together
88
+ with *pending*.
84
89
85
90
obj_type : str, optional
86
91
The object type being deprecated.
@@ -101,7 +106,9 @@ def warn_deprecated(
101
106
message = _generate_deprecation_message (
102
107
since , message , name , alternative , pending , obj_type , removal = removal )
103
108
message = '\n ' + message
104
- warnings .warn (message , mplDeprecation , stacklevel = 2 )
109
+ category = (PendingDeprecationWarning if pending
110
+ else MatplotlibDeprecationWarning )
111
+ warnings .warn (message , category , stacklevel = 2 )
105
112
106
113
107
114
def deprecated (since , message = '' , name = '' , alternative = '' , pending = False ,
@@ -120,8 +127,7 @@ def deprecated(since, message='', name='', alternative='', pending=False,
120
127
specifier `%(name)s` may be used for the name of the object,
121
128
and `%(alternative)s` may be used in the deprecation message
122
129
to insert the name of an alternative to the deprecated
123
- object. `%(obj_type)s` may be used to insert a friendly name
124
- for the type of object being deprecated.
130
+ object.
125
131
126
132
name : str, optional
127
133
The name of the deprecated object; if not provided the name
@@ -135,18 +141,19 @@ def new_function():
135
141
oldFunction = new_function
136
142
137
143
alternative : str, optional
138
- An alternative object that the user may use in place of the
139
- deprecated object . The deprecation warning will tell the user
140
- about this alternative if provided.
144
+ An alternative API that the user may use in place of the deprecated
145
+ API . The deprecation warning will tell the user about this alternative
146
+ if provided.
141
147
142
148
pending : bool, optional
143
149
If True, uses a PendingDeprecationWarning instead of a
144
- DeprecationWarning.
150
+ DeprecationWarning. Cannot be used together with *removal*.
145
151
146
152
removal : str, optional
147
153
The expected removal version. With the default (an empty string), a
148
154
removal version is automatically computed from *since*. Set to other
149
- Falsy values to not schedule a removal date.
155
+ Falsy values to not schedule a removal date. Cannot be used together
156
+ with *pending*.
150
157
151
158
addendum : str, optional
152
159
Additional text appended directly to the final message.
@@ -159,9 +166,14 @@ def new_function():
159
166
@deprecated('1.4.0')
160
167
def the_function_to_deprecate():
161
168
pass
162
-
163
169
"""
164
170
171
+ if obj_type is not None :
172
+ warn_deprecated (
173
+ "3.0" , "Passing 'obj_type' to the 'deprecated' decorator has no "
174
+ "effect, and is deprecated since Matplotlib %(since)s; support "
175
+ "for it will be removed %(removal)s." )
176
+
165
177
def deprecate (obj , message = message , name = name , alternative = alternative ,
166
178
pending = pending , addendum = addendum ):
167
179
@@ -174,12 +186,7 @@ def deprecate(obj, message=message, name=name, alternative=alternative,
174
186
func = obj .__init__
175
187
176
188
def finalize (wrapper , new_doc ):
177
- try :
178
- obj .__doc__ = new_doc
179
- except (AttributeError , TypeError ):
180
- # cls.__doc__ is not writeable on Py2.
181
- # TypeError occurs on PyPy
182
- pass
189
+ obj .__doc__ = new_doc
183
190
obj .__init__ = wrapper
184
191
return obj
185
192
else :
@@ -204,9 +211,11 @@ def finalize(wrapper, new_doc):
204
211
message = _generate_deprecation_message (
205
212
since , message , name , alternative , pending , obj_type , addendum ,
206
213
removal = removal )
214
+ category = (PendingDeprecationWarning if pending
215
+ else MatplotlibDeprecationWarning )
207
216
208
217
def wrapper (* args , ** kwargs ):
209
- warnings .warn (message , mplDeprecation , stacklevel = 2 )
218
+ warnings .warn (message , category , stacklevel = 2 )
210
219
return func (* args , ** kwargs )
211
220
212
221
old_doc = textwrap .dedent (old_doc or '' ).strip ('\n ' )
0 commit comments