Skip to content

Commit

Permalink
feat: Improve type annotations in utils.py (#413)
Browse files Browse the repository at this point in the history
* feat: Improve type annotations in utils.py

Replace some Anys with proper types.

* fix: Linting issue

* fix: Broken tests because of lint fixes, argh
  • Loading branch information
bluetech authored and untitaker committed Jul 5, 2019
1 parent 518a966 commit d460727
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
from typing import Tuple
from typing import Type
from typing import Union
from types import FrameType
from types import TracebackType

from sentry_sdk.hub import Hub

ExcInfo = Tuple[
Optional[Type[BaseException]], Optional[BaseException], Optional[Any]
Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]
]

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


def get_type_name(cls):
# type: (Any) -> str
# type: (Optional[type]) -> Optional[str]
return getattr(cls, "__qualname__", None) or getattr(cls, "__name__", None)


def get_type_module(cls):
# type: (Any) -> Optional[Any]
# type: (Optional[type]) -> Optional[str]
mod = getattr(cls, "__module__", None)
if mod not in (None, "builtins", "__builtins__"):
return mod
return None


def should_hide_frame(frame):
# type: (Any) -> bool
# type: (FrameType) -> bool
try:
mod = frame.f_globals["__name__"]
if mod.startswith("sentry_sdk."):
Expand All @@ -238,11 +240,12 @@ def should_hide_frame(frame):


def iter_stacks(tb):
# type: (Any) -> Iterator[Any]
while tb is not None:
if not should_hide_frame(tb.tb_frame):
yield tb
tb = tb.tb_next
# type: (Optional[TracebackType]) -> Iterator[TracebackType]
tb_ = tb # type: Optional[TracebackType]
while tb_ is not None:
if not should_hide_frame(tb_.tb_frame):
yield tb_
tb_ = tb_.tb_next


def slim_string(value, length=MAX_STRING_LENGTH):
Expand All @@ -265,7 +268,7 @@ def get_lines_from_file(
source = None
if loader is not None and hasattr(loader, "get_source"):
try:
source_str = loader.get_source(module)
source_str = loader.get_source(module) # type: Optional[str]
except (ImportError, IOError):
source_str = None
if source_str is not None:
Expand Down Expand Up @@ -299,9 +302,9 @@ def get_lines_from_file(


def get_source_context(frame, tb_lineno):
# type: (Any, int) -> Tuple[List[str], Optional[str], List[str]]
# type: (FrameType, int) -> Tuple[List[str], Optional[str], List[str]]
try:
abs_path = frame.f_code.co_filename
abs_path = frame.f_code.co_filename # type: Optional[str]
except Exception:
abs_path = None
try:
Expand Down Expand Up @@ -355,7 +358,10 @@ def safe_repr(value):


def filename_for_module(module, abs_path):
# type: (str, str) -> str
# type: (Optional[str], Optional[str]) -> Optional[str]
if not abs_path or not module:
return abs_path

try:
if abs_path.endswith(".pyc"):
abs_path = abs_path[:-1]
Expand All @@ -373,14 +379,14 @@ def filename_for_module(module, abs_path):


def serialize_frame(frame, tb_lineno=None, with_locals=True):
# type: (Any, Optional[int], bool) -> Dict[str, Any]
# type: (FrameType, Optional[int], bool) -> Dict[str, Any]
f_code = getattr(frame, "f_code", None)
if f_code:
abs_path = frame.f_code.co_filename
function = frame.f_code.co_name
else:
if not f_code:
abs_path = None
function = None
else:
abs_path = frame.f_code.co_filename
function = frame.f_code.co_name
try:
module = frame.f_globals["__name__"]
except Exception:
Expand All @@ -400,14 +406,14 @@ def serialize_frame(frame, tb_lineno=None, with_locals=True):
"pre_context": pre_context,
"context_line": context_line,
"post_context": post_context,
}
} # type: Dict[str, Any]
if with_locals:
rv["vars"] = frame.f_locals
return rv


def stacktrace_from_traceback(tb=None, with_locals=True):
# type: (Any, bool) -> Dict[str, List[Dict[str, Any]]]
# type: (Optional[TracebackType], bool) -> Dict[str, List[Dict[str, Any]]]
return {
"frames": [
serialize_frame(
Expand Down Expand Up @@ -442,7 +448,7 @@ def get_errno(exc_value):
def single_exception_from_error_tuple(
exc_type, # type: Optional[type]
exc_value, # type: Optional[BaseException]
tb, # type: Optional[Any]
tb, # type: Optional[TracebackType]
client_options=None, # type: Optional[dict]
mechanism=None, # type: Optional[Dict[str, Any]]
):
Expand Down

0 comments on commit d460727

Please sign in to comment.