Skip to content

Commit c454d2f

Browse files
[truncate explanation] Remove some redundant checks by grouping them together
1 parent b86c6f4 commit c454d2f

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

src/_pytest/assertion/truncate.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,14 @@ def _truncate_explanation(
8080
tolerable_max_chars = (
8181
max_chars + 70 # 64 + 1 (for plural) + 2 (for '99') + 3 for '...'
8282
)
83-
if (
84-
# The truncation explanation add two lines to the output
85-
max_lines == 0 or len(input_lines) <= max_lines + 2
86-
) and (
87-
max_chars == 0 or sum(len(line) for line in input_lines) <= tolerable_max_chars
88-
):
89-
return input_lines
90-
# Truncate first to max_lines, and then truncate to max_chars if necessary
91-
if max_lines > 0:
92-
truncated_explanation = input_lines[:max_lines]
93-
else:
83+
# The truncation explanation add two lines to the output
84+
if max_lines == 0 or len(input_lines) <= max_lines + 2:
85+
if max_chars == 0 or sum(len(s) for s in input_lines) <= tolerable_max_chars:
86+
return input_lines
9487
truncated_explanation = input_lines
88+
else:
89+
# Truncate first to max_lines, and then truncate to max_chars if necessary
90+
truncated_explanation = input_lines[:max_lines]
9591
# We reevaluate the need to truncate chars following removal of some lines
9692
need_to_truncate_char = (
9793
max_chars > 0

0 commit comments

Comments
 (0)