Debian Codegraph @

django/utils/deprecation.py

raw
1 import inspect
2 import warnings
3
4 from asgiref.sync import iscoroutinefunction, markcoroutinefunction, sync_to_async
5
6
7 class RemovedInDjango60Warning(DeprecationWarning):
8 pass
9
10
11 class RemovedInDjango61Warning(PendingDeprecationWarning):
12 pass
13
14
15 RemovedInNextVersionWarning = RemovedInDjango60Warning
16 RemovedAfterNextVersionWarning = RemovedInDjango61Warning
17
18
19 class warn_about_renamed_method:
20 def __init__(
21 self, class_name, old_method_name, new_method_name, deprecation_warning
22 ):
23 self.class_name = class_name
24 self.old_method_name = old_method_name
25 self.new_method_name = new_method_name
26 self.deprecation_warning = deprecation_warning
27
28 def __call__(self, f):
29 def wrapper(*args, **kwargs):
30 warnings.warn(
31 "`%s.%s` is deprecated, use `%s` instead."
32 % (self.class_name, self.old_method_name, self.new_method_name),
33 self.deprecation_warning,
34 2,
35 )
36 return f(*args, **kwargs)
37
38 return wrapper
39
40
41 class RenameMethodsBase(type):
42 """
43 Handles the deprecation paths when renaming a method.
44
45 It does the following:
46 1) Define the new method if missing and complain about it.
47 2) Define the old method if missing.
48 3) Complain whenever an old method is called.
49
50 See #15363 for more details.
51 """
52
53 renamed_methods = ()
54
55 def __new__(cls, name, bases, attrs):
56 new_class = super().__new__(cls, name, bases, attrs)
57
58 for base in inspect.getmro(new_class):
59 class_name = base.__name__
60 for renamed_method in cls.renamed_methods:
61 old_method_name = renamed_method[0]
62 old_method = base.__dict__.get(old_method_name)
63 new_method_name = renamed_method[1]
64 new_method = base.__dict__.get(new_method_name)
65 deprecation_warning = renamed_method[2]
66 wrapper = warn_about_renamed_method(class_name, *renamed_method)
67
68 # Define the new method if missing and complain about it
69 if not new_method and old_method:
70 warnings.warn(
71 "`%s.%s` method should be renamed `%s`."
72 % (class_name, old_method_name, new_method_name),
73 deprecation_warning,
74 2,
75 )
76 setattr(base, new_method_name, old_method)
77 setattr(base, old_method_name, wrapper(old_method))
78
79 # Define the old method as a wrapped call to the new method.
80 if not old_method and new_method:
81 setattr(base, old_method_name, wrapper(new_method))
82
83 return new_class
84
85
86 class MiddlewareMixin:
87 sync_capable = True
88 async_capable = True
89
90 def __init__(self, get_response):
91 if get_response is None:
92 raise ValueError("get_response must be provided.")
93 self.get_response = get_response
94 # If get_response is a coroutine function, turns us into async mode so
95 # a thread is not consumed during a whole request.
96 self.async_mode = iscoroutinefunction(self.get_response)
97 if self.async_mode:
98 # Mark the class as async-capable, but do the actual switch inside
99 # __call__ to avoid swapping out dunder methods.
100 markcoroutinefunction(self)
101 super().__init__()
102
103 def __repr__(self):
104 return "<%s get_response=%s>" % (
105 self.__class__.__qualname__,
106 getattr(
107 self.get_response,
108 "__qualname__",
109 self.get_response.__class__.__name__,
110 ),
111 )
112
113 def __call__(self, request):
114 # Exit out to async mode, if needed
115 if self.async_mode:
116 return self.__acall__(request)
117 response = None
118 if hasattr(self, "process_request"):
119 response = self.process_request(request)
120 response = response or self.get_response(request)
121 if hasattr(self, "process_response"):
122 response = self.process_response(request, response)
123 return response
124
125 async def __acall__(self, request):
126 """
127 Async version of __call__ that is swapped in when an async request
128 is running.
129 """
130 response = None
131 if hasattr(self, "process_request"):
132 response = await sync_to_async(
133 self.process_request,
134 thread_sensitive=True,
135 )(request)
136 response = response or await self.get_response(request)
137 if hasattr(self, "process_response"):
138 response = await sync_to_async(
139 self.process_response,
140 thread_sensitive=True,
141 )(request, response)
142 return response