Skip to content

Fix NUMBER and ELLIPSIS doctest interaction - #15101

Open
Yoruxyv wants to merge 1 commit into
pytest-dev:mainfrom
Yoruxyv:fix-doctest-number-ellipsis
Open

Yoruxyv wants to merge 1 commit into
pytest-dev:mainfrom
Yoruxyv:fix-doctest-number-ellipsis

Conversation

@Yoruxyv

@Yoruxyv Yoruxyv commented Sep 25, 2026 •

Copy link
Copy Markdown

Closes #13327

Summary

Fixes the interaction between the NUMBER and ELLIPSIS doctest options when an ellipsized region contains additional numeric values.

Previously, NUMBER collected numeric tokens from the entire expected and actual output and paired them positionally. This breaks with ELLIPSIS, which can hide arbitrary output including additional numbers.

This change keeps the existing NUMBER-only path unchanged and adds an ellipsis-aware fallback that aligns the visible, non-ellipsis portions of the expected output before applying the existing NUMBER precision logic.

It also preserves NORMALIZE_WHITESPACE behavior by normalizing whitespace before the ellipsis-aware alignment.

Reproduction

The original issue can be reproduced using the reporter's repository:

git clone https://github.com/wade-cheng/pytest-doctest-bug.git
cd pytest-doctest-bug

python -m pytest -v --doctest-modules working.py
python -m pytest -v --doctest-modules failing.py

On unmodified pytest main, working.py passes while failing.py fails.

With this change, both pass.

Tests

Added coverage for:

  • NUMBER before and after an ellipsized region containing additional numbers
  • multiple ellipsized regions
  • a non-matching visible number, to ensure the fallback does not introduce false positives
  • NUMBER + ELLIPSIS + NORMALIZE_WHITESPACE

Validation performed with:

python -m pytest testing/test_doctest.py -k "number" -vv
python -m pytest testing/test_doctest.py -vv
python -m tox -e py313 -- testing/test_doctest.py
python -m tox -e linting

Results:

NUMBER-focused tests: 24 passed, 1 xfailed
testing/test_doctest.py: 165 passed, 1 xfailed
tox py313: 165 passed, 1 xfailed
linting: passed

Why not just remove the numeric-count check?

The existing implementation assumes that the expected and actual output contain the same number of numeric tokens. Simply removing the numeric-count check is not sufficient: the current pairing uses zip(..., strict=True), and relaxing it would still leave numeric tokens paired by their global position.

For example:

Expected:
... final=3.0

Actual:
hidden=2.0 final=3.0001

A positional pairing would associate the expected 3.0 with the hidden 2.0, even though that value belongs to the ellipsized region.

The fallback therefore aligns the visible portions first and only applies NUMBER matching to numbers that belong to those portions.

@Yoruxyv
Yoruxyv force-pushed the fix-doctest-number-ellipsis branch from 5246203 to f1b2037 Compare September 25, 2026 08:35
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 25, 2026

@MateehUllah MateehUllah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The ellipsis-aware suffix path can raise ValueError for an ordinary non-match instead of returning False. For example, with want='prefix ... value=1.0' and got='prefix', position is negative, but match_numbers(position, last_numbers) is called before the position < start and endswith guards. That produces an empty matched_got_numbers slice, and zip(..., strict=True) raises because last_numbers contains one item.

A doctest mismatch should remain a failed comparison, not escape from the output checker. Please perform the suffix position/shape checks before calling match_numbers and add a regression with a missing or shortened trailing numeric chunk.

@Yoruxyv
Yoruxyv force-pushed the fix-doctest-number-ellipsis branch from f1b2037 to bf125bd Compare September 25, 2026 09:11
@Yoruxyv

Yoruxyv commented Sep 25, 2026

Copy link
Copy Markdown
Author

Thank you so much for catching this @MateehUllah. I had missed that edge case, and your example made the failure mode very clear. I’ve updated the suffix handling so the position/shape checks happen before numeric matching and added a regression test for the shortened trailing chunk. Really appreciate the review!

Co-authored-by: ChatGPT <noreply@openai.com>
@Yoruxyv
Yoruxyv force-pushed the fix-doctest-number-ellipsis branch from bf125bd to e8ed97a Compare September 25, 2026 09:55
@Yoruxyv

Yoruxyv commented Sep 25, 2026

Copy link
Copy Markdown
Author

While rechecking the ellipsis-aware path against stdlib doctest.OutputChecker semantics, I found two additional preprocessing cases that needed to be preserved: <BLANKLINE> handling and ASCII/Unicode canonicalization before ellipsis matching.
I added focused regression coverage for both, including preservation of DONT_ACCEPT_BLANKLINE, and updated the fallback to apply the same relevant pre-ellipsis canonicalization before performing the NUMBER-aware alignment. The final comparison is still delegated to stdlib's OutputChecker.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

doctest options NUMBER and ELLIPSIS seem incompatible

2 participants