Skip to content

[refactor] Introduce a TruncationBudget for assertion truncation#14652

Merged
Pierre-Sassoulas merged 2 commits into
pytest-dev:mainfrom
Pierre-Sassoulas:truncation-budget-refactor
Jun 28, 2026
Merged

[refactor] Introduce a TruncationBudget for assertion truncation#14652
Pierre-Sassoulas merged 2 commits into
pytest-dev:mainfrom
Pierre-Sassoulas:truncation-budget-refactor

Conversation

@Pierre-Sassoulas

Copy link
Copy Markdown
Member

Proposal of refactor prior to #14523 where a lot of truncation budget will be passed around in assertion generator.

Bundle the (max_lines, max_chars) pair that the truncation machinery passes around into a small frozen, keyword-only TruncationBudget. _get_truncation_parameters now returns (should_truncate, budget) and _truncate_explanation takes the budget directly. Keyword-only construction means the two limits can never be silently swapped at a call site.

The budget carries the truncation_limit_lines / truncation_limit_chars ini values verbatim — positive bounds the dimension, 0 leaves it unbounded — so there is no extra sentinel translation. Pure refactor: truncation behaviour, footer wording and hidden-line count are unchanged.

@Pierre-Sassoulas Pierre-Sassoulas added the skip news used on prs to opt out of the changelog requirement label Jun 25, 2026
@Pierre-Sassoulas

Copy link
Copy Markdown
Member Author

@bluetech what do you think ?

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea to combine these to a type 👍

Comment thread src/_pytest/assertion/_typing.py Outdated
Comment on lines +19 to +20
Constructed keyword-only so the two limits can never be silently
swapped at a call site.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this comment, I don't think it needs to be explained.

Comment thread src/_pytest/assertion/truncate.py Outdated
Comment on lines +31 to +32
The budget carries the ``truncation_limit_lines`` / ``truncation_limit_chars``
ini values verbatim, where ``0`` means the matching dimension is unbounded.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this sentence, duplicates the TruncationBudget docstring

Bundle the (max_lines, max_chars) pair that the truncation machinery
passes around into a small frozen, keyword-only TruncationBudget.
_get_truncation_parameters now returns (should_truncate, budget) and
_truncate_explanation takes the budget directly. Keyword-only
construction means the two limits can never be silently swapped at a
call site.

The budget carries the truncation_limit_lines / truncation_limit_chars
ini values verbatim — positive bounds the dimension, 0 leaves it
unbounded — so there is no extra sentinel translation. Pure refactor:
truncation behaviour, footer wording and hidden-line count are
unchanged.
Drop the keyword-only rationale from TruncationBudget and the verbatim
ini-values sentence from _get_truncation_parameters; both restated info
already carried by the TruncationBudget docstring.
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the truncation-budget-refactor branch from 3da7181 to 6bde1b2 Compare June 28, 2026 08:25
@Pierre-Sassoulas Pierre-Sassoulas merged commit ed7672b into pytest-dev:main Jun 28, 2026
35 checks passed
@Pierre-Sassoulas Pierre-Sassoulas deleted the truncation-budget-refactor branch June 28, 2026 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news used on prs to opt out of the changelog requirement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants