Skip to content

Support generic mappings in assertion comparison#14482

Open
codingkiddo wants to merge 3 commits into
pytest-dev:mainfrom
codingkiddo:assertion-support-generic-mapping
Open

Support generic mappings in assertion comparison#14482
codingkiddo wants to merge 3 commits into
pytest-dev:mainfrom
codingkiddo:assertion-support-generic-mapping

Conversation

@codingkiddo
Copy link
Copy Markdown

@codingkiddo codingkiddo commented May 14, 2026

Summary

Support rich assertion comparison output for objects implementing
collections.abc.Mapping, not only concrete dict instances.

Motivation

_compare_eq_dict() already accepts Mapping[object, object], but the
dispatch logic only recognized actual dict instances. As a result,
custom mapping implementations fell back to generic iterable diff output
instead of the more useful mapping-specific explanation.

Changes

  • Broaden the mapping guard in isdict() from dict to Mapping
  • Add a regression test using a custom Mapping implementation that is
    not a dict subclass
  • Verify that mapping-specific assertion details are rendered

Tests

python -m pytest testing/test_assertion.py -k Mapping
python -m pytest testing/test_assertion.py -k "Mapping or dict"

Fixes #14461

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label May 14, 2026
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.

Support generic mapping in builtin assertrepr compare

1 participant