4
4
import sys
5
5
import warnings
6
6
from pprint import pformat
7
+ from typing import Any , Dict , List , Optional , Sequence , Tuple , Union
7
8
8
9
from asgiref .local import Local
10
+ from django .http import QueryDict
9
11
from django .template import Node
10
12
from django .utils .html import format_html
11
- from django .utils .safestring import mark_safe
13
+ from django .utils .safestring import SafeString , mark_safe
12
14
13
- from debug_toolbar import settings as dt_settings
15
+ from debug_toolbar import _stubs as stubs , settings as dt_settings
14
16
15
17
try :
16
18
import threading
21
23
_local_data = Local ()
22
24
23
25
24
- def _is_excluded_frame (frame , excluded_modules ) :
26
+ def _is_excluded_frame (frame : Any , excluded_modules : Optional [ Sequence [ str ]]) -> bool :
25
27
if not excluded_modules :
26
28
return False
27
29
frame_module = frame .f_globals .get ("__name__" )
@@ -34,7 +36,7 @@ def _is_excluded_frame(frame, excluded_modules):
34
36
)
35
37
36
38
37
- def _stack_trace_deprecation_warning ():
39
+ def _stack_trace_deprecation_warning () -> None :
38
40
warnings .warn (
39
41
"get_stack() and tidy_stacktrace() are deprecated in favor of"
40
42
" get_stack_trace()" ,
@@ -43,7 +45,7 @@ def _stack_trace_deprecation_warning():
43
45
)
44
46
45
47
46
- def tidy_stacktrace (stack ) :
48
+ def tidy_stacktrace (stack : List [ stubs . InspectStack ]) -> stubs . TidyStackTrace :
47
49
"""
48
50
Clean up stacktrace and remove all entries that are excluded by the
49
51
HIDE_IN_STACKTRACES setting.
@@ -68,7 +70,7 @@ def tidy_stacktrace(stack):
68
70
return trace
69
71
70
72
71
- def render_stacktrace (trace ) :
73
+ def render_stacktrace (trace : stubs . TidyStackTrace ) -> SafeString :
72
74
show_locals = dt_settings .get_config ()["ENABLE_STACKTRACES_LOCALS" ]
73
75
html = ""
74
76
for abspath , lineno , func , code , locals_ in trace :
@@ -103,7 +105,7 @@ def render_stacktrace(trace):
103
105
return mark_safe (html )
104
106
105
107
106
- def get_template_info ():
108
+ def get_template_info () -> Optional [ Dict [ str , Any ]] :
107
109
template_info = None
108
110
cur_frame = sys ._getframe ().f_back
109
111
try :
@@ -131,7 +133,9 @@ def get_template_info():
131
133
return template_info
132
134
133
135
134
- def get_template_context (node , context , context_lines = 3 ):
136
+ def get_template_context (
137
+ node : Node , context : stubs .RequestContext , context_lines : int = 3
138
+ ) -> Dict [str , Any ]:
135
139
line , source_lines , name = get_template_source_from_exception_info (node , context )
136
140
debug_context = []
137
141
start = max (1 , line - context_lines )
@@ -146,7 +150,9 @@ def get_template_context(node, context, context_lines=3):
146
150
return {"name" : name , "context" : debug_context }
147
151
148
152
149
- def get_template_source_from_exception_info (node , context ):
153
+ def get_template_source_from_exception_info (
154
+ node : Node , context : stubs .RequestContext
155
+ ) -> Tuple [int , List [Tuple [int , str ]], str ]:
150
156
if context .template .origin == node .origin :
151
157
exception_info = context .template .get_exception_info (
152
158
Exception ("DDT" ), node .token
@@ -161,7 +167,7 @@ def get_template_source_from_exception_info(node, context):
161
167
return line , source_lines , name
162
168
163
169
164
- def get_name_from_obj (obj ) :
170
+ def get_name_from_obj (obj : Any ) -> str :
165
171
if hasattr (obj , "__name__" ):
166
172
name = obj .__name__
167
173
else :
@@ -174,7 +180,7 @@ def get_name_from_obj(obj):
174
180
return name
175
181
176
182
177
- def getframeinfo (frame , context = 1 ) :
183
+ def getframeinfo (frame : Any , context : int = 1 ) -> inspect . Traceback :
178
184
"""
179
185
Get information about a frame or traceback object.
180
186
@@ -213,7 +219,9 @@ def getframeinfo(frame, context=1):
213
219
return inspect .Traceback (filename , lineno , frame .f_code .co_name , lines , index )
214
220
215
221
216
- def get_sorted_request_variable (variable ):
222
+ def get_sorted_request_variable (
223
+ variable : Union [Dict [str , Any ], QueryDict ]
224
+ ) -> Dict [str , Union [List [Tuple [str , Any ]], Any ]]:
217
225
"""
218
226
Get a data structure for showing a sorted list of variables from the
219
227
request data.
@@ -227,7 +235,7 @@ def get_sorted_request_variable(variable):
227
235
return {"raw" : variable }
228
236
229
237
230
- def get_stack (context = 1 ):
238
+ def get_stack (context = 1 ) -> List [ stubs . InspectStack ] :
231
239
"""
232
240
Get a list of records for a frame and all higher (calling) frames.
233
241
@@ -280,7 +288,13 @@ def get_source_file(self, frame):
280
288
281
289
return value
282
290
283
- def get_stack_trace (self , * , excluded_modules = None , include_locals = False , skip = 0 ):
291
+ def get_stack_trace (
292
+ self ,
293
+ * ,
294
+ excluded_modules : Optional [Sequence [str ]] = None ,
295
+ include_locals : bool = False ,
296
+ skip : int = 0 ,
297
+ ):
284
298
trace = []
285
299
skip += 1 # Skip the frame for this method.
286
300
for frame in _stack_frames (skip = skip ):
0 commit comments