From 890fc85e58e4e214fda1cad2c389adfaac9ed8cb Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Wed, 7 Aug 2019 12:22:30 +0200 Subject: [PATCH 1/2] fix: Remove duplicate function See #377 --- sentry_sdk/utils.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 49b4a0c76c..54f135e2a8 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -236,15 +236,6 @@ def iter_stacks(tb): tb_ = tb_.tb_next -def slim_string(value, length=MAX_STRING_LENGTH): - # type: (str, int) -> str - if not value: - return value - if len(value) > length: - return value[: length - 3] + "..." - return value[:length] - - def get_lines_from_file( filename, # type: str lineno, # type: int @@ -276,11 +267,11 @@ def get_lines_from_file( try: pre_context = [ - slim_string(line.strip("\r\n")) for line in source[lower_bound:lineno] + strip_string(line.strip("\r\n")) for line in source[lower_bound:lineno] ] - context_line = slim_string(source[lineno].strip("\r\n")) + context_line = strip_string(source[lineno].strip("\r\n")) post_context = [ - slim_string(line.strip("\r\n")) + strip_string(line.strip("\r\n")) for line in source[(lineno + 1) : upper_bound] ] return pre_context, context_line, post_context @@ -652,12 +643,18 @@ def _module_in_set(name, set): return False -def strip_string(value, max_length=512): - # type: (str, int) -> Union[AnnotatedValue, str] +def strip_string(value, max_length=None): + # type: (str, Optional[int]) -> Union[AnnotatedValue, str] # TODO: read max_length from config if not value: return value + + if max_length is None: + # This is intentionally not just the default such that one can patch `MAX_STRING_LENGTH` and affect `strip_string`. + max_length = MAX_STRING_LENGTH + length = len(value) + if length > max_length: return AnnotatedValue( value=value[: max_length - 3] + u"...", From 4bba45a63a3fd32d20bffef21cd7f075b2a5450c Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Thu, 8 Aug 2019 12:13:48 +0200 Subject: [PATCH 2/2] fix: Fix type hints --- sentry_sdk/utils.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 54f135e2a8..fe3302700b 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -195,6 +195,13 @@ def __init__(self, value, metadata): self.metadata = metadata +if MYPY: + from typing import TypeVar + + T = TypeVar("T") + Annotated = Union[AnnotatedValue, T] + + def get_type_name(cls): # type: (Optional[type]) -> Optional[str] return getattr(cls, "__qualname__", None) or getattr(cls, "__name__", None) @@ -242,7 +249,7 @@ def get_lines_from_file( loader=None, # type: Optional[Any] module=None, # type: Optional[str] ): - # type: (...) -> Tuple[List[str], Optional[str], List[str]] + # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]] context_lines = 5 source = None if loader is not None and hasattr(loader, "get_source"): @@ -280,8 +287,11 @@ def get_lines_from_file( return [], None, [] -def get_source_context(frame, tb_lineno): - # type: (FrameType, int) -> Tuple[List[str], Optional[str], List[str]] +def get_source_context( + frame, # type: FrameType + tb_lineno, # type: int +): + # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]] try: abs_path = frame.f_code.co_filename # type: Optional[str] except Exception: