Skip to content

Handle dictionary keys that cannot be sorted in has_entries - #279

Open
ryanduguid wants to merge 6 commits into
hamcrest:mainfrom
ryanduguid:fix-unorderable-dictionary-keys
Open

ryanduguid wants to merge 6 commits into
hamcrest:mainfrom
ryanduguid:fix-unorderable-dictionary-keys

Conversation

@ryanduguid

@ryanduguid ryanduguid commented Oct 8, 2026 •

Copy link
Copy Markdown

Summary

Allow has_entries to match dictionaries with mixed or unordered keys, including frozen dataclass instances. Preserve sorted descriptions and matcher evaluation order when sorting succeeds; use the original item order when comparisons raise TypeError.

items = list(value_matchers.items())
try:
    ordered_items = sorted(items)
except TypeError:
    ordered_items = items

Fixes #271. Add ten regression tests and a changelog fragment.

Evidence

  • Before: The initial regression run against the original runtime produces seven failing outcomes and 22 passes, including subtests. Mixed and frozen dataclass keys raise TypeError during construction.
    After: The focused suite passes all 27 tests. Coverage includes descriptions, first mismatch, evaluation order, input preservation, one-shot item iteration and exception boundaries.
  • Linux uv tool run tox -s: all 20 configured environments pass, including typing, lint, documentation, package description, changelog and coverage. CPython 3.10 through 3.14 each passes 456 tests with 14 NumPy skips, or 470 with NumPy. The four PyPy aliases each pass 448 tests with 15 skips and all resolve to PyPy 3.10.16.
  • Windows Python 3.14: 456 tests pass with 14 skips and two warnings; the configured lint target passes. Deliberate mutations that retry item acquisition, evaluate a second value after the first failure, or sort the fallback list in place fail the corresponding regression assertions.
  • Documentation builds successfully with 274 warnings. Coverage reports 98% for the changed runtime file and no partial branches.

Risk

The fallback also absorbs TypeError raised by a custom comparison method. Errors from obtaining or materialising items, other comparison exceptions and value matcher exceptions still propagate. Fallback descriptions and the first mismatch follow the supplied item order.

Unverified

PyPy, macOS and other Windows Python versions were not run. Documentation warnings were not compared with master. Hosted CI has not run.

Keep the original item snapshot when sorting raises TypeError, while
preserving sorted order when comparisons succeed. Cover diagnostic and
evaluation order, one-shot items and exception boundaries, and add the
required changelog fragment for issue 271.
@ryanduguid
ryanduguid requested a deployment to publish-test-release October 8, 2026 10:12 — with GitHub Actions Waiting
self.assert_does_not_match("second fails", matcher, {order[0]: 1, order[1]: 0})
self.assertEqual(order, calls)

def testOneShotItemsAreConsumedOnceAndRemainReusable(self):

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.

Barring a few very specific examples, the tests in this repo go through the factory functions (has_entries) without directly instantiating the matcher object. I would prefer to keep that pattern unless there's a compelling reason not to, and I don't see one here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved the coverage to has_entries and dropped the three tests that built IsDictContainingEntries directly (e748f27).

@ryanduguid
ryanduguid requested a deployment to publish-test-release October 14, 2026 12:28 — with GitHub Actions Waiting
@ryanduguid

Copy link
Copy Markdown
Author

@offbyone the tests now go through has_entries only (e748f27), as you asked. I have also reverted two unrelated workflow commits that had been added to this branch (d95eaa2), so the diff is back to the fix, its tests and the changelog fragment. Could you take another look when you have time?

This branch is waiting to be deployed

1 waiting (outdated) deployment
publish-test-release — e748f270 Waiting Oct 14, 2026 by ryanduguid via Publish in-dev package to test.pypi.org #254
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.

has_entries TypeError if keys can't sort

2 participants