Skip to content

Commit 0c144fd

Browse files
authored
Merge pull request #12151 from anntzer/classmethod-deprecation
Don't pretend @deprecated applies to classmethods.
2 parents d701d5c + abecfdf commit 0c144fd

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

lib/matplotlib/cbook/deprecation.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import functools
2-
import textwrap
2+
import inspect
33
import warnings
44

55

@@ -118,6 +118,11 @@ def deprecated(since, message='', name='', alternative='', pending=False,
118118
"""
119119
Decorator to mark a function or a class as deprecated.
120120
121+
When deprecating a classmethod, a staticmethod, or a property, the
122+
``@deprecated`` decorator should go *under* the ``@classmethod``, etc.
123+
decorator (i.e., `deprecated` should directly decorate the underlying
124+
callable).
125+
121126
Parameters
122127
----------
123128
since : str
@@ -184,31 +189,22 @@ def deprecate(obj, message=message, name=name, alternative=alternative,
184189

185190
if isinstance(obj, type):
186191
obj_type = "class"
187-
old_doc = obj.__doc__
188192
func = obj.__init__
193+
old_doc = obj.__doc__
189194

190195
def finalize(wrapper, new_doc):
191196
obj.__doc__ = new_doc
192197
obj.__init__ = wrapper
193198
return obj
194199
else:
195200
obj_type = "function"
196-
if isinstance(obj, classmethod):
197-
func = obj.__func__
198-
old_doc = func.__doc__
199-
200-
def finalize(wrapper, new_doc):
201-
wrapper = functools.wraps(func)(wrapper)
202-
wrapper.__doc__ = new_doc
203-
return classmethod(wrapper)
204-
else:
205-
func = obj
206-
old_doc = func.__doc__
207-
208-
def finalize(wrapper, new_doc):
209-
wrapper = functools.wraps(func)(wrapper)
210-
wrapper.__doc__ = new_doc
211-
return wrapper
201+
func = obj
202+
old_doc = func.__doc__
203+
204+
def finalize(wrapper, new_doc):
205+
wrapper = functools.wraps(func)(wrapper)
206+
wrapper.__doc__ = new_doc
207+
return wrapper
212208

213209
message = _generate_deprecation_message(
214210
since, message, name, alternative, pending, obj_type, addendum,
@@ -221,11 +217,13 @@ def wrapper(*args, **kwargs):
221217
_warn_external(message, category)
222218
return func(*args, **kwargs)
223219

224-
old_doc = textwrap.dedent(old_doc or '').strip('\n')
220+
old_doc = inspect.cleandoc(old_doc or '').strip('\n')
225221
message = message.strip()
226-
new_doc = (('\n.. deprecated:: %(since)s'
227-
'\n %(message)s\n\n' %
228-
{'since': since, 'message': message}) + old_doc)
222+
new_doc = ('.. deprecated:: {since}\n'
223+
' {message}\n'
224+
'\n'
225+
'{old_doc}'
226+
.format(since=since, message=message, old_doc=old_doc))
229227
if not old_doc:
230228
# This is to prevent a spurious 'unexected unindent' warning from
231229
# docutils when the original docstring was blank.

0 commit comments

Comments
 (0)