Skip to content

Commit d460727

Browse files
bluetechuntitaker
authored andcommitted
feat: Improve type annotations in utils.py (getsentry#413)
* feat: Improve type annotations in utils.py Replace some Anys with proper types. * fix: Linting issue * fix: Broken tests because of lint fixes, argh
1 parent 518a966 commit d460727

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

sentry_sdk/utils.py

+27-21
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
from typing import Tuple
2121
from typing import Type
2222
from typing import Union
23+
from types import FrameType
24+
from types import TracebackType
2325

2426
from sentry_sdk.hub import Hub
2527

2628
ExcInfo = Tuple[
27-
Optional[Type[BaseException]], Optional[BaseException], Optional[Any]
29+
Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]
2830
]
2931

3032
Event = Dict[str, Any]
@@ -206,20 +208,20 @@ def __init__(self, value, metadata):
206208

207209

208210
def get_type_name(cls):
209-
# type: (Any) -> str
211+
# type: (Optional[type]) -> Optional[str]
210212
return getattr(cls, "__qualname__", None) or getattr(cls, "__name__", None)
211213

212214

213215
def get_type_module(cls):
214-
# type: (Any) -> Optional[Any]
216+
# type: (Optional[type]) -> Optional[str]
215217
mod = getattr(cls, "__module__", None)
216218
if mod not in (None, "builtins", "__builtins__"):
217219
return mod
218220
return None
219221

220222

221223
def should_hide_frame(frame):
222-
# type: (Any) -> bool
224+
# type: (FrameType) -> bool
223225
try:
224226
mod = frame.f_globals["__name__"]
225227
if mod.startswith("sentry_sdk."):
@@ -238,11 +240,12 @@ def should_hide_frame(frame):
238240

239241

240242
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
246249

247250

248251
def slim_string(value, length=MAX_STRING_LENGTH):
@@ -265,7 +268,7 @@ def get_lines_from_file(
265268
source = None
266269
if loader is not None and hasattr(loader, "get_source"):
267270
try:
268-
source_str = loader.get_source(module)
271+
source_str = loader.get_source(module) # type: Optional[str]
269272
except (ImportError, IOError):
270273
source_str = None
271274
if source_str is not None:
@@ -299,9 +302,9 @@ def get_lines_from_file(
299302

300303

301304
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]]
303306
try:
304-
abs_path = frame.f_code.co_filename
307+
abs_path = frame.f_code.co_filename # type: Optional[str]
305308
except Exception:
306309
abs_path = None
307310
try:
@@ -355,7 +358,10 @@ def safe_repr(value):
355358

356359

357360
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+
359365
try:
360366
if abs_path.endswith(".pyc"):
361367
abs_path = abs_path[:-1]
@@ -373,14 +379,14 @@ def filename_for_module(module, abs_path):
373379

374380

375381
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]
377383
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:
382385
abs_path = None
383386
function = None
387+
else:
388+
abs_path = frame.f_code.co_filename
389+
function = frame.f_code.co_name
384390
try:
385391
module = frame.f_globals["__name__"]
386392
except Exception:
@@ -400,14 +406,14 @@ def serialize_frame(frame, tb_lineno=None, with_locals=True):
400406
"pre_context": pre_context,
401407
"context_line": context_line,
402408
"post_context": post_context,
403-
}
409+
} # type: Dict[str, Any]
404410
if with_locals:
405411
rv["vars"] = frame.f_locals
406412
return rv
407413

408414

409415
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]]]
411417
return {
412418
"frames": [
413419
serialize_frame(
@@ -442,7 +448,7 @@ def get_errno(exc_value):
442448
def single_exception_from_error_tuple(
443449
exc_type, # type: Optional[type]
444450
exc_value, # type: Optional[BaseException]
445-
tb, # type: Optional[Any]
451+
tb, # type: Optional[TracebackType]
446452
client_options=None, # type: Optional[dict]
447453
mechanism=None, # type: Optional[Dict[str, Any]]
448454
):

0 commit comments

Comments
 (0)