@@ -195,6 +195,13 @@ def __init__(self, value, metadata):
195195 self .metadata = metadata
196196
197197
198+ if MYPY :
199+ from typing import TypeVar
200+
201+ T = TypeVar ("T" )
202+ Annotated = Union [AnnotatedValue , T ]
203+
204+
198205def get_type_name (cls ):
199206 # type: (Optional[type]) -> Optional[str]
200207 return getattr (cls , "__qualname__" , None ) or getattr (cls , "__name__" , None )
@@ -236,22 +243,13 @@ def iter_stacks(tb):
236243 tb_ = tb_ .tb_next
237244
238245
239- def slim_string (value , length = MAX_STRING_LENGTH ):
240- # type: (str, int) -> str
241- if not value :
242- return value
243- if len (value ) > length :
244- return value [: length - 3 ] + "..."
245- return value [:length ]
246-
247-
248246def get_lines_from_file (
249247 filename , # type: str
250248 lineno , # type: int
251249 loader = None , # type: Optional[Any]
252250 module = None , # type: Optional[str]
253251):
254- # type: (...) -> Tuple[List[str], Optional[str], List[str]]
252+ # type: (...) -> Tuple[List[Annotated[ str]] , Optional[Annotated[ str]] , List[Annotated[ str] ]]
255253 context_lines = 5
256254 source = None
257255 if loader is not None and hasattr (loader , "get_source" ):
@@ -276,11 +274,11 @@ def get_lines_from_file(
276274
277275 try :
278276 pre_context = [
279- slim_string (line .strip ("\r \n " )) for line in source [lower_bound :lineno ]
277+ strip_string (line .strip ("\r \n " )) for line in source [lower_bound :lineno ]
280278 ]
281- context_line = slim_string (source [lineno ].strip ("\r \n " ))
279+ context_line = strip_string (source [lineno ].strip ("\r \n " ))
282280 post_context = [
283- slim_string (line .strip ("\r \n " ))
281+ strip_string (line .strip ("\r \n " ))
284282 for line in source [(lineno + 1 ) : upper_bound ]
285283 ]
286284 return pre_context , context_line , post_context
@@ -289,8 +287,11 @@ def get_lines_from_file(
289287 return [], None , []
290288
291289
292- def get_source_context (frame , tb_lineno ):
293- # type: (FrameType, int) -> Tuple[List[str], Optional[str], List[str]]
290+ def get_source_context (
291+ frame , # type: FrameType
292+ tb_lineno , # type: int
293+ ):
294+ # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
294295 try :
295296 abs_path = frame .f_code .co_filename # type: Optional[str]
296297 except Exception :
@@ -652,12 +653,18 @@ def _module_in_set(name, set):
652653 return False
653654
654655
655- def strip_string (value , max_length = 512 ):
656- # type: (str, int) -> Union[AnnotatedValue, str]
656+ def strip_string (value , max_length = None ):
657+ # type: (str, Optional[ int] ) -> Union[AnnotatedValue, str]
657658 # TODO: read max_length from config
658659 if not value :
659660 return value
661+
662+ if max_length is None :
663+ # This is intentionally not just the default such that one can patch `MAX_STRING_LENGTH` and affect `strip_string`.
664+ max_length = MAX_STRING_LENGTH
665+
660666 length = len (value )
667+
661668 if length > max_length :
662669 return AnnotatedValue (
663670 value = value [: max_length - 3 ] + u"..." ,
0 commit comments