Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve type annotations in utils.py #413

Merged
merged 3 commits into from
Jul 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 23 additions & 20 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 @@ -373,14 +376,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 +403,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 +445,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