PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/django/utils/deprecation.py

https://gitlab.com/briankiragu/django
Python | 130 lines | 84 code | 31 blank | 15 comment | 9 complexity | cf47216e567f4c61ed61d129d0814866 MD5 | raw file
  1. from __future__ import absolute_import
  2. import inspect
  3. import warnings
  4. class RemovedInDjango20Warning(DeprecationWarning):
  5. pass
  6. class RemovedInDjango21Warning(PendingDeprecationWarning):
  7. pass
  8. RemovedInNextVersionWarning = RemovedInDjango20Warning
  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)
  63. class CallableBool:
  64. """
  65. An boolean-like object that is also callable for backwards compatibility.
  66. """
  67. do_not_call_in_templates = True
  68. def __init__(self, value):
  69. self.value = value
  70. def __bool__(self):
  71. return self.value
  72. def __call__(self):
  73. warnings.warn(
  74. "Using user.is_authenticated() and user.is_anonymous() as a method "
  75. "is deprecated. Remove the parentheses to use it as an attribute.",
  76. RemovedInDjango20Warning, stacklevel=2
  77. )
  78. return self.value
  79. def __nonzero__(self): # Python 2 compatibility
  80. return self.value
  81. def __repr__(self):
  82. return 'CallableBool(%r)' % self.value
  83. CallableFalse = CallableBool(False)
  84. CallableTrue = CallableBool(True)
  85. class MiddlewareMixin(object):
  86. def __init__(self, get_response=None):
  87. self.get_response = get_response
  88. super(MiddlewareMixin, self).__init__()
  89. def __call__(self, request):
  90. response = None
  91. if hasattr(self, 'process_request'):
  92. response = self.process_request(request)
  93. if not response:
  94. response = self.get_response(request)
  95. if hasattr(self, 'process_response'):
  96. response = self.process_response(request, response)
  97. return response