Skip to content

fix: approx() tolerance not applied to sequences/mappings nested inside dicts#14498

Open
Ashutosh-177 wants to merge 2 commits into
pytest-dev:mainfrom
Ashutosh-177:fix/approx-nested-sequences
Open

fix: approx() tolerance not applied to sequences/mappings nested inside dicts#14498
Ashutosh-177 wants to merge 2 commits into
pytest-dev:mainfrom
Ashutosh-177:fix/approx-nested-sequences

Conversation

@Ashutosh-177
Copy link
Copy Markdown

Running into an issue where pytest.approx works fine at the top level but silently falls back to exact equality when the same sequence or mapping appears as a dict value.

For example:

assert {"a": [1.0, 2.0 + 1e-7]} == pytest.approx({"a": [1.0, 2.0]}, abs=1e-5)

This fails even though the tolerance should cover the difference. The top-level approx() call dispatches to ApproxMapping, which iterates over the dict values and calls _approx_scalar() on each one. But _approx_scalar() only checks for Decimal and datetime/timedelta — everything else, including lists and nested dicts, falls through to ApproxScalar, which does exact comparison.

The top-level approx() dispatcher already has the correct isinstance checks for Mapping and sequence-like types. Adding the same two checks to _approx_scalar() makes nested structures recurse properly.

Fixes #8703

When approx() is called on a dict whose values are themselves lists or
dicts, _approx_scalar() was returning ApproxScalar for those values,
which does exact equality. The top-level approx() dispatcher correctly
routes sequences to ApproxSequenceLike and mappings to ApproxMapping,
but _approx_scalar() -- called per-element by ApproxMapping.__eq__ --
missed those cases.

Adding the same isinstance checks in _approx_scalar means nested
structures get recursive approximate comparison instead of falling back
to ==.

Fixes pytest-dev#8703

Signed-off-by: Ashutosh Kumar Singh <ahutoshhjp1067@gmail.com>
Copilot AI review requested due to automatic review settings May 19, 2026 16:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Extends _approx_scalar in python_api.py to dispatch Mapping and sequence-like values to ApproxMapping and ApproxSequenceLike respectively, enabling nested approximate comparisons within scalar dispatch.

Changes:

  • Add Mapping branch to return ApproxMapping.
  • Add sequence-like branch to return ApproxSequenceLike.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/_pytest/python_api.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug with tolerances and other undesired behavior with approx using dict

2 participants