@@ -195,6 +195,13 @@ def __init__(self, value, metadata):
195
195
self .metadata = metadata
196
196
197
197
198
+ if MYPY :
199
+ from typing import TypeVar
200
+
201
+ T = TypeVar ("T" )
202
+ Annotated = Union [AnnotatedValue , T ]
203
+
204
+
198
205
def get_type_name (cls ):
199
206
# type: (Optional[type]) -> Optional[str]
200
207
return getattr (cls , "__qualname__" , None ) or getattr (cls , "__name__" , None )
@@ -236,22 +243,13 @@ def iter_stacks(tb):
236
243
tb_ = tb_ .tb_next
237
244
238
245
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
-
248
246
def get_lines_from_file (
249
247
filename , # type: str
250
248
lineno , # type: int
251
249
loader = None , # type: Optional[Any]
252
250
module = None , # type: Optional[str]
253
251
):
254
- # type: (...) -> Tuple[List[str], Optional[str], List[str]]
252
+ # type: (...) -> Tuple[List[Annotated[ str]] , Optional[Annotated[ str]] , List[Annotated[ str] ]]
255
253
context_lines = 5
256
254
source = None
257
255
if loader is not None and hasattr (loader , "get_source" ):
@@ -276,11 +274,11 @@ def get_lines_from_file(
276
274
277
275
try :
278
276
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 ]
280
278
]
281
- context_line = slim_string (source [lineno ].strip ("\r \n " ))
279
+ context_line = strip_string (source [lineno ].strip ("\r \n " ))
282
280
post_context = [
283
- slim_string (line .strip ("\r \n " ))
281
+ strip_string (line .strip ("\r \n " ))
284
282
for line in source [(lineno + 1 ) : upper_bound ]
285
283
]
286
284
return pre_context , context_line , post_context
@@ -289,8 +287,11 @@ def get_lines_from_file(
289
287
return [], None , []
290
288
291
289
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]]]
294
295
try :
295
296
abs_path = frame .f_code .co_filename # type: Optional[str]
296
297
except Exception :
@@ -652,12 +653,18 @@ def _module_in_set(name, set):
652
653
return False
653
654
654
655
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]
657
658
# TODO: read max_length from config
658
659
if not value :
659
660
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
+
660
666
length = len (value )
667
+
661
668
if length > max_length :
662
669
return AnnotatedValue (
663
670
value = value [: max_length - 3 ] + u"..." ,
0 commit comments