@@ -45,6 +45,84 @@ class MatplotlibDeprecationWarning(UserWarning):
4545mplDeprecation = 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+
48126def 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 = {}
0 commit comments