8000 Don't pretend @deprecated applies to classmethods. · matplotlib/matplotlib@167ede8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 167ede8

Browse files
committed
Don't pretend @deprecated applies to classmethods.
The `@deprecated` decorator has a branch for handling deprecation of classmethods, but that won't ever work because we start by getting the `__name__` attribute on the object, which raises an AttributeError on classmethods. This could be fixed by fetching `obj.__func__.__name__` instead, but given that one can as well put the `@deprecated` decorator *under* the `@classmethod` decorator (so that it sees the underlying function), we can just remove that non-functioning code. Also switch some docstring formatting to str.format.
1 parent ef04ad9 commit 167ede8

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

lib/matplotlib/cbook/deprecation.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ def deprecated(since, message='', name='', alternative='', pending=False,
117117
"""
118118
Decorator to mark a function or a class as deprecated.
119119
120+
When deprecating a classmethod, a staticmethod, or a property, the
121+
``@deprecated`` decorator should go *under* the ``@classmethod``, etc.
122+
decorator (i.e., `deprecated` should directly decorate the underlying
123+
callable).
124+
120125
Parameters
121126
----------
122127
since : str
@@ -183,31 +188,22 @@ def deprecate(obj, message=message, name=name, alternative=alternative,
183188

184189
if isinstance(obj, type):
185190
obj_type = "class"
186-
old_doc = obj.__doc__
187191
func = obj.__init__
192+
old_doc = obj.__doc__
188193

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

212208
message = _generate_deprecation_message(
213209
since, message, name, alternative, pending, obj_type, addendum,
@@ -221,9 +217,11 @@ def wrapper(*args, **kwargs):
221217

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

0 commit comments

Comments
 (0)
0