20
20
from typing import Tuple
21
21
from typing import Type
22
22
from typing import Union
23
+ from types import FrameType
24
+ from types import TracebackType
23
25
24
26
from sentry_sdk .hub import Hub
25
27
26
28
ExcInfo = Tuple [
27
- Optional [Type [BaseException ]], Optional [BaseException ], Optional [Any ]
29
+ Optional [Type [BaseException ]], Optional [BaseException ], Optional [TracebackType ]
28
30
]
29
31
30
32
Event = Dict [str , Any ]
@@ -206,20 +208,20 @@ def __init__(self, value, metadata):
206
208
207
209
208
210
def get_type_name (cls ):
209
- # type: (Any ) -> str
211
+ # type: (Optional[type] ) -> Optional[ str]
210
212
return getattr (cls , "__qualname__" , None ) or getattr (cls , "__name__" , None )
211
213
212
214
213
215
def get_type_module (cls ):
214
- # type: (Any ) -> Optional[Any ]
216
+ # type: (Optional[type] ) -> Optional[str ]
215
217
mod = getattr (cls , "__module__" , None )
216
218
if mod not in (None , "builtins" , "__builtins__" ):
217
219
return mod
218
220
return None
219
221
220
222
221
223
def should_hide_frame (frame ):
222
- # type: (Any ) -> bool
224
+ # type: (FrameType ) -> bool
223
225
try :
224
226
mod = frame .f_globals ["__name__" ]
225
227
if mod .startswith ("sentry_sdk." ):
@@ -238,11 +240,12 @@ def should_hide_frame(frame):
238
240
239
241
240
242
def iter_stacks (tb ):
241
- # type: (Any) -> Iterator[Any]
242
- while tb is not None :
243
- if not should_hide_frame (tb .tb_frame ):
244
- yield tb
245
- tb = tb .tb_next
243
+ # type: (Optional[TracebackType]) -> Iterator[TracebackType]
244
+ tb_ = tb # type: Optional[TracebackType]
245
+ while tb_ is not None :
246
+ if not should_hide_frame (tb_ .tb_frame ):
247
+ yield tb_
248
+ tb_ = tb_ .tb_next
246
249
247
250
248
251
def slim_string (value , length = MAX_STRING_LENGTH ):
@@ -265,7 +268,7 @@ def get_lines_from_file(
265
268
source = None
266
269
if loader is not None and hasattr (loader , "get_source" ):
267
270
try :
268
- source_str = loader .get_source (module )
271
+ source_str = loader .get_source (module ) # type: Optional[str]
269
272
except (ImportError , IOError ):
270
273
source_str = None
271
274
if source_str is not None :
@@ -299,9 +302,9 @@ def get_lines_from_file(
299
302
300
303
301
304
def get_source_context (frame , tb_lineno ):
302
- # type: (Any , int) -> Tuple[List[str], Optional[str], List[str]]
305
+ # type: (FrameType , int) -> Tuple[List[str], Optional[str], List[str]]
303
306
try :
304
- abs_path = frame .f_code .co_filename
307
+ abs_path = frame .f_code .co_filename # type: Optional[str]
305
308
except Exception :
306
309
abs_path = None
307
310
try :
@@ -355,7 +358,10 @@ def safe_repr(value):
355
358
356
359
357
360
def filename_for_module (module , abs_path ):
358
- # type: (str, str) -> str
361
+ # type: (Optional[str], Optional[str]) -> Optional[str]
362
+ if not abs_path or not module :
363
+ return abs_path
364
+
359
365
try :
360
366
if abs_path .endswith (".pyc" ):
361
367
abs_path = abs_path [:- 1 ]
@@ -373,14 +379,14 @@ def filename_for_module(module, abs_path):
373
379
374
380
375
381
def serialize_frame (frame , tb_lineno = None , with_locals = True ):
376
- # type: (Any , Optional[int], bool) -> Dict[str, Any]
382
+ # type: (FrameType , Optional[int], bool) -> Dict[str, Any]
377
383
f_code = getattr (frame , "f_code" , None )
378
- if f_code :
379
- abs_path = frame .f_code .co_filename
380
- function = frame .f_code .co_name
381
- else :
384
+ if not f_code :
382
385
abs_path = None
383
386
function = None
387
+ else :
388
+ abs_path = frame .f_code .co_filename
389
+ function = frame .f_code .co_name
384
390
try :
385
391
module = frame .f_globals ["__name__" ]
386
392
except Exception :
@@ -400,14 +406,14 @@ def serialize_frame(frame, tb_lineno=None, with_locals=True):
400
406
"pre_context" : pre_context ,
401
407
"context_line" : context_line ,
402
408
"post_context" : post_context ,
403
- }
409
+ } # type: Dict[str, Any]
404
410
if with_locals :
405
411
rv ["vars" ] = frame .f_locals
406
412
return rv
407
413
408
414
409
415
def stacktrace_from_traceback (tb = None , with_locals = True ):
410
- # type: (Any , bool) -> Dict[str, List[Dict[str, Any]]]
416
+ # type: (Optional[TracebackType] , bool) -> Dict[str, List[Dict[str, Any]]]
411
417
return {
412
418
"frames" : [
413
419
serialize_frame (
@@ -442,7 +448,7 @@ def get_errno(exc_value):
442
448
def single_exception_from_error_tuple (
443
449
exc_type , # type: Optional[type]
444
450
exc_value , # type: Optional[BaseException]
445
- tb , # type: Optional[Any ]
451
+ tb , # type: Optional[TracebackType ]
446
452
client_options = None , # type: Optional[dict]
447
453
mechanism = None , # type: Optional[Dict[str, Any]]
448
454
):
0 commit comments