Skip to content

Commit 6d1598e

Browse files
committed
Fixed support for staticmethods in deprecated() and fixed an issue where the full deprecation message was not being displayed
1 parent 13ab02f commit 6d1598e

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

‎astropy/utils/misc.py‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import unicodedata
2323
import warnings
2424

25-
from .exceptions import AstropyDeprecationWarning, AstropyPendingDeprecationWarning
25+
from .exceptions import (AstropyDeprecationWarning,
26+
AstropyPendingDeprecationWarning)
2627

2728
from ..extern import six
2829
from ..extern.six.moves import urllib
@@ -372,6 +373,9 @@ def new_function():
372373
The type of this object, if the automatically determined one
373374
needs to be overridden.
374375
"""
376+
377+
method_types = (classmethod, staticmethod)
378+
375379
def deprecate_doc(old_doc, message):
376380
"""
377381
Returns a given docstring with a deprecation message prepended
@@ -391,9 +395,10 @@ def deprecate_doc(old_doc, message):
391395

392396
def get_function(func):
393397
"""
394-
Given a function or classmethod, get the function object.
398+
Given a function or classmethod (or other function wrapper type), get
399+
the function object.
395400
"""
396-
if isinstance(func, classmethod):
401+
if isinstance(func, method_types):
397402
try:
398403
func = func.__func__
399404
except AttributeError:
@@ -406,7 +411,7 @@ def get_function(func):
406411
func = method.im_func
407412
else:
408413
# Nothing we can do really... just return the original
409-
# classmethod
414+
# classmethod, etc.
410415
return func
411416
return func
412417

@@ -415,7 +420,12 @@ def deprecate_function(func, message):
415420
Returns a wrapped function that displays an
416421
``AstropyDeprecationWarning`` when it is called.
417422
"""
418-
is_classmethod = isinstance(func, classmethod)
423+
424+
if isinstance(func, method_types):
425+
func_wrapper = type(func)
426+
else:
427+
func_wrapper = lambda f: f
428+
419429
func = get_function(func)
420430

421431
def deprecated_func(*args, **kwargs):
@@ -438,9 +448,7 @@ def deprecated_func(*args, **kwargs):
438448
deprecated_func.__doc__ = deprecate_doc(
439449
deprecated_func.__doc__, message)
440450

441-
if is_classmethod:
442-
return classmethod(deprecated_func)
443-
return deprecated_func
451+
return func_wrapper(deprecated_func)
444452

445453
def deprecate_class(cls, message):
446454
"""
@@ -487,7 +495,7 @@ def deprecate(obj, message=message, name=name, alternative=alternative,
487495
obj_type_name = 'class'
488496
elif inspect.isfunction(obj):
489497
obj_type_name = 'function'
490-
elif inspect.ismethod(obj):
498+
elif inspect.ismethod(obj) or isinstance(obj, method_types):
491499
obj_type_name = 'method'
492500
else:
493501
obj_type_name = 'object'
@@ -515,13 +523,10 @@ def deprecate(obj, message=message, name=name, alternative=alternative,
515523
'obj_type': obj_type_name}) +
516524
altmessage)
517525

518-
if not altmessage:
519-
altmessage = message
520-
521526
if isinstance(obj, type):
522-
return deprecate_class(obj, altmessage)
527+
return deprecate_class(obj, message)
523528
else:
524-
return deprecate_function(obj, altmessage)
529+
return deprecate_function(obj, message)
525530

526531
if type(message) == type(deprecate):
527532
return deprecate(message)

0 commit comments

Comments
 (0)