Skip to content

Commit 49bb987

Browse files
[truncate explanation] Optimize the string length calculation
sum(len(x for s in strings) is consistentely faster than len(''.join(strings)), see https://claude.ai/public/artifacts/6a4c33e7-9ad5-4078-8ee7-e343984ce087
1 parent 4e543e8 commit 49bb987

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

src/_pytest/assertion/truncate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _truncate_explanation(
6767
because _get_truncation_parameters was called first.
6868
"""
6969
# Check if truncation required
70-
input_char_count = len("".join(input_lines))
70+
input_char_count = sum(len(line) for line in input_lines)
7171
# The length of the truncation explanation depends on the number of lines
7272
# removed but is at least 68 characters:
7373
# The real value is
@@ -96,7 +96,8 @@ def _truncate_explanation(
9696
truncated_explanation = input_lines
9797
# We reevaluate the need to truncate chars following removal of some lines
9898
need_to_truncate_char = (
99-
max_chars > 0 and len("".join(truncated_explanation)) > tolerable_max_chars
99+
max_chars > 0
100+
and sum(len(e) for e in truncated_explanation) > tolerable_max_chars
100101
)
101102
if need_to_truncate_char:
102103
truncated_explanation = _truncate_by_char_count(

0 commit comments

Comments
 (0)