You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

deprecation.py 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import absolute_import
  2. import inspect
  3. import warnings
  4. class RemovedInDjango20Warning(PendingDeprecationWarning):
  5. pass
  6. class RemovedInDjango110Warning(DeprecationWarning):
  7. pass
  8. RemovedInNextVersionWarning = RemovedInDjango110Warning
  9. class warn_about_renamed_method(object):
  10. def __init__(self, class_name, old_method_name, new_method_name, deprecation_warning):
  11. self.class_name = class_name
  12. self.old_method_name = old_method_name
  13. self.new_method_name = new_method_name
  14. self.deprecation_warning = deprecation_warning
  15. def __call__(self, f):
  16. def wrapped(*args, **kwargs):
  17. warnings.warn(
  18. "`%s.%s` is deprecated, use `%s` instead." %
  19. (self.class_name, self.old_method_name, self.new_method_name),
  20. self.deprecation_warning, 2)
  21. return f(*args, **kwargs)
  22. return wrapped
  23. class RenameMethodsBase(type):
  24. """
  25. Handles the deprecation paths when renaming a method.
  26. It does the following:
  27. 1) Define the new method if missing and complain about it.
  28. 2) Define the old method if missing.
  29. 3) Complain whenever an old method is called.
  30. See #15363 for more details.
  31. """
  32. renamed_methods = ()
  33. def __new__(cls, name, bases, attrs):
  34. new_class = super(RenameMethodsBase, cls).__new__(cls, name, bases, attrs)
  35. for base in inspect.getmro(new_class):
  36. class_name = base.__name__
  37. for renamed_method in cls.renamed_methods:
  38. old_method_name = renamed_method[0]
  39. old_method = base.__dict__.get(old_method_name)
  40. new_method_name = renamed_method[1]
  41. new_method = base.__dict__.get(new_method_name)
  42. deprecation_warning = renamed_method[2]
  43. wrapper = warn_about_renamed_method(class_name, *renamed_method)
  44. # Define the new method if missing and complain about it
  45. if not new_method and old_method:
  46. warnings.warn(
  47. "`%s.%s` method should be renamed `%s`." %
  48. (class_name, old_method_name, new_method_name),
  49. deprecation_warning, 2)
  50. setattr(base, new_method_name, old_method)
  51. setattr(base, old_method_name, wrapper(old_method))
  52. # Define the old method as a wrapped call to the new method.
  53. if not old_method and new_method:
  54. setattr(base, old_method_name, wrapper(new_method))
  55. return new_class
  56. class DeprecationInstanceCheck(type):
  57. def __instancecheck__(self, instance):
  58. warnings.warn(
  59. "`%s` is deprecated, use `%s` instead." % (self.__name__, self.alternative),
  60. self.deprecation_warning, 2
  61. )
  62. return super(DeprecationInstanceCheck, self).__instancecheck__(instance)